code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int a, b; scan(a, b);
if (1 <= a && a <= 9 && 1 <= b && b <= 9)
writeln(a * b);
else writeln("-1");
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib, core.bitop;
void main(){
auto S = scanString;
writeln((15-S.length)+S.count('o')>=8?"YES":"NO");
}
long gcd(long a, long b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
string scanString()
{
return readln.strip;
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(T[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
//素因数分解
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
//約数をすべて列挙
long[] divisors(long n)
{
long[] list;
void func(Factor[] fs, long n)
{
if(fs.empty){
list ~= n;
return;
}
foreach(c; 0..fs[0].c+1)
{
func(fs[1..$], n * (fs[0].n ^^ c));
}
}
func(factors(n), 1);
sort(list);
return list;
}
//nまでの素数のリスト
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
//素数判定
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
void main() {
string n = readln.chomp;
string m;
foreach (x; n) {
m ~= x == '1' ? '9' : '1';
}
m.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std;
import core.bitop;
ulong MAX = 100_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, "x", long, "g");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
auto t = sread();
sort!"a<b"(cast(ubyte[]) s);
sort!"a>b"(cast(ubyte[]) t);
// s.writeln();
// t.writeln();
if (s < t)
writeln("Yes");
else
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp;
auto dp = new int[](s.length+1);
dp[0] = 0;
dp[1] = 1;
if (dp.length > 2) {
if (s[0] == s[1]) {
dp[2] = 1;
} else {
dp[2] = 2;
}
}
foreach (i; 3..s.length+1) {
if (s[i-2] != s[i-1]) {
dp[i] = dp[i-1] + 1;
} else {
dp[i] = max(dp[i-1], dp[i-3] + 2);
}
}
//dp.writeln;
dp[$-1].writeln;
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
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); }
void main() {
auto size = readln.chomp.to!int;
auto lucky = cast(byte[])readln.chomp;
lucky[] -= '0';
void solve() {
int count;
foreach(x; 0..10) foreach(y; 0..10) foreach(z; 0..10) {
if (!lucky.canFind(x)) continue;
auto after = lucky.findSplitAfter([x]);
if (!after[1].canFind(y)) continue;
after = after[1].findSplitAfter([y]);
if (!after[1].canFind(z)) continue;
count++;
}
writeln(count);
}
solve();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto ds = readln.split.to!(int[]);
bool[24] T;
T[0] = true;
int solve(int i) {
if (i == N) {
int r = 12, j;
foreach (k; 1..24) if (T[k]) {
r = min(r, k - j, 24 - k + j);
j = k;
}
r = min(r, 24 - j);
return r;
} else {
if (ds[i] == 0) return 0;
int j = ds[i], k = 24-ds[i], r1, r2;
if (!T[j]) {
T[j] = true;
r1 = solve(i+1);
T[j] = false;
}
if (!T[k]) {
T[k] = true;
r2 = solve(i+1);
T[k] = false;
}
return max(r1, r2);
}
}
writeln(solve(0));
}
|
D
|
import std.stdio: writeln, readln;
import std.string: strip;
void main() {
string a = readln.strip;
string b = readln.strip;
if (a == b)
writeln("EQUAL");
else if (a.length > b.length)
writeln("GREATER");
else if (b.length > a.length)
writeln("LESS");
else if (a > b)
writeln("GREATER");
else
writeln("LESS");
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln(readln.chomp == "1" ? "Hello World" : (2.iota.map!(_ => readln.chomp.to!int).sum).to!string);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
writeln(n*(n+1)/2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_; 0..T) {
auto t = readln.chomp;
int o, z;
foreach (c; t) {
if (c == '0') ++z;
if (c == '1') ++o;
}
if (o == 0 || z == 0) {
writeln(t);
continue;
}
char[] s;
foreach (i; 0..t.length-1) {
s ~= t[i];
if (t[i] == t[i+1]) {
if (t[i] == '0') {
s ~= '1';
} else {
s ~= '0';
}
}
}
s ~= t[$-1];
writeln(s);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main()
{
int N = readln.chomp.to!int;
auto buf = readln.split;
int A = buf[0].to!int;
int B = buf[1].to!int;
int[] P = readln.split.to!(int[]);
int[] cnt = [0, 0, 0];
foreach (i; 0..N){
if (P[i] <= A){
cnt[0]++;
}
else if (P[i] <= B){
cnt[1]++;
}
else {
cnt[2]++;
}
}
int min_cnt = cnt[0];
foreach (i; 1..3){
if (cnt[i] < min_cnt) min_cnt = cnt[i];
}
writeln(min_cnt);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[][10^^5*2] T;
bool[10^^5*2] FS;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N-1) {
auto ab = readln.split.to!(int[]);
auto a = ab[0]-1;
auto b = ab[1]-1;
T[a] ~= b;
T[b] ~= a;
}
alias EL = Tuple!(int, "e", int, "l");
EL view(int i, int l) {
auto el = EL(i, l+1);
FS[i] = true;
foreach (n; T[i]) {
if (FS[n]) continue;
auto nn = view(n, l+1);
el = el.l > nn.l ? el : nn;
}
return el;
}
auto p1 = view(0, 0);
FS[] = false;
auto p2 = view(p1.e, 0);
writeln(p2.l > 1 && (p2.l-2)%3 == 0 ? "Second" : "First");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto D = RD;
writeln(max(min(B, D) - max(A, C), 0));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto a = readln.split.to!(int[]);
auto b = readln.split.to!(int[]);
foreach (i ; 0 .. N) {
if (a[i] != b[i] && b[i] >= (a[i] + 1) / 2) {
writeln(-1);
return;
}
}
auto d = new long[][](50 + 1, 50 + 1);
fillAll(d, infl);
foreach (i ; 0 .. 51) d[i][i] = 0;
foreach (i ; 1 .. 51) {
foreach (j ; 1 .. i + 1) {
chmin(d[i][i % j], 1L << j);
}
}
foreach (i ; 0 .. 51) {
foreach (j ; 0 .. 51) {
foreach (k ; 0 .. 51) {
chmin(d[i][j], d[i][k] + d[k][j]);
}
}
}
auto s = new long[](N);
foreach (i ; 0 .. N) {
s[i] = 1L << a[i];
}
auto f = new bool[](N);
long ans;
foreach (_ ; 0 .. N) {
int im;
long cm;
foreach (i ; 0 .. N) {
if (f[i]) continue;
long cst = infl;
foreach (j ; 0 .. 51) {
if (s[i] & 1L << j) {
chmin(cst, d[j][b[i]]);
}
}
if (cm < cst) {
cm = cst;
im = i;
}
}
f[im] = true;
ans |= cm;
foreach (i ; 0 .. N) {
foreach (j ; 0 .. 51) if (s[i] & 1L << j) {
foreach (k ; 1 .. 51) if (cm & 1L << k) {
s[i] |= 1L << (j % k);
}
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, k; rd(n, k);
long tot=0;
for(int a=1; a<=n; a++){
if(a%k==0){
auto y=(n/k).to!(long);
tot+=y*y;
}else{
auto x=k-a%k;
if((x*2)%k==0){
if(n>=x){
auto y=((n-x)/k+1).to!(long);
tot+=y*y;
}
}
// writeln(a, " ", tot);
}
}
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long a, b, c;
scan(a, b, c);
writeln(min(a + b, a + c, b + c));
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
void main()
{
for(string S; (S=readln().chomp()).length; )
{
auto n = S.to!int();
string w = "<3";
foreach(i;0..n)
w ~= readln().chomp() ~ "<3";
immutable string t = readln().chomp().idup;
const auto M = w.length;
const auto N = t.length;
if(M>N){ writeln("no"); return; }
auto dp = new int[N+1];
auto dpn = new int[N+1];
foreach(j,v;w)
{
foreach(i;j..j+N-M+1)
dpn[i+1]=max(dpn[i],dp[i+(v==t[i]?0:1)]+(v==t[i]?1:0));
dp.swap(dpn);
}
writeln(w.length==dp[N]?"yes":"no");
}
}
|
D
|
import std.stdio,
std.array,
std.conv,
std.string;
void main(){
string s;
for(;;){
s=readln();
if(stdin.eof()) break;
for(int i = cast(int)s.length; i > 1; --i ){
write(s[i-2]);
}
if(s.length > 0) write("\n");
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
int h, w, k;
scan(h, w, k);
auto c = iota(h).map!(i => readln.chomp).array;
int ans;
foreach (rs; 0 .. 1 << h) {
foreach (cs; 0 .. 1 << w) {
int cnt;
foreach (i; 0 .. h) {
foreach (j; 0 .. w) {
if (c[i][j] == '.')
continue;
if ((1 << i) & rs || (1 << j) & cs)
continue;
cnt++;
}
}
ans += (k == cnt);
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import 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()
{
readln();
auto a = aryread();
a.reduce!((x, y) => gcd(x, y)).writeln();
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long N, K;
scan(N, K);
long R, S, P;
scan(R, S, P);
auto T = sread();
auto dp = new long[][](N + 1, 3);
foreach (i; 0 .. N)
{
if (i < K)
{
switch (T[i])
{
case 'r':
dp[i + 1][2] = P;
break;
case 's':
dp[i + 1][0] = R;
break;
case 'p':
dp[i + 1][1] = S;
break;
default:
assert(0);
}
}
else
{
// dprint(i - K);
foreach (j; 0 .. 3)
dp[i + 1][j] = dp[i + 1 - K].reduce!max;
switch (T[i])
{
case 'r':
dp[i + 1][2] = max(dp[i + 1 - K][0], dp[i + 1 - K][1]) + P;
break;
case 's':
dp[i + 1][0] = max(dp[i + 1 - K][1], dp[i + 1 - K][2]) + R;
break;
case 'p':
dp[i + 1][1] = max(dp[i + 1 - K][0], dp[i + 1 - K][2]) + S;
break;
default:
assert(0);
}
}
}
dp[$ - K .. $].map!(reduce!max).sum().writeln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto a = ab[0];
auto b = ab[1];
if (a > 0) {
writeln("Positive");
} else if (a <= 0 && b >= 0) {
writeln("Zero");
} else {
writeln((b-a+1)%2 == 0 ? "Positive" : "Negative");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
int input;
immutable limitList = 200000;
bool[] listNumbers = new bool[](limitList);
int[] listPrimeNumbers;
listNumbers.fill(true);
foreach (i; 2..limitList.to!double.sqrt.to!int) {
if (listNumbers[i]) {
for (int j = i*2; j < limitList; j += i) listNumbers[j] = false;
}
}
foreach (i; 2..listNumbers.length) {
if (listNumbers[i]) {
listPrimeNumbers ~= i.to!int;
}
}
while ((input = readln.chomp.to!int) != 0) {
ulong sum = 0;
foreach (i; 0..listPrimeNumbers.length) {
if (i == input) break;
sum += listPrimeNumbers[i];
}
writeln(sum);
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto Q = s[1];
auto st = new LazySegmentTree!(long, long, min, (a,b)=>b, (a,b)=>b, (a,b)=>a, (1L<<31)-1, (1L<<31)-1)(N);
while (Q--) {
s = readln.split.map!(to!int);
int q = s[0];
int l = s[1];
int r = s[2];
if (q == 0) {
long v = s[3];
st.update(l, r, v);
} else {
st.query(l, r).writeln;
}
}
}
class LazySegmentTree(T, L, alias opTT, alias opTL, alias opLL, alias opPrd, L eL, T eT) {
T[] table;
L[] lazy_;
int n;
int size;
this(int n) {
this.n = n;
size = 1;
while (size <= n) size <<= 1;
size <<= 1;
table = new T[](size);
lazy_ = new T[](size);
table[] = eT;
lazy_[] = eL;
}
void push(int i, int a, int b) {
if (lazy_[i] == eL) return;
table[i] = opTL(table[i], opPrd(lazy_[i], b - a + 1));
if (i * 2 + 1 < size) {
lazy_[i*2] = opLL(lazy_[i*2], lazy_[i]);
lazy_[i*2+1] = opLL(lazy_[i*2+1], lazy_[i]);
}
lazy_[i] = eL;
}
T query(int l, int r) {
if (l > r) return eT;
return query(l, r, 1, 0, n-1);
}
T query(int l, int r, int i, int a, int b) {
if (b < l || r < a) return eT;
push(i, a, b);
if (l <= a && b <= r) {
return table[i];
} else {
return opTT(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
}
}
void update(int l, int r, L val) {
if (l > r) return;
update(l, r, 1, 0, n-1, val);
}
void update(int l, int r, int i, int a, int b, L val) {
if (b < l || r < a) {
push(i, a, b);
} else if (l <= a && b <= r) {
lazy_[i] = opLL(lazy_[i], val);
push(i, a, b);
} else {
push(i, a, b);
update(l, r, i*2, a, (a+b)/2, val);
update(l, r, i*2+1, (a+b)/2+1, b, val);
table[i] = opTT(table[i*2], table[i*2+1]);
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
long calc(long n, long a, long b) {
if (n == 1) {
return a == b ? 1 : 0;
}
if (a > b) {
return 0;
}
return (n - 2) * (b - a) + 1;
}
void main() {
auto nab = readints;
int n = nab[0], a = nab[1], b = nab[2];
writeln(calc(n, a, b));
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=readln.split.to!(long[]);
a=0L~a~0L;
long s=0;
import std.math;
for(int i=0; i<=n; i++){
s+=(a[i+1]-a[i]).abs;
}
for(int i=1; i<=n; i++){
auto t=s;
t-=(a[i]-a[i-1]).abs;
t-=(a[i+1]-a[i]).abs;
t+=(a[i+1]-a[i-1]).abs;
writeln(t);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
readint();
auto ss = assumeSorted(readints());
readint();
auto ts = readints();
ulong ans = ts.count!(e => ss.contains(e));
writeln(ans);
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
void main()
{
while (1) {
string[] str = split(readln());
int a = to!(int)(str[0]);
int b = to!(int)(str[1]);
if (a == 0 && b == 0) {
break;
} else if (a > b) {
writeln(b, " ", a);
} else {
writeln(a, " ", b);
}
}
}
|
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;
int[] a;
void main() {
scan(n);
a = iota(n).map!(i => readln.chomp.to!int - 1).array;
auto d = new int[](n);
d[] = -1;
d[0] = 0;
int pos;
while (pos != 1) {
if (d[a[pos]] == -1) {
d[a[pos]] = d[pos] + 1;
pos = a[pos];
}
else {
break;
}
}
writeln(d[1]);
}
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);
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto x = readln.split.to!(int[]);
int L, Q;
scan(L);
scan(Q);
auto dp = new int[][](N, 20);
int r = N - 1;
foreach_reverse (i ; 0 .. N) {
while (x[r] - x[i] > L) {
r--;
}
assert(r >= i);
dp[i][0] = r;
}
foreach (j ; 1 .. 20) {
foreach (i ; 0 .. N) {
dp[i][j] = dp[dp[i][j - 1]][j - 1];
}
}
debug {
writefln("%(%s\n%)", dp);
}
foreach (_ ; 0 .. Q) {
int a, b;
scan(a, b);
a--, b--;
if (a > b) swap(a, b);
int ans;
foreach_reverse (i ; 0 .. 20) {
if (a == b) break;
if (dp[a][i] < b) {
a = dp[a][i];
ans += 1 << i;
}
}
assert(a <= b);
if (a < b) ans++;
writeln(ans);
}
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() { // 日本語文字列のテスト
int w = readint;
int n = readint;
auto xs = new int[w + 1];
for (int i = 0; i < xs.length; i++)
xs[i] = i;
for (int i = 0; i < n; i++) {
auto ab = readln.chomp.split(",").to!(int[]);
int a = ab[0], b = ab[1];
swap(xs[a], xs[b]);
}
foreach (x; xs[1 .. $])
writeln(x);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
immutable W = 30;
long N = lread();
auto A = aryread!uint();
auto S = new long[][](N + 1, W);
foreach (i; 0 .. N)
foreach (j; 0 .. W)
{
S[i + 1][j] = S[i][j] + ((A[i] & (1 << j)) != 0);
}
long i;
long j;
long ans;
while (i < N)
{
incj: while (true)
{
if (!(j <= N))
break incj;
auto tmp = new long[](W);
tmp[] = S[j][] - S[i][];
// writeln(tmp);
if (tmp.all!(x => x <= 1)())
j++;
else
break incj;
}
j--;
long len = j - i;
// writefln("%s %s %s", i, j, len);
ans += len;
i++;
inci: while (true)
{
if (!(i < N) || !(i <= j))
break inci;
auto tmp = new long[](W);
tmp[] = S[j][] - S[i][];
// writeln("j", tmp);
if (!(tmp.all!(x => x <= 1)()))
i++;
else
break inci;
}
}
writeln(ans);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, q; rd(n, q);
auto tree=new SquareRootDecomposition(n);
while(q--){
auto args=readln.split.to!(int[]);
if(args[0]==0){
tree.add(args[1]-1, args[2], args[3]);
}else{
writeln(tree.sum(args[1]-1, args[2]));
}
}
}
class SquareRootDecomposition{ // starry
import std.algorithm;
int D=1, nil=-1;
long[] val, buc, star;
this(int n){
while(D*D<n) D++;
val.length=D*D;
buc.length=star.length=D;
}
void add(int ql, int qr, long x){
foreach(k; 0..D){
int l=k*D, r=(k+1)*D;
if(r<=ql || qr<=l){
//
}else if(ql<=l && r<=qr){
star[k]+=x;
}else{
// push(k);
foreach(i; max(l, ql)..min(r, qr)){
buc[k]+=x;
val[i]+=x;
}
}
}
}
long sum(int ql, int qr){
long ret=0;
foreach(k; 0..D){
int l=k*D, r=(k+1)*D;
if(r<=ql || qr<=l){
//
}else if(ql<=l && r<=qr){
ret+=(buc[k]+star[k]*D);
}else{
int s=max(l, ql), t=min(r, qr);
ret+=star[k]*(t-s);
foreach(i; s..t) ret+=val[i];
}
}
return ret;
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void calc(int a, int b) {
int d = abs(a - b);
if (d % 2 == 1) {
writeln("IMPOSSIBLE");
return;
}
writeln(min(a, b) + d / 2);
}
void calc2(int a, int b) {
if (a % 2 != b % 2) {
writeln("IMPOSSIBLE");
return;
}
writeln((a + b) / 2);
}
void main() {
int a, b; scan(a, b);
calc2(a, b);
}
|
D
|
void main()
{
long[] tmp = readln.split.to!(long[]);
long a = tmp[0], b = tmp[1], c = tmp[2];
long x = tmp[3], y = tmp[4];
if (a + b < 2 * c)
{
writeln(a * x + b * y);
}
else
{
long z = min(x, y);
long u = a * (x - z) + b * (y - z);
long v = 2 * c * (max(x, y) - z);
writeln(2 * c * z + min(u, v));
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
N gcd(N)(N a, N b) if (isNumeric!N || is (N == BigInt)) { return a ? gcd(b % a, a) : b; }
N lcm(N)(N a, N b) if (isNumeric!N || is (N == BigInt)) { return a / gcd(a, b) * b; }
void main() {
long N = readln.chomp.to!long;
long[] T;
foreach (_; 0..N) {
T ~= readln.chomp.to!long;
}
long ans = T[0];
foreach (long t; T[1..$]) {
ans = lcm(ans, t);
}
writeln(ans);
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int n = readln.chomp.to!int;
int k = n / 15;
writeln(n * 800 - k * 200);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n, k;
scan(n, k);
long i;
while(n)
{
n /= k;
i++;
}
writeln(i);
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto w = readln.split.to!(int[]);
auto s = w.sum();
int ans = s;
int t;
foreach (i ; 0 .. n) {
t += w[i];
ans = min(ans, abs(s - 2*t));
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, m; rd(n, m);
auto g=new int[][](n, 0);
foreach(_; 0..m){
int a, b; rd(a, b);
a--; b--;
g[a]~=b; g[b]~=a;
}
auto c=new int[](n);
fill(c, -1);
bool dfs(int i, int w, int p=-1){
c[i]=w;
bool ret=true;
foreach(j; g[i])if(j!=p){
if(c[j]==-1) ret&=dfs(j, w^1, i);
if((c[j]^w)==0) ret=false;
}
return ret;
}
if(dfs(0, 0)){
long w=0;
foreach(e; c)if(e==0) w++;
writeln(w*(long(n)-w)-m);
}else{
writeln(long(n)*(n-1)/2-m);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial, std.numeric;
void main(){
readln.split.map!(to!int).map!"a-1".reduce!"a*b".writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
int a; rd(a);
if(n%500<=a) writeln("Yes");
else writeln("No");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
//END OF TEMPLATE
void main(){
auto s=scan!string;
auto t=scan!string;
foreach(i;0..s.length){
if(s[i]!=t[i]){
"No".writeln;
return;
}
}
"Yes".writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int H, W;
scan(H, W);
int h, w;
scan(h, w);
auto ans = (H - h) * (W - w);
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(string s) {
int n = cast(int)s.length;
assert(n % 2 == 0);
// 末尾から i 文字分だけ削る
for (int i = 1; i < n; i++) {
int m = (n - i) / 2;
auto a = s[0..m];
auto b = s[m..m * 2];
if (a == b) return m * 2;
}
assert(false);
}
void main() {
string s = read!string;
writeln(calc(s));
}
|
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 H = RD!int;
auto W = RD!int;
auto a = new string[](H);
foreach (y; 0..H)
a[y] = RD!string;
auto cnt = new long[](26);
foreach (y; 0..H)
{
foreach (x; 0..W)
{
++cnt[a[y][x]-'a'];
}
}
bool ans = true;
auto hh = H/2;
auto ww = W/2;
foreach (i; 0..hh*ww)
{
auto pos = cnt.MIN_POS!"a > b"();
if (cnt[pos] < 4)
{
ans = false;
break;
}
cnt[pos] -= 4;
}
auto hhh = H%2;
auto www = W%2;
foreach (i; 0..hhh*ww+www*hh)
{
auto pos = cnt.MIN_POS!"a > b"();
if (cnt[pos] < 2)
{
ans = false;
break;
}
cnt[pos] -= 2;
}
writeln(ans ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
unittest
{
assert( [ "ATCODER" ].parse.expand.solve == 3 );
assert( [ "HATAGAYA" ].parse.expand.solve == 5 );
assert( [ "SHINJUKU" ].parse.expand.solve == 0 );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
auto ans = 0L;
auto m = 0L;
foreach( c; s )
{
if( c == 'A' || c == 'C' || c == 'G' || c == 'T' )
{
m++;
if( ans < m ) ans = m;
}
else
{
m = 0;
}
}
return ans;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
enum lim = 80 * 80 * 2;
void main() {
int m1, d1;
scan(m1, d1);
int m2, d2;
scan(m2, d2);
writeln(m1 < m2 ? 1 : 0);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
auto w = ["Sunny", "Cloudy", "Rainy"];
string s;
scan(s);
auto i = w.countUntil(s);
writeln(w[(i + 1) % 3]);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main() {
problem();
}
void problem() {
auto K = scan!int;
auto S = scan;
string solve() {
if (S.length > K) return S[0..K] ~ "...";
return S;
}
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.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)); }
enum M = 360;
void main() {
try {
for (; ; ) {
const X = readInt();
const ans = M / gcd(X, M);
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm, std.math;
long calc(long x) {
return x * (x + 1) / 2;
}
void main() {
string s = readln.chomp;
if (s[0] == '>') s = '<' ~ s;
if (s[$-1] == '<') s ~= '>';
auto g = s.group.array;
long[] a = new long[g.length];
foreach (i, x; g) {
a[i] = x[1];
}
long ans;
for (auto i = 0; i < a.length; i += 2) {
if (a[i] < a[i+1]) {
ans += calc(a[i]-1) + calc(a[i+1]);
} else {
ans += calc(a[i]) + calc(a[i+1]-1);
}
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int n; readV(n);
string[] s; readM(n, s);
auto t = ['M', 'A', 'R', 'C', 'H'];
auto h = new long[](5);
foreach (si; s) {
auto i = t.countUntil(si[0]);
if (i >= 0) h[i]++;
}
auto calc(int a, int b, int c)
{
return h[a].to!long * h[b] * h[c];
}
auto r =
calc(0, 1, 2) + calc(0, 1, 3) + calc(0, 1, 4) + calc(0, 2, 3) + calc(0, 2, 4) +
calc(0, 3, 4) + calc(1, 2, 3) + calc(1, 2, 4) + calc(1, 3, 4) + calc(2, 3, 4);
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.array;
void main(){
auto ip = readln.chomp.to!(char[]);
replace(ip, ",", " ").writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
int N = readln.chomp.to!int;
int H = readln.chomp.to!int;
int W = readln.chomp.to!int;
int result = (N - H + 1) * (N - W + 1);
writeln(result);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto x = readln.chomp.split.to!(int[]);
if (x[0] * x[1] % 2) {
writeln("Odd");
} else {
writeln("Even");
}
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
auto N = readln.chomp.to!long;
long result;
foreach (n; 1 .. N + 1)
{
long cnt;
if (n % 2 == 0)
{
continue;
}
foreach (k; 1 .. n + 1)
if (n % k == 0)
{
cnt++;
}
if (cnt == 8)
{
result++;
}
}
result.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
// 差の絶対値
@nogc @safe pure T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
// 切り上げ除算
@nogc @safe pure T divCeil(T)(const ref T a, const ref T b) {
return (a+b-1)/b;
}
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
@nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
@nogc @safe pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) {
if (n < k) return 0;
ulong r = 1;
for (ulong i = n-k+1; i <= n; i++) {
r *= i;
r %= m;
}
return r;
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modFact(ulong n, ulong m) {
return modPerm(n, n, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
@nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) {
return modPerm(n, r, m)*modInv(modFact(r, m), m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong a;
string s;
readInto(a);
readInto(s);
if (a < 3200) writeln("red");
else writeln(s);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
bool[10^^5+1] PS;
void prime_init()
{
PS[] = true;
PS[0] = false;
PS[1] = false;
foreach (i; 2..10^^5+1) {
if (PS[i]) {
auto x = i*2;
while (x <= 10^^5) {
PS[x] = false;
x += i;
}
}
}
}
void main()
{
prime_init();
auto qs = new int[](10^^5+1);
foreach (i; 2..10^^5+1) if (PS[i] && PS[(i+1)/2]) qs[i] = 1;
foreach (i; 0..10^^5) qs[i+1] += qs[i];
auto Q = readln.chomp.to!int;
foreach (_; 0..Q) {
auto lr = readln.split.to!(int[]);
writeln(qs[lr[1]] - qs[lr[0]-1]);
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.math;
void main(){
auto input = readln().chomp.split.map!(to!int);
if(input[1] % input[0] == 0){
writeln(input[0] + input[1]);
}else{
writeln(input[1] - input[0]);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long L, R;
scan(L, R);
long ans = long.max;
foreach (i; L .. min(R + 1, L + 5000))
{
foreach (j; i + 1 .. min(R + 1, i + 5000))
{
ans = ans.min(i * j % 2019);
}
}
writeln(ans);
}
|
D
|
import std;
char get(char a, char b) {
foreach (c; "RGB") {
if (c != a && c != b) return c;
}
assert(false);
}
long calc(string s) {
int n = cast(int)s.length;
auto r = new int[n];
auto g = new int[n];
auto b = new int[n];
if (s[0] == 'R') r[0] = 1;
if (s[0] == 'G') g[0] = 1;
if (s[0] == 'B') b[0] = 1;
foreach (i; 1..n) {
r[i] = r[i-1] + (s[i] == 'R');
g[i] = g[i-1] + (s[i] == 'G');
b[i] = b[i-1] + (s[i] == 'B');
}
long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n - 1; j++) {
if (s[i] == s[j]) continue;
char c = get(s[i], s[j]);
// writeln("i ", i, " j ", j, " ", c);
int step = j - i;
if (c == 'R') {
ans += r[$-1] - r[j-1];
if (j + step < n && s[j + step] == 'R') ans--;
}
if (c == 'G') {
ans += g[$-1] - g[j-1];
if (j + step < n && s[j + step] == 'G') ans--;
}
if (c == 'B') {
ans += b[$-1] - b[j-1];
if (j + step < n && s[j + step] == 'B') ans--;
}
}
}
return ans;
}
void main() {
readint;
string s = read!string;
writeln(calc(s));
}
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.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
bool good = true;
foreach (i; 1..4)
{
if (S[i] == S[i-1])
good = false;
}
writeln(good ? "Good" : "Bad");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long[] as;
foreach(i; 0 .. 6) as ~= read.to!long;
// 同じ頂点が3回出ていたら不可
long[long] cnt;
foreach(a; as) if(a !in cnt) cnt[a] = 1; else cnt[a] += 1;
long max = 0;
foreach(c; cnt) if(c > max) max = c;
if(max == 3) writeln("NO");
else writeln("YES");
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long n, m, x;
scan(n, m, x);
auto 参考書の値段とアルゴリズム = new long[][](n);
// writeln(tmp);
foreach (i; 0 .. n)
{
参考書の値段とアルゴリズム[i] = aryread();
}
long func(long state)
{
auto アルゴリズムの合計 = new long[](m);
long 合計金額;
foreach (i; 0 .. n)
{
if ((state & (1 << i)) != 0)
{
合計金額 += 参考書の値段とアルゴリズム[i][0];
foreach (j; 1 .. m + 1)
{
アルゴリズムの合計[j - 1] += 参考書の値段とアルゴリズム[i][j];
}
}
}
foreach (k; 0 .. m)
{
if (アルゴリズムの合計[k] >= x)
{
continue;
}
else
{
return long.max;
}
}
return 合計金額;
}
long ans = long.max;
foreach (state; 0 .. 1 << n)
{
ans = min(func(state), ans);
}
if (ans == long.max)
{
writeln(-1);
return;
}
writeln(ans);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, q; rd(n, q);
int s=0;
while(q--){
int t, k; rd(t, k);
if(t){
(s+=k)%=n;
}else{
writeln(((k-1)+s)%n+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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto S = sread();
long ans;
foreach (i; 0 .. N - 2)
if (S[i .. i + 3] == "ABC")
{
ans++;
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
void main()
{
while (1) {
auto n = readln.chomp.to!int;
if (!n) break;
auto sum = readln.chomp.split.map!(to!int).reduce!("a+b");
writeln(sum / (n-1));
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto k = readNums!int;
auto start = k[0] * 60 + k[1];
auto end = k[2] * 60 + k[3];
writeln(end - start - k[4]);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
int[] N;
N.length = S.length;
int x;
foreach (i, c; S) {
if (c == 'R') {
++x;
} else {
N[i-1] += (x+1)/2;
N[i] += x/2;
x = 0;
}
}
x = 0;
foreach_reverse (i, c; S) {
if (c == 'L') {
++x;
} else {
N[i+1] += (x+1)/2;
N[i] += x/2;
x = 0;
}
}
writeln(N.to!(string[]).join(" "));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long[10^^5] HS, TABLE;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
foreach (i, h; readln.split.to!(long[])) HS[i] = h;
foreach_reverse (i; 0..N) {
if (i == N-1) continue;
auto c = long.max;
foreach (j; i+1..i+1+K) {
if (j == N) break;
c = min(c, TABLE[j] + abs(HS[i] - HS[j]));
}
TABLE[i] = c;
}
writeln(TABLE[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)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD;
auto cnt = new long[](N);
foreach (x; 1..N+1)
{
auto x2 = x^^2;
if (x2 > N) break;
foreach (y; 1..N+1)
{
auto y2 = y^^2;
if (x2 + y2 + x*y > N) break;
foreach (z; 1..N)
{
auto z2 = z^^2;
auto a = x2 + y2 + z2 + x*y + y*z + z*x;
if (a > N) break;
++cnt[a-1];
}
}
}
foreach (i; 0..N)
{
writeln(cnt[i]);
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
//auto s = new string[](n);
int ab, a, b;
int res;
foreach (i; 0..n) {
immutable s = readln.chomp;
res += s.count("AB");
if (s[0] == 'B' && s[$-1] == 'A') {
ab++;
} else if (s[0] == 'B') {
b++;
} else if (s[$-1] == 'A') {
a++;
}
}
if (ab > 0) {
res += ab - 1;
if (b > 0) {
res++;
b--;
}
if (a > 0) {
res++;
a--;
}
}
res += min(a, b);
res.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
int n, k;
scan(n, k);
auto a = readln.split.to!(int[]);
auto presum = new long[](n + 1);
auto possum = new long[](n + 1);
foreach (i ; 1 .. n + 1) {
presum[i] = a[i-1] + presum[i-1];
possum[i] = max(a[i-1], 0) + possum[i-1];
}
long ans;
foreach (i ; 0 .. n - k + 1) {
long t = possum[n] - (possum[i + k] - possum[i]) + max(0, presum[i + k] - presum[i]);
ans = max(ans, t);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, m;
scan(n, m);
auto adj = new int[][](n, 0);
auto iri = new int[](n);
auto de = new int[](n);
foreach (i ; 0 .. n - 1 + m) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
adj[ai] ~= bi;
de[ai]++;
iri[bi]++;
}
int root = -1;
foreach (i ; 0 .. n) {
if (iri[i] == 0) {
root = i;
break;
}
}
debug {
writeln("iri", iri);
writeln("de", de);
}
auto p = new int[](n);
p[] = -1;
void dfs(int v, int par) {
if (p[v] == -1) p[v] = par + 1;
foreach (u ; adj[v]) {
if (u == par) continue;
iri[u]--;
}
foreach (u ; adj[v]) {
if (u == par) continue;
if (p[u] != -1) continue;
debug {
writefln("v: %d, u:%d, iri: %d", v, u, iri[u]);
}
if (iri[u] == 0) {
dfs(u, v);
}
}
}
dfs(root, -1);
foreach (i ; 0 .. n) {
writeln(p[i]);
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
if (n == 3 || n == 5 || n == 7)
writeln("YES");
else
writeln("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;
import std.numeric;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
// }}}
ulong lcm(ulong a, ulong b)
{
return a / gcd(a, b) * b;
}
void main()
{
auto cin = new Scanner;
int n;
cin.scan(n);
auto a = cin.nextArray!ulong(n);
ulong res;
foreach (e; a)
{
res += e - 1;
}
res.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main(){
stdin.readln.chomp.filter!(a => a != ',' && a != '.').array.split.filter!(a => a.length > 2 && a.length < 7).reduce!((a, b) => a ~ " " ~ b).writeln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
immutable int mod = 10 ^^ 9 + 7;
immutable int limit = 60;
immutable auto p2 = limit.iota.map !(i => (1L << i) % mod).array.idup;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(long)).array;
auto c = new int [limit];
foreach (j; 0..n)
{
foreach (d; 0..limit)
{
if ((a[j] >> d) & 1)
{
c[d] += 1;
}
}
}
int res = 0;
foreach (j; 0..n)
{
int lo = 0;
int hi = 0;
foreach (d; 0..limit)
{
if ((a[j] >> d) & 1)
{
lo = (lo + p2[d] * 1L * c[d]) % mod;
hi = (hi + p2[d] * 1L * n) % mod;
}
else
{
lo = (lo + p2[d] * 1L * 0) % mod;
hi = (hi + p2[d] * 1L * c[d]) % mod;
}
}
res = (res + lo * 1L * hi) % mod;
}
writeln (res);
}
}
|
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;
import core.bitop;
void main()
{
int a, b;
readln.chomp.split.tie(a, b);
int ite = 0;
for (int i = 1; i <= 999; ++i) {
if (b - a == i && ite - a > 0) {
debug verbose(i, ite);
writeln = ite - a;
break;
}
ite += i;
}
}
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.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
long N;
scan(N);
if (N % 2) {
writeln(0);
return;
}
long d = 10;
long ans;
while (d <= N) {
ans += N / d;
d *= 5;
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import 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 s = sread();
long result = long.max;
foreach (i; 0 .. s.length - 2)
{
long n = s[i .. i + 3].to!long();
// writeln(" ", n);
result.minAssign(abs(n - 753));
}
writeln(result);
}
|
D
|
import std.stdio;
void main()
{
auto r1 = readln();
auto r2 = readln();
auto r3 = readln();
write(r1[0], r2[1], r3[2]);
}
|
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;
while (true)
{
int n, p;
cin.scan(n, p);
if (n == 0 && p == 0) break;
int[] ar = new int[n];
int sum;
for (int i = 0; i < n; i = (i + 1) % n)
{
if (p == 0)
{
p = ar[i];
sum -= ar[i];
ar[i] = 0;
}
else
{
++sum;
++ar[i];
--p;
if (p == 0 && sum == ar[i])
{
writeln(i);
break;
}
}
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int a, b; rd(a, b);
for(int c=1; c<=3; c++){
if((a*b*c)&1){
writeln("Yes");
return;
}
}
writeln("No");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
void main()
{
string n = readln.chomp;
n.count('2').writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int N = readln.chomp.to!int;
int[][] CSF = (N-1).iota.map!(_ => readln.chomp.split.to!(int[])).array;
int[] C, S, F;
foreach (csf; CSF) {
C ~= csf[0];
S ~= csf[1];
F ~= csf[2];
}
foreach (i; 0..N) {
int x;
for (ulong j = i; j < N - 1; j++) {
if (x < S[j]) {
x = S[j];
}
if (x % F[j] != 0) {
x = x+F[j] - x%F[j];
}
x += C[j];
}
writeln(x);
}
}
|
D
|
import std.string;
import std.conv;
import std.algorithm;
import std.stdio;
void main() {
int hp = readln.chomp.split(" ")[0].to!int;
foreach (e;readln.chomp.split(" ").map!(to!int)) {
hp -= e;
if (hp <= 0) {
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main()
{
string s = readln.chomp;
int w = readln.chomp.to!int;
for (int i = 0; i < s.length; i++) {
if (!(i % w)) write(s[i]);
}
}
|
D
|
import std;
void main() {
int n, m; scan(n, m);
auto a = readints;
int ans = n - a.sum;
writeln(max(ans, -1));
}
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;
alias P = Tuple!(long, "x", long, "y");
const MOD = 10^^9 + 7;
long calc(P[] ps, int zeros) {
long[P] m;
foreach (p; ps) m[p]++;
bool[P] used;
long[] group;
foreach (p; ps) {
if (used.get(p, false)) continue;
// p に属するグループ数
auto a1 = p;
auto a2 = P(-p.x, -p.y);
long acnt = m.get(a1, 0) + m.get(a2, 0);
// p と垂直な位置関係にある点のグループ数
auto b1 = P(-p.y, p.x);
auto b2 = P(p.y, -p.x);
long bcnt = m.get(b1, 0) + m.get(b2, 0);
long cnt = 0;
// s のグループからのみ選ぶ組み合わせ数
cnt += modpow(2, acnt) - 1; // -1 は何も選ばない分を引いている
// t のグループからのみ選ぶ組み合わせ数
cnt += modpow(2, bcnt) - 1;
// s からも t からも選ばない
cnt += 1;
cnt %= MOD;
group ~= cnt;
used[a1] = used[a2] = used[b1] = used[b2] = true;
}
long ans = 0;
if (group.length > 0) {
ans += group.fold!((a, b) => a * b % MOD)(1L);
ans -= 1; // 全てのグループにおいて何も選ばなかった分の 1 を引く
}
ans += zeros;
return (ans + MOD) % MOD;
}
void main() {
int n; scan(n);
P[] ps;
int zeros = 0;
foreach (_; 0..n) {
long a, b; scan(a, b);
if (a == 0 && b == 0) zeros++;
else {
long g = gcd(abs(a), abs(b));
ps ~= P(a/g, b/g);
}
}
writeln(calc(ps, zeros));
}
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;
long modpow(long x, long k) {
if (k == 0) return 1;
if (k % 2 == 0) return modpow(x * x % MOD, k / 2);
return x * modpow(x, k - 1) % MOD;
}
|
D
|
import std.stdio;
void main() {
for (int i = 0; i < 1000; i++) {
writeln("Hello World");
}
}
|
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 scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
long[string] d;
foreach (i; 0 .. N)
{
d[sread()] = 0;
}
writeln(d.keys.length);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
import std.math;
void main() {
int n;
scan(n);
auto s = readln.split;
foreach (ch ; s) {
if (ch == "Y") {
writeln("Four");
return;
}
}
writeln("Three");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
long mod = 1000000007;
long modinv(long a) {
long b=mod, u=1, v=0;
while(b>0) {
long t=a/b;
a -= t*b; swap(a,b);
u -= t*v; swap(u,v);
}
u %= mod;
if(u<0) u+=mod;
return u;
}
void main() {
long x = readln.chomp.to!long;
long n = 100;
int count = 0;
while(n<x) {
n = n + n/100;
count++;
}
writeln(count);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
string s;
while ((s = readln.chomp) != null) {
auto ab = s.split.map!(to!int).array;
int a = ab[0], b = ab[1];
writeln(gcd(a, b));
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.array;
import std.string;
import std.math;
void main(string[] args) {
readln.chomp.solve.writeln;
}
auto solve(string input) {
return input.count("1");
}
unittest {
assert(solve("101") == 2);
assert(solve("000") == 0);
assert(solve("0001") == 1);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
auto readNum(T)(){
return readln.chomp.to!T;
}
auto readNums(T)(){
return readln.chomp.split.to!(T[]);
}
void main(){
auto nm = readNums!int;
int maxGate = nm[0]+1, minGate = 0;
foreach(i; 0 .. nm[1]){
auto g = readNums!int;
minGate = max(minGate, g[0]);
maxGate = min(maxGate, g[1]);
}
int ret = maxGate - minGate + 1;
writeln(ret >= 0 ? ret : 0);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, k; rd(n, k);
auto cost=new int[][](k, k);
foreach(i; 0..k) cost[i]=readln.split.to!(int[]);
auto a=new int[][](n, n);
foreach(i; 0..n){
a[i]=readln.split.to!(int[]);
a[i].each!((ref e)=>e--);
}
auto s=new int[][](k, n*2);
foreach(c; 0..k)for(int i0=0; i0<=((n-1)*2); i0++){
for(int j=max(0, i0-n+1), i=min(i0, n-1); i>=0 && j<n; i--, j++){
s[c][i0]+=cost[a[i][j]][c];
}
}
int mn=1_000_000_000;
foreach(c1; 0..k)foreach(c2; 0..k)foreach(c3; 0..k){
if((c1!=c2 && c2!=c3 && c3!=c1)==false) continue;
int beta=0;
for(int i=0; i<=(n-1)*2; i++){
if(i%3==0){
beta+=s[c1][i];
}else if(i%3==1){
beta+=s[c2][i];
}else{
beta+=s[c3][i];
}
}
mn=min(mn, beta);
}
writeln(mn);
}
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.