code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int parse(string s)
{
int r;
if (s[0] == '0') {
r += s[1] - '0';
} else {
r += s[0..2].to!int;
}
r *= 60;
if (s[3] == '0') {
r += s[4] - '0';
} else {
r += s[3..5].to!int;
}
r *= 60;
if (s[6] == '0') {
r += s[7] - '0';
} else {
r += s[6..8].to!int;
}
return r;
}
void main()
{
for (;;) {
auto N = readln.chomp.to!int;
if (N == 0) return;
auto tt = new int[](60*60*24);
foreach (_; 0..N) {
auto t = readln.split;
++tt[parse(t[0])];
--tt[parse(t[1])];
}
int max_t = tt[0];
foreach (i; 1..tt.length) {
tt[i] += tt[i-1];
max_t = max(max_t, tt[i]);
}
writeln(max_t);
}
}
|
D
|
void main()
{
string s = rdStr;
s = s[0..$-2];
long len = s.length >> 1;
while (s[0..len] != s[len..$])
{
s = s[0..$-2];
--len;
}
s.length.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
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;
// dfmt on
void main()
{
long H, W;
scan(H, W);
auto cnt = new long[](26);
foreach (_; 0 .. H)
{
auto A = sread();
foreach (a; A)
{
cnt[a - 'a']++;
}
}
auto pool = () {
if (H & 1 && W & 1)
return [0, 1, (W - 1) / 2 + (H - 1) / 2, 0, ((W - 1) / 2) * ((H - 1) / 2)];
if (H & 1)
return [0, 0, W / 2, 0, (W / 2) * ((H - 1) / 2)];
if (W & 1)
return [0, 0, H / 2, 0, (H / 2) * ((W - 1) / 2)];
return [0, 0, 0, 0, (W / 2) * (H / 2)];
}();
foreach_reverse (x; 0 .. pool.length)
{
foreach (i; 0 .. cnt.length)
{
// dprint(cast(char)(i + 'a'), cnt[i], pool);
if (0 < pool[x] && x <= cnt[i])
{
while (0 < pool[x] && x <= cnt[i])
{
cnt[i] -= x;
pool[x]--;
}
}
}
// dprint(cast(char)(i + 'a'), cnt[i], pool);
if (pool[x] != 0)
{
writeln("No");
return;
}
}
writeln(cnt.all!"a==0" ? "Yes" : "No");
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string s;
sc.scan(s);
int res = 0;
foreach (i; 0 .. s.length) {
int tmp;
foreach (j; i .. s.length) {
if (s[j] == 'A' || s[j] == 'C' || s[j] == 'G' || s[j] == 'T')
tmp++;
else
break;
}
if (res < tmp)
res = tmp;
}
writeln(res);
}
|
D
|
import std.stdio, std.range, std.random, std.conv, std.string, std.math;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting;
void main()
{
int[] input = readln().strip.split().to!(int[]);
int N = input[0];
int K = input[1];
if((N+1)/2 >= K)
{
writeln("YES");
}else
writeln("NO");
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const m1 = readInt();
const d1 = readInt();
const m2 = readInt();
const d2 = readInt();
writeln((m1 != m2) ? 1 : 0);
}
} catch (EOFException e) {
}
}
|
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 = lread();
N %= 10;
writeln(N == 3 ? "bon" : (N.among(2, 4, 5, 7, 9) ? "hon" : "pon"));
}
|
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 k=scan!size_t;
auto s=scan!string;
(s.length<=k?s:s[0..k]~"...").writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math; // math functions
void main()
{
auto rd = readln.split.to!(long[]), x = rd[0], y = rd[1];
writeln((x-y).abs <= 1 ? "Brown" : "Alice");
}
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string n = chomp(readln());
int a = to!int(n);
if(a%10 == a/100){
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto S = RD!string;
auto T = RD!string;
auto A = RD;
auto B = RD;
auto U = RD!string;
if (S == U)
writeln(A-1, " ", B);
else
writeln(A, " ", B-1);
stdout.flush;
debug readln;
}
|
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;
long calc(long n, long k) {
if (k == 0) return n * n;
long ans = 0;
for (int b = 1; b <= n; b++) {
// N = pb + r
long p = n / b;
long r = n % b;
long x = max(b - k, 0) * p + max(r - k + 1, 0);
ans += x;
}
return ans;
}
void main() {
auto nk = readints;
int n = nk[0], k = nk[1];
writeln(calc(n, k));
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto N = readln.split.to!(int[]), A=N[0], B=N[1], C=N[2];
if((A<C&&C<B)||(B<C&&C<A)){
writeln("Yes");
}else{
writeln("No");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
T[][] comb(T)(in T[] arr, in int k) pure nothrow {
if (k == 0) return [[]];
typeof(return) result;
foreach (immutable i, immutable x; arr)
foreach (suffix; arr[i + 1 .. $].comb(k - 1))
result ~= x ~ suffix;
return result;
}
int N;
string[10^^5] SS;
void main()
{
N = readln.chomp.to!int;
foreach (i; 0..N) SS[i] = readln.chomp;
long[5] march;
foreach (s; SS[0..N]) {
switch (s[0]) {
case 'M': ++march[0]; break;
case 'A': ++march[1]; break;
case 'R': ++march[2]; break;
case 'C': ++march[3]; break;
case 'H': ++march[4]; break;
default:
}
}
auto cb = [0, 1, 2, 3, 4].comb(3);
long ret;
foreach (c; cb) ret += march[c[0]] * march[c[1]] * march[c[2]];
writeln(ret);
}
|
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;
long calc(int a, int b, int c) {
long ans = 0;
// 毒入り(美味しい) -> 解毒剤(美味しい)の順に食べる
int x = min(b, c);
ans += x * 2;
b -= x;
c -= x;
// 毒入り(美味しい) -> 解毒剤(美味しくない)の順に食べる
x = min(a, c);
ans += x;
a -= x;
c -= x;
// 毒入り(美味しい)が余っているなら 1 枚だけ食べる
if (c > 0) ans++;
// 余りの解毒剤(美味しい)を食べる
ans += b;
return ans;
}
void main() {
auto abc = readints;
int a = abc[0], b = abc[1], c = abc[2];
writeln(calc(a, b, c));
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv;
void main() {
auto a = map!(x => x.to!int)(readln.chomp.split);
if (a[0] + a[1] == a[2] + a[3]) {
writeln("Balanced");
} else if (a[0] + a[1] < a[2] + a[3]) {
writeln("Right");
} else {
writeln("Left");
}
}
|
D
|
import std.stdio, std.conv, std.array,std.string,std.algorithm;
void main()
{
auto n=readln.chomp.to!int,a=readln.split;
int ma=a[0].to!int,mi=a[0].to!int;
for(int i;i<n;i++){
ma=max(ma,a[i].to!int);
mi=min(mi,a[i].to!int);
}
writeln(ma-mi);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto o = readln.chomp, no = o.length;
auto e = readln.chomp, ne = e.length;
auto r = new char[](no+ne);
foreach (i, c; o) r[i*2] = c;
foreach (i, c; e) r[i*2+1] = c;
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nhlr = readln.split.to!(int[]);
auto N = nhlr[0];
auto H = nhlr[1];
auto L = nhlr[2];
auto R = nhlr[3];
auto as = readln.split.to!(int[]);
auto DP = new int[][](N, H);
foreach (ref dp; DP) dp[] = -1;
int solve(int i, int h) {
if (i == N) return 0;
if (DP[i][h] == -1) {
auto r1 = (h + as[i]) % H;
auto r2 = (h + as[i] - 1) % H;
DP[i][h] = max(
solve(i+1, r1) + (L <= r1 && r1 <= R ? 1 : 0),
solve(i+1, r2) + (L <= r2 && r2 <= R ? 1 : 0)
);
}
return DP[i][h];
}
writeln(solve(0, 0));
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
bool[int] isPrime;
void init_isPrime() {
foreach (i; 1..10^^5+1) {
isPrime[i] = true;
}
foreach (i; 2..100001) {
if (isPrime[i]) {
for (int j = i + i; j <= 100000; j += i){
isPrime[j] = false;
}
}
}
}
bool isLike2017(int n) {
return isPrime[n] && isPrime[(n+1)/2];
}
void main() {
int Q = readln.chomp.to!int;
bool[int] like2017;
int[][] lrs = Q.iota.map!(_ => readln.chomp.split.to!(int[])).array;
int[100001] dp;
init_isPrime();
for (int i = 3; i <= 100000; i += 2) {
if (isLike2017(i)) {
dp[i]++;
}
}
for (int i = 3; i <= 100000; i++) {
dp[i] += dp[i-1];
}
foreach (lr; lrs) {
int l = lr[0],
r = lr[1];
writeln(dp[r] - dp[l-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, std.format, std.bitmanip;
// }}}
// 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;
}
}
// }}}
void main() {
auto cin = new Scanner;
int n;
string s;
cin.scan(n, s);
long[] r = new long[2 * n], g = new long[2 * n], b = new long[2 * n];
foreach (i; 0 .. n) {
if (s[i] == 'R') {
r[i] = 1;
} else if (s[i] == 'G') {
g[i] = 1;
} else if (s[i] == 'B') {
b[i] = 1;
}
}
foreach (i; 1 .. 2 * n) {
r[i] += r[i - 1];
g[i] += g[i - 1];
b[i] += b[i - 1];
}
long res;
alias T = Tuple!(uint, "f", uint, "s", string, "str");
T[] ar;
string[] per = ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"];
foreach (e; per) {
foreach (i; 0 .. n - 1) {
foreach (j; i + 1 .. n - 1) {
if (s[i] == e[0] && s[j] == e[1]) {
ar ~= T(i, j, e);
}
}
}
}
debug writeln(r);
debug writeln(g);
debug writeln(b);
foreach (e; ar) {
uint l = e.s + (e.s - e.f);
auto sel = e.str[2] == 'R' ? r : e.str[2] == 'G' ? g : b;
res += sel[l - 1] - sel[e.s];
res += sel[n - 1] - sel[l];
debug writeln(e.str, " ", e, " ", sel[l - 1] - sel[e.s], " ", sel[n - 1] - sel[l]);
}
writeln(res);
}
|
D
|
void main()
{
long[] tmp = readln.split.to!(long[]);
long k = tmp[0], t = tmp[1];
long[] a = readln.split.to!(long[]);
long day = 2 * a.reduce!max - a.sum - 1;
writeln(day > 0 ? day : 0);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.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;
foreach(i;0..3)
{
if(S[i]==S[i+1]){
writeln("Bad");
return;
}
}
writeln("Good");
return;
}
struct Vec2{
long x,y;
alias a=x, b=y;
}
struct Vec3{
long x,y,z;
alias a=x, b=y, c=z;
}
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 scanElem!string;
}
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() {
auto X = rs;
string res = X[0..1];
foreach(i; X[1..$]) {
if(res.length == 0) { res ~= i; continue; }
auto p = res.back;
if(p == 'S' && i == 'T') res.popBack;
else res ~= i;
}
res.length.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool isReachable(string s, int fm, int to) {
assert(s[fm] == '.' && s[to] == '.');
int p = fm;
while (p < to) {
if (p + 2 <= to && s[p + 2] == '.') p += 2;
else if (s[p + 1] == '.') p++;
else return false;
}
return true;
}
bool findJumpPoint(string s, int lo, int hi) {
int n = cast(int)s.length;
for (int i = lo; i <= hi; i++) {
if (i + 2 >= n) break;
if (s[i..i+3] == "...") return true;
}
return false;
}
bool calc(string s, int a, int b, int c, int d) {
int n = cast(int)s.length;
bool reachOk = isReachable(s, a, c) && isReachable(s, b, d);
// a, b の移動は独立している
if (c < d) return reachOk;
// a は b を追い越す必要がある
return reachOk && findJumpPoint(s, b-1, d-1);
}
void main() {
int n, a, b, c, d; scan(n, a, b, c, d);
string s = read!string;
writeln(calc(s, a-1, b-1, c-1, d-1) ? "Yes" : "No");
}
|
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);}}
enum MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long H1, M1, H2, M2, K;
scan(H1, M1, H2, M2, K);
long L1 = H1 * 60 + M1;
long L2 = H2 * 60 + M2;
// dprint(H1, M1, H2, M2, K, L1, L2);
writeln(max(0, L2 - L1 - K));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main() {
int s = readln.chomp.to!int;
writeln(s / 3600, ":", s / 60 % 60, ":", s % 60);
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
//Kadane's algorithm
long maximumSubArraySum(R) (R range) if (isInputRange!R && isIntegral!(ElementType!R)) {
alias T = ElementType!R;
alias S = Tuple! (long, "best", long, "cur");
S next (in S s, in T x) {
S t;
t.cur = s.cur + x;
t.best = max (s.best, t.cur);
if (t.cur < 0) t.cur = 0;
return t;
}
return reduce!next (tuple!("best", "cur") (long.min, 0L), range).best;
}
bool test (in long[] a) {
long y = sum (a);
long best = y;
long cur = 0;
int l = 0;
foreach (x; a) {
cur += x;
++l;
if (best <= cur && l < a.length) {
return false;
}
if (cur < 0) {
cur = 0;
l = 0;
}
}
return true;
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
immutable n = r.next!uint ();
auto a = r.nextA!(long)(n);
debug stderr.writeln (a);
auto b = a.dup;
b.reverse ();
writeln ((test (a) && test (b)) ? "YES" : "NO");
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable inf = 10^^9 + 7;
void main() {
int n;
scan(n);
long ans = 1;
foreach (k ; 1 .. 30) {
long bn = 1L * (2L^^k - 1) * (2L^^(k - 1));
if (k > bn) break;
if (n % bn == 0) {
ans = max(ans, bn);
}
}
writeln(ans);
}
struct UnionFind {
private {
int N;
int[] p;
int[] rank;
}
this (int n) {
N = n;
p = iota(N).array;
rank = new int[](N);
}
int find_root(int x) {
if (p[x] != x) {
p[x] = find_root(p[x]);
}
return p[x];
}
bool same(int x, int y) {
return find_root(x) == find_root(y);
}
void unite(int x, int y) {
int u = find_root(x), v = find_root(y);
if (u == v) return;
if (rank[u] < rank[v]) {
p[u] = v;
}
else {
p[v] = u;
if (rank[u] == rank[v]) {
rank[u]++;
}
}
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this(int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head = (head + 1) % N;
}
void clear() {
head = tail = 0;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
void main()
{
foreach (line; stdin.byLine) {
string str = line.to!string.chomp;
foreach (i; 0..26) {
if (str.caesar(i).split.count!(s => s == "the" || s == "this" || s == "that") > 0) {
str = str.caesar(i);
break;
}
}
str.writeln;
}
}
string caesar(string str, int n)
{
string ret = "";
foreach (c; str) {
if (c == '.' || c == ' ') ret ~= c;
else if (c + n >= 'a' && c + n <= 'z') ret ~= c + n;
else ret ~= 'a' + c + n - 1 - 'z';
}
return ret;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto A = readln.split.map!(to!long).array;
auto dp = new long[](N);
fill(dp, 1L << 59);
dp[0] = 0;
foreach (i; 1..N) {
foreach (j; max(0, i - K)..i) {
dp[i] = min(dp[i], dp[j] + abs(A[i] - A[j]));
}
}
dp[N-1].writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
bool read(ref string str){
str=readln.chomp();
if(str==null)return false;
return true;
}
void main(){
string str;
while(read(str)){
string[] s=str.split(" ");
auto n1=s[0].to!int();
auto n2=s[1].to!int();
writeln(log10(n1+n2).to!int+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;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias TOWN = Tuple!(long, "x", long, "y");
void main()
{
auto n = lread();
auto s = sread();
foreach(e; s)
{
auto next = (e - 'A' + n) % 26;
write(('A' + next).to!(char));
}
writeln();
}
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;
}
}
long product_MOD(long n)
{
long p_MOD = 1;
while (n > 0)
{
p_MOD *= n--;
p_MOD %= MOD;
}
return p_MOD;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto q = RD!int;
auto ans = new bool[](q);
foreach (i; 0..q)
{
auto n = RD!int;
auto p = RDA(-1);
bool ok = true;
foreach (j; 1..n)
{
if ((p[j-1]+1)%n != p[j])
{
ok = false;
break;
}
}
if (!ok)
{
ok = true;
foreach (j; 1..n)
{
if (p[j-1] != (p[j]+1)%n)
{
ok = false;
break;
}
}
}
ans[i] = ok;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto x = RDA;
auto cnt = new int[](60);
foreach (j; 0..60)
{
auto bit = 1L << j;
foreach (i; 0..n)
{
if (x[i] & bit)
++cnt[j];
}
}
foreach (j; 0..n)
{
long rem;
foreach (i; 0..60)
{
auto bit = 1L << i;
auto bitm = bit % mod;
if (x[j] & bit)
{
bitm.modm(n);
rem.moda(bitm);
}
else
{
bitm.modm(cnt[i]);
rem.moda(bitm);
}
}
long tmp;
foreach (i; 0..60)
{
auto bit = 1L << i;
if (x[j] & bit)
{
long c = cnt[i];
c.modm(bit%mod);
tmp.moda(c);
}
}
tmp.modm(rem);
ans[ti].moda(tmp);
}
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
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!(wchar[]);
int r = int.max;
foreach (i; 0..S.length-2) {
r = min(r, abs(753 - S[i..i+3].to!int));
}
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; }
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;
long ans;
foreach (i; 0..N)
{
auto l = RD;
auto r = RD;
ans += r - l + 1;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n, m;
scan(n, m);
long t = 100 * (n - m) + 1900 * m;
long ans = 2L^^m * t;
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.functional;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
pure
X genericPower(alias mul, X, Y) (X x, Y y, X one = 1.to!X)
if (isUnsigned!Y) {
X a = one, b = x;
while (y > 0) {
if (y & 1) {
a = binaryFun!mul (a, b);
}
b = binaryFun!mul (b, b);
y >>>= 1;
}
return a;
}
int test (uint[] a, const int m) {
sort (a);
a[] %= m;
auto d = new ulong[m];
auto x = new int[m];
foreach (f, i; a) {
if (f > 0) {
int k = i;
foreach (j; 0 .. m) {
d[k] += x[j];
if (--k < 0) k += m;
}
}
++x[i];
}
if (d[0] > 0) return 0;
debug stderr.writeln (d);
int mul (int i, int j) {
return (i * j) % m;
}
int res = 1;
foreach (k; 1 .. m) {
res = mul (res, genericPower!(mul, int, ulong)(k, d[k]));
}
return res;
}
void main() {
auto r = new InputReader ();
//immutable nt = r.next!uint ();
const n = r.next!uint ();
const m = r.next!uint ();
auto a = r.nextA!uint (n);
writeln (test (a, m));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias A = Tuple!(long, "i", long, "j");
void main()
{
auto hwd = readln.split.to!(int[]);
auto H = hwd[0];
auto W = hwd[1];
auto D = hwd[2];
auto AS = new A[](H*W+1);
foreach (long i; 0..H) {
foreach (j, a; readln.split.to!(int[])) {
AS[a] = A(i, j.to!long);
}
}
auto BS = new long[](H*W+1);
foreach (x; 1..D+1) {
while (x+D <= H*W) {
auto p = AS[x];
auto q = AS[x+D];
BS[x+D] = BS[x] + abs(p.i - q.i) + abs(p.j - q.j);
x += D;
}
}
auto Q = readln.chomp.to!int;
foreach (_; 0..Q) {
auto lr = readln.split.to!(int[]);
writeln(BS[lr[1]] - BS[lr[0]]);
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
void main()
{
auto hw = readln.chomp.split.map!(to!int);
auto field = new string[](hw[0]);
foreach (i; 0..hw[0]) {
field[i] = readln.chomp;
}
int cnt;
foreach (y; 0..hw[0]-1) {
foreach (x; 0..hw[1]-1) {
bool f, a, c, e;
foreach (i; 0..2) {
foreach (j;0..2) {
if (field[y+i][x+j] == 'f') f = 1;
if (field[y+i][x+j] == 'a') a = 1;
if (field[y+i][x+j] == 'c') c = 1;
if (field[y+i][x+j] == 'e') e = 1;
}
}
if (f + a + c + e == 4) cnt++;
}
}
cnt.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
long[] as = readln.split.to!(long[]);
long I = as[0];
long O = as[1];
long J = as[3];
long L = as[4];
writeln(
O + max(
I*J*L==0 ? 0 : 3 + (I-1)/2*2 + (J-1)/2*2 + (L-1)/2*2,
I/2*2 + J/2*2 + L/2*2
)
);
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.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 N = readln.chomp.to!int;
auto A = N.iota.map!(_ => readln.chomp.to!int).array;
auto S = new int[](100001);
foreach (i; 0..100001)
S[i] = int.max;
S[0] = -1;
S[1] = A[0];
auto SS = S.assumeSorted;
int ans = 1;
foreach (i; 1..N) {
auto lb = SS.lowerBound(A[i]).length.to!int;
ans = max(ans, lb);
if (S[lb] > A[i])
S[lb] = A[i];
}
ans.writeln;
}
|
D
|
import std.stdio;
int main(string[] argv)
{
for (int i = 0; i < 1000; ++i)
writeln("Hello World");
return 0;
}
|
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;
auto dx = readints;
int d = dx[0], x = dx[1];
int[] a;
for (int i = 0; i < n; i++) {
a ~= readint;
}
int ans = x + a.map!(e => (d - 1) / e + 1).sum;
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() {
auto rgb = readints;
int x = 100 * rgb[0] + 10 * rgb[1] + rgb[2];
writeln(x % 4 == 0 ? "YES" : "NO");
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void solve(){
long u = scan;
long v = scan;
long y = v * v;
long x = - (u * u);
writeln(x, " ", y);
}
void main(){
long tests = scan; // Toggle!
while(tests--) solve;
}
/************** ***** That's All Folks! ***** **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
void main() {
auto X = readAs!long;
ulong cnt;
long tmp = 100;
while(X > tmp) {
tmp *= 1.01;
cnt++;
}
cnt.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto M = RD;
long m = -1;
if (N % 2 == 0)
m = N / 2;
long l = 1, r = N;
foreach (i; 0..M)
{
debug writeln(r-l, ":", N-(r-l) - (r-l));
if (m != -1)
{
if (m == r-l || (N-(r-l) - (r-l) == 2))
{
++l;
}
}
writeln(l, " ", r);
++l;
--r;
}
stdout.flush;
debug readln;
}
|
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 i = readln.chomp;
if (i[0] != 'A')
{
"WA".writeln;
return;
}
if (i[1].isUpper || i[$ - 1].isUpper)
{
"WA".writeln;
return;
}
long cntc;
foreach (c; i[2 .. $ - 1])
{
if (c == 'C')
{
cntc++;
}
if (c.isUpper && c != 'C')
{
"WA".writeln;
return;
}
}
if (cntc != 1)
{
"WA".writeln;
return;
}
"AC".writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
T gcd(T)(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
T lcm(T)(T a, T b) {
return a * (b / gcd(a, b));
}
long numDivisors(long n, long k) {
return n / k;
}
long calc(long a, long b, long c, long d) {
long x = numDivisors(b, c) + numDivisors(b, d) - numDivisors(b, lcm(c, d));
long y = numDivisors(a-1, c) + numDivisors(a-1, d) - numDivisors(a-1, lcm(c, d));
return (b - a + 1) - (x - y);
}
void main() {
long a, b, c, d; scan(a, b, c, d);
writeln(calc(a, b, c, d));
}
|
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;
alias Rect = Tuple!(int, "left", int, "right");
int calc(Rect a, Rect b) {
if (a.left > b.left) return calc(b, a);
return max(0, b.left - a.right);
}
void main() {
auto wab = readints;
int w = wab[0], a = wab[1], b = wab[2];
auto r1 = Rect(a, a + w);
auto r2 = Rect(b, b + w);
writeln(calc(r1, r2));
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm;
void main()
{
int b=0;
while(1){
auto a = readln.split.map!(to!int);
if(a[0]==0) break;
if(b) writeln();
int flag=0;
for(uint i=a[0];i<=a[1];++i){
if(i%4==0){
if(i%100==0){
if(i%400==0){
flag=1;
i.writeln();
}
}else{
flag=1;
i.writeln();
}
}
}
if(!flag) writeln("NA");
b=1;
}
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.conv;
void main()
{
int t;
scanf("%d", &t);
getchar();
foreach(_; 0..t)
{
long a,b,c;
scanf("%lld %lld %lld\n", &a, &b, &c);
writeln(max(0, max(b,c) - a + 1), ' ', max(0, max(a,c) - b + 1), ' ', max(0, max(a,b) - c + 1));
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.array;
void main(){
auto a=readln.chomp;
if(a=="oxx"||a=="xox"||a=="xxo")writeln(700+100);
else if(a=="oox"||a=="oxo"||a=="xoo")writeln(700+200);
else if(a=="ooo")writeln(700+300);
else writeln(700);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto w = readln.chomp;
auto a = new int[](26);
foreach (c; w) ++a[cast(int)(c - 'a')];
writeln(a.all!(ai => ai % 2 == 0) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a, b, t;
scan(a, b, t);
writeln(t / a * b);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import 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[2][] es;
if(n % 2 == 0){
foreach(i; 1 .. n + 1) foreach(j; i + 1 .. n + 1){
if(i + j != n + 1) es ~= [i, j];
}
}
else{
foreach(i; 1 .. n) foreach(j; i + 1 .. n){
if(i + j != n) es ~= [i, j];
}
foreach(i; 1 .. n) es ~= [i, n];
}
es.length.writeln;
foreach(e; es) writeln(e[0], " ", e[1]);
}
/*
Nが偶数(たとえば6)のときは
→たして7になるもの同士をペアにして、自分のペア相手以外の全てと手をつなぐ
Nが奇数(たとえば7)のときは
→減らして6で考え、上記の通りにした上で、最後に7は自分以外の全てと手をつなぐ
*/
|
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(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = scan!int;
long[] as = scan!long(n);
long tmp = as[0];
foreach(a; as) tmp = gcd(tmp, a);
if(tmp > 1){
"not coprime".writeln;
return;
}
long[] ps = primesTo(1_000_009);
bool[] us = new bool[1_000_009];
foreach(a; as){
foreach(p; ps) if(a % p == 0){
long q = a / p;
if(us[p] || us[q]){
"setwise coprime".writeln;
return;
}
us[p] = 1, us[q] = 1;
}
else if(p * p <= a) continue;
else{
if(a > p && us[a]){
"setwise coprime".writeln;
return;
}
us[a] = 1;
break;
}
}
"pairwise coprime".writeln;
}
/// 最大公約数。最大の公約数。
/// 例 gcd(12, -18) = 6, gcd(-12, -18) = 6, gcd(12, 0) = 12, gcd(0, 0) = 0
/// ※本当は gcd(0, 0) は存在しないが便宜上 0 を返している
long gcd(long a, long b){
if(a < 0) a = -a;
if(b == 0) return a;
if(b < 0) return gcd(a, -b);
if(a % b == 0) return b;
if(a < b) return gcd(b, a);
else return gcd(b, a % b);
}
/// a 以下の素数の集合
/// 例 primesTo(48) = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
/// 時間O(N f(N))、空間O(N) ただしf(N)は素数の個数(=O(log N))
/// 都度計算なのでコストは都度かかる
long[] primesTo(long a){
bool[] isNg = new bool[](a + 1);
isNg[0] = 1, isNg[1] = 1;
foreach(i; 2 .. a + 1){
if(isNg[i]) continue;
foreach(k; 2 .. a / i + 1) isNg[i * k] = 1;
}
long[] res;
foreach(i; 0 .. a + 1) if(! isNg[i]) res ~= i;
return res;
}
/// 約数の集合
/// 1とその数自身も含む、正の約数の集合。
/// 例 divisors(48) = [1, 2, 3, 4, 6, 8, 12, 16, 24, 48]
/// O(√N)
long[] divisors(long a){
long[] res, res2;
for(long d = 1; d * d <= a; d ++){
if(a % d == 0) res ~= d, res2 ~= a / d;
}
foreach_reverse(d; res2) if(res[$ - 1] < d) res ~= d;
return res;
}
|
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(){
long n,k;
scan(n,k);
min(n%k,abs(n%k-k)).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()
{
auto S = sread();
writeln(max(S[0] - '0' - 1 + 9 * (S.length - 1), S.map!"a-'0'"().sum()));
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.conv;
enum long INF = long.max/5;
void main() {
while(solve()){}
}
bool solve() {
int n = readln.chomp.to!int;
if (n == 0) return false;
long[] as = new long[n];
foreach(i; 0..n) {
as[i] = readln.chomp.to!long;
}
kadane!(
long, max, -INF, "a+b", 0
)(as).writeln;
return true;
}
T kadane(
T, // 型
alias plusFun, // 加法の2項演算
T plusIdentity, // 加法の単位元
alias multFun, // 乗法の2項演算
T multIdentity // 乗法の単位元
)(T[] as) {
alias _plusFun = binaryFun!plusFun;
alias _multFun = binaryFun!multFun;
T res = plusIdentity;
T s = multIdentity;
foreach(a; as) {
s = _plusFun(_multFun(s, a), a);
res = _plusFun(res, s);
}
return res;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto as = readln.split.to!(int[]);
auto ss = new bool[](2000*n+1);
void solve(int i, int s) {
if (i == n) return;
solve(i+1, s);
s += as[i];
ss[s] = true;
solve(i+1, s);
}
solve(0, 0);
auto m = readln.chomp.to!int;
foreach (q; readln.split.to!(int[])) {
writeln(ss[q] ? "yes" : "no");
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, m; readV(n, m);
auto s = new int[][](m);
foreach (i; 0..m) {
auto rd = rdsp;
int k; pick(rd, k);
s[i] = new int[](k);
foreach (j; 0..k) pick(rd, s[i][j]);
--s[i][];
}
int[] p; readA(m, p);
auto r = 0;
loop: foreach (i; 0..(1<<n)) {
foreach (j; 0..m) {
auto c = s[j].count!(k => ((i>>k)&1) == 1);
if (c%2 != p[j]) continue loop;
}
++r;
}
writeln(r);
}
|
D
|
void main()
{
long n = rdElem;
long[] a = rdRow;
long[] b = new long[n+1];
long[long] cnt;
++cnt[0];
foreach (i, x; a)
{
b[i+1] = b[i] + a[i];
++cnt[b[i+1]];
}
long result;
foreach (x; cnt.byValue)
{
result += x * (x - 1) / 2;
}
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import 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 s1 = RD!string;
auto s2 = RD!string;
long last = -1;
foreach (i; 0..n)
{
if (last == -1)
{
if (s1[i] != s2[i])
{
ans[ti] += 2;
last = -1;
}
else if (s1[i] == '0')
{
ans[ti] += 1;
last = 0;
}
else
last = 1;
}
else if (last == 0)
{
if (s1[i] != s2[i])
{
ans[ti] += 2;
last = -1;
}
else if (s1[i] == '0')
{
ans[ti] += 1;
last = 0;
}
else
{
ans[ti] += 1;
last = -1;
}
}
else
{
if (s1[i] != s2[i])
{
ans[ti] += 2;
last = -1;
}
else if (s1[i] == '0')
{
ans[ti] += 2;
last = -1;
}
else
last = 1;
}
debug writeln("i:", i, " ", ans[ti]);
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum mod = 10L^^9 + 7;
enum inf = 10^^9;
void main() {
int n, k;
scan(n, k);
auto a = readln.split.to!(long[]);
auto b = new long[][](64, 0);
auto ac = new long[](n + 1);
foreach (i ; 0 .. n) {
ac[i+1] = ac[i] + a[i];
}
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n + 1) {
auto ss = ac[j] - ac[i];
debug {
writefln("(%d, %d) : %d", i, j, ss);
}
foreach (l ; 0 .. 64) {
if (ss & (1L << l)) {
b[l] ~= ss;
}
}
}
}
debug {
writefln("%(%s\n%)", b);
}
long ans;
for (int r = 63; r >= 0; r--) {
if (b[r].length < k) {
continue;
}
debug {
writeln(r, " ", b[r]);
}
ans += (1L<<r);
foreach (i ; 0 .. r) {
b[i] = null;
}
foreach (bi ; b[r]) {
foreach (i ; 0 .. r) {
if (bi & (1L << i)) {
b[i] ~= bi;
}
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// 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;
// dfmt on
void main()
{
long N = lread();
auto A = new long[](N);
foreach (i; 0 .. N)
A[i] = lread();
long s = cast(long) sqrt(cast(real) N);
long d = (N + s - 1) / s;
auto D = new long[](d);
D[] = long.min;
foreach (i; 0 .. N)
{
D[i / s] = D[i / s].max(A[i]);
}
// writeln(s, D);
foreach (i; 0 .. N)
{
long ans = long.min;
foreach (j, dv; D)
if (j != (i / s))
ans = ans.max(dv);
foreach (j; (i / s) * s .. min((i / s) * s + s, N))
{
if (j != i)
ans = ans.max(A[j]);
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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 c = RDA;
long tot1 = c[0], tot2 = c[1];
long x = c[0], y = c[1];
ans[ti] = x*n + y*n;
foreach (i; 2..n)
{
if (i % 2 == 0)
{
tot1 += c[i];
x.chmin(c[i]);
}
else
{
tot2 += c[i];
y.chmin(c[i]);
}
auto rem1 = n - (i+2) / 2;
auto rem2 = n - (i+1) / 2;
ans[ti].chmin(tot1 + tot2 + rem1*x + rem2*y);
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
long k;
rd(n, k);
auto s = readln.chomp.to!(char[]);
auto dp = new long[][](n + 1, n + 1);
dp[0][0] = 1;
auto last_pos = new int[](26);
fill(last_pos, -1);
for (int i = 1; i <= n; i++) {
foreach (j; 0 .. i) {
dp[i][j] += dp[i - 1][j]; // not use s[i - 1]
dp[i][j + 1] += dp[i - 1][j];
if (last_pos[s[i - 1] - 'a'] >= 0) {
dp[i][j + 1] -= dp[last_pos[s[i - 1] - 'a']][j];
}
}
last_pos[s[i - 1] - 'a'] = i - 1;
}
long ans = 0;
for (int j = n; j >= 0 && k > 0; j--) {
if (k >= dp[n][j]) {
ans += dp[n][j] * (n - j);
} else {
ans += k * (n - j);
}
k = max(0L, k - dp[n][j]);
}
if (k > 0) {
writeln(-1);
} else {
writeln(ans);
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.string,
std.stdio,
std.conv;
void main() {
int[] xab = readln.chomp.split.to!(int[]);
int x = xab[0],
a = xab[1],
b = xab[2];
int z = b - a;
if (z <= 0) {
writeln("delicious");
} else if (0 < z && z <= x) {
writeln("safe");
} else {
writeln("dangerous");
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto T = RD!int;
auto ans = new long[](T);
foreach (ti; 0..T)
{
auto n = RD!int;
auto a = RDA;
ans[ti] = a[0];
foreach (i; 0..n)
{
ans[ti] &= a[i];
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nd = readln.split.to!(int[]);
auto N = nd[0];
auto D = nd[1];
int[][] xs;
xs.length = N;
foreach (i; 0..N) {
xs[i] = readln.split.to!(int[]);
}
int c;
foreach (i; 0..N) {
foreach (j; i+1..N) {
int d;
foreach (k; 0..D) {
d += (xs[i][k] - xs[j][k]) ^^ 2;
}
int dd = sqrt(d.to!double).to!int^^2;
if (d == dd) ++c;
}
}
writeln(c);
}
|
D
|
void main() {
int n = readln.chomp.to!int;
writeln((n - 1) / 2);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[][](t, 2);
foreach (ti; 0..t)
{
auto n = RD!int;
auto x = RD!int;
auto a = RDA;
auto tot = a.sum;
ans[ti][0] = (tot+x-1) / x;
foreach (i; 0..n)
{
ans[ti][1] += (a[i]+x-1) / x;
}
}
foreach (e; ans)
writeln(e[0], " ", e[1]);
stdout.flush;
debug readln;
}
|
D
|
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, 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; }
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }
int L;
string S;
void main() {
try {
for (; ; ) {
S = readToken();
L = cast(int)(S.length);
bool ans;
foreach (i; 0 .. L + 1) foreach (j; i .. L + 1) {
if (S[0 .. i] ~ S[j .. L] == "keyence") {
ans = true;
}
}
writeln(ans ? "YES" : "NO");
}
} catch (EOFException 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;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
long calc(long n, long[] a) {
long cap = a.reduce!min;
// N 人を k 個のグループに分ける
long k = (n + cap - 1) / cap;
// k - 1 個のグループを進ませて、最後の 1 グループが 5 マス進む
return (k - 1) + 5;
}
void main() {
long n = read!long;
long[] a;
for (int i = 0; i < 5; i++) a ~= read!long;
writeln(calc(n, a));
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv;
void main() {
long x, y;
auto s = map!(x => x.to!long)(readln.chomp.split);
x = s[0];
y = s[1];
long res = 0;
while (x <= y) {
x *= 2;
++res;
}
writeln(res);
}
|
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() {
auto nq = readints();
int n = nq[0], q = nq[1];
auto bit = new BinaryIndexTree(n);
for (int i = 0; i < q; i++) {
auto xs = readints();
switch (xs[0]) {
case 0: // add
bit.add(xs[1], xs[2]);
break;
case 1: // sum
int sum = bit.sum(xs[2]) - bit.sum(xs[1] - 1);
writeln(sum);
break;
default:
break;
}
}
}
class BinaryIndexTree {
private int[] _bit;
this(int n) {
_bit = new int[n + 1];
}
int sum(int p) {
int s = 0;
while (p > 0) {
s += _bit[p];
p -= p & -p;
}
return s;
}
void add(int p, int x) {
while (p < _bit.length) {
_bit[p] += x;
p += p & -p;
}
}
}
|
D
|
import std.array;
import std.range;
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main(string[] args) {
string s = readln.chomp;
string[] words = ["dream", "dreamer", "erase", "eraser"];
bool ans = false;
loop: while (true) {
if (s.length <= 0) {
ans = true;
break loop;
} else {
foreach (word; words) {
if (s.endsWith(word)) {
s = s[0..($-word.length)];
continue loop;
}
}
break loop;
}
}
(ans ? "YES" : "NO").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;
void main()
{
auto n = readln.chomp.to!int;
auto s = readln.chomp;
int r, b;
foreach (i; 0..n) {
if (s[i] == 'R') r++;
else b++;
}
if (r > b) writeln("Yes");
else writeln("No");
}
|
D
|
// unihernandez22
// https://atcoder.jp/contests/abc150/tasks/abc150_c
// implementation
import std.stdio;
import std.array;
import std.conv: to;
import std.string: chomp;
import std.algorithm;
import std.range: iota;
import std.math: abs;
void main() {
int n = readln.chomp.to!int;
int[] p = readln.split.map!(to!int).array;
int[] q = readln.split.map!(to!int).array;
int[] x = iota(1, n+1).array;
int a, b;
int step = 1;
while (x.nextPermutation) {
if (x == p)
a = step;
if (x == q)
b = step;
step++;
}
abs(a-b).writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.range;
import std.conv;
import std.stdio;
import std.string;
import std.typecons;
alias FairyPos = Tuple!(int,int);
void main(){
auto inputs = readln.chomp.split.map!(to!int);
auto a = inputs[0];
auto b = inputs[1];
auto c = inputs[2];
string answer;
if(c <= a + b){
answer = "Yes";
}else{
answer = "No";
}
writeln(answer);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n - 1) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
adj[ai] ~= bi;
adj[bi] ~= ai;
}
auto c = new int[](n);
auto d = new int[](n);
auto p = new int[](n);
void dfs(int v, int par) {
p[v] = par;
c[v] = 1;
foreach (u ; adj[v]) {
if (u == par) continue;
d[u] = d[v] + 1;
dfs(u, v);
c[v] += c[u];
}
}
dfs(0, 0);
int u = n - 1;
foreach (i ; 0 .. (d[n - 1] + 1) / 2 - 1) {
u = p[u];
}
debug {
writeln(c);
writeln(d);
writeln(p);
writeln(u);
}
auto ans = 2*c[u] >= n ? "Snuke" : "Fennec";
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.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD;
auto A = RDA;
bool zero;
foreach (i; 0..N)
{
if (A[i] == 0)
zero = true;
}
if (zero)
writeln(0);
else
{
long ans = 1;
foreach (i; 0..N)
{
debug writeln("i:", i, " ans:", ans);
if (A[i] > 10L^^18 / ans)
{
ans = -1;
break;
}
ans *= A[i];
debug writeln("i:", i, " ans:", ans);
if (ans > 10L^^18)
{
ans = -1;
break;
}
}
writeln(ans);
}
stdout.flush;
debug readln;
}
|
D
|
import std;
const MOD = 10^^9 + 7;
long calc(int n, int k) {
long ans = 0;
for (long i = k; i <= n + 1; i++) {
long lo = (0 + i - 1) * i / 2;
long hi = (n - i + 1 + n) * i / 2;
ans = (ans + hi - lo + 1) % MOD;
}
return ans;
}
void main() {
int n, k; scan(n, k);
writeln(calc(n, k));
}
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
|
// Try AtCoder
/// author: Leonardone @ NEETSDKASU
import std.stdio : readln, writeln;
import std.string : chomp;
import std.array : split;
import std.conv : to;
import std.typecons : Tuple;
import std.math : abs;
auto getVals(T)() { return readln.chomp.split.to!(T[]); }
void main() {
Tuple!(int,"x",int,"a",int,"b") t = getVals!int()[0..3];
writeln(abs(t.x - t.a) < abs(t.x - t.b) ? "A" : "B");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n, m;
scan(n, m);
auto p = readln.split.to!(int[]);
p[] -= 1;
auto uf = UnionFind(n);
foreach (i ; 0 .. m) {
int xi, yi;
scan(xi, yi);
xi--, yi--;
uf.merge(xi, yi);
}
auto c = new int[](n);
foreach (i ; 0 .. n) {
c[i] = uf.findRoot(i);
}
auto ok = new bool[int][](n);
foreach (int i ; 0 .. n) {
ok[c[i]][i] = 1;
}
debug {
writeln(ok);
}
int ans;
foreach (i ; 0 .. n) {
if (p[i] in ok[c[i]]) {
ans++;
}
}
writeln(ans);
}
struct UnionFind {
private {
int _size;
int[] _parent;
}
this(int N)
in {
assert(N > 0);
}
body {
_size = N;
_parent = new int[](_size);
foreach (i ; 0 .. _size) {
_parent[i] = i;
}
}
int findRoot(int x)
in {
assert(0 <= x && x < _size);
}
body {
if (_parent[x] != x) {
_parent[x] = findRoot(_parent[x]);
}
return _parent[x];
}
bool same(int x, int y)
in {
assert(0 <= x && x < _size);
assert(0 <= y && y < _size);
}
body {
return findRoot(x) == findRoot(y);
}
void merge(int x, int y)
in {
assert(0 <= x && x < _size);
assert(0 <= y && y < _size);
}
body {
int u = findRoot(x);
int v = findRoot(y);
if (u == v) return;
_parent[v] = u;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
// 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, m;
int[] a;
void main() {
scan(n, m);
a = readln.split.to!(int[]);
auto imos0 = new long[](m + 2);
auto imos1 = new long[](m + 2);
foreach (i ; 0 .. n - 1) {
int s = a[i], t = a[i + 1];
if (s < t) {
imos0[1] += t - s;
imos0[s + 1] -= t - s;
imos0[s + 1] += t + 1;
imos0[t + 1] -= t + 1;
imos1[s + 1] -= 1;
imos1[t + 1] += 1;
imos0[t + 1] += t - s;
}
else {
imos0[1] += t + 1;
imos0[t + 1] -= t + 1;
imos1[1] -= 1;
imos1[t + 1] += 1;
imos0[t + 1] += m - s + t;
imos0[s + 1] -= m - s + t;
imos0[s + 1] += t + m + 1;
imos1[s + 1] -= 1;
}
debug {
writeln("imos0:", imos0);
writeln("imos1:", imos1);
}
}
foreach (i ; 0 .. m) {
imos0[i + 1] += imos0[i];
imos1[i + 1] += imos1[i];
}
debug {
writeln("imos0:", imos0);
writeln("imos1:", imos1);
}
long ans = 1L * m * n;
foreach (i ; 1 .. m + 1) {
ans = min(ans, imos0[i] + imos1[i] * i);
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.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;
alias Edge = Tuple!(int, "to", int, "idx");
void main() {
int N;
scan(N);
auto ans = new int[](N - 1);
ans[] = -1;
auto adj = new Edge[][](N, 0);
foreach (i ; 0 .. N - 1) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
adj[ai] ~= Edge(bi, i);
adj[bi] ~= Edge(ai, i);
}
int K = 0;
void dfs(int v, int p, int pc) {
int col = 1;
foreach (e ; adj[v]) if (e.to != p) {
if (col == pc) col++;
ans[e.idx] = col;
K = max(K, col);
dfs(e.to, v, col);
col++;
}
}
dfs(0, -1, -1);
writeln(K);
ans.each!(ansi => writeln(ansi));
}
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;
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;
void main()
{
auto _ = readln.chomp.split.map!(to!long);
long n = _.front;
long m = _.back;
/+
enum ofs = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [0, 0], [1, 0],
[-1, 1], [0, 1], [1, 1]
];
bool[][] field = new bool[][](n, m);
auto f = (int x, int y) {
return x < 0 || m <= x
|| y < 0 || n <= y;
};
foreach (i; 0..n) {
foreach (j; 0..m) {
foreach (d; ofs) {
int nx = cast(int)j + d[0];
int ny = cast(int)i + d[1];
if (f(nx, ny)) {
continue;
}
field[ny][nx] = !field[ny][nx];
}
}
}
foreach (i; 0..n) {
foreach (j; 0..m) {
if (field[i][j]) {
"o".write;
} else {
"x".write;
}
}
writeln;
}
+/
if (n == 1 && m == 1) {
1.writeln;
} else if (min(n, m) == 1) {
writeln(max(0, max(n, m) - 2));
} else {
writeln = n * m - 2 * n - 2 * m + 4;
}
}
|
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;
int[] di = [1, 1, -1, -1];
int[] dj = [1, -1, 1, -1];
void main() {
int n;
scan(n);
auto m = iota(n).map!(i => readln.chomp).array;
int ans;
foreach (i ; 1 .. n - 1) {
foreach (j ; 1 .. n - 1) {
if (m[i][j] != 'X') continue;
bool ok = true;
foreach (k ; 0 .. 4) {
int ni = i + di[k];
int nj = j + dj[k];
if (m[ni][nj] != 'X') {
ok = false;
break;
}
}
ans += ok;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.numeric, std.math, std.array;
int n, x; rd(n, x);
auto a=readln.split.to!(int[]);
auto d=a.map!(e=>abs(e-x)).array;
if(n==1){
writeln(d[0]);
return;
}
auto g=gcd(d[0], d[1]);
foreach(i; 2..n) g=gcd(g, d[i]);
writeln(g);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.range,
std.algorithm;
void main() {
int[] buf = readln.chomp.split.to!(int[]);
int n = buf[0], m = buf[1], x = buf[2];
int[] A = readln.chomp.split.to!(int[]);
int count_left,
count_right;
for (int i = x - 1; i >= 1; i--) {
if (canFind(A, i)) {
count_left++;
}
}
for (int i = x + 1; i <= n; i++) {
if (canFind(A, i)) {
count_right++;
}
}
writeln(min(count_left, count_right));
}
|
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()
{
bool[string] c;
auto a = readln.chomp.split;
foreach (e; a) {
c[e] = true;
}
c.keys.length.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;
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 N, M;
scan(N, M);
auto b = new bool[](M);
auto A = aryread();
b[] = false;
foreach (a; A[1 .. $])
b[a - 1] = true;
foreach (_; 1 .. N)
{
auto C = aryread();
auto tmp = new bool[](M);
foreach (c; C[1 .. $])
tmp[c - 1] = true;
foreach (i; 0 .. b.length)
b[i] = b[i] && tmp[i];
}
// writeln(b);
writeln(M - b.map!(x => 0 ^^ x).sum());
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.group.canFind!"a[1] >= 3" ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
contest
son
---+/
/+:---test
contest
programming
---+/
/+:---test
contest
sentence
---+/
void main(string[] args) {
const S = readln.chomp;
const T = readln.chomp;
const Sn = S.length;
long[][] chpos;
chpos.length = 'z'-'a';
foreach (i, c; S) {
chpos[c-'a'] ~= i;
}
long rotation;
long offset;
foreach (i, c; T) {
auto pos = chpos[c-'a'].assumeSorted;
if (pos.length == 0) {
"-1".writeln;
return;
}
auto upper_pos = pos.upperBound(offset-1);
if (upper_pos.length > 0) {
offset = upper_pos[0]+1;
} else {
offset = pos[0]+1;
++rotation;
}
}
(rotation*Sn + offset).writeln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto xs = 5.iota.map!(_ => readln.chomp.to!int).array;
auto cs = xs.map!(x => x % 10).filter!(m => m > 0).array;
auto m = cs.empty ? 0 : (10 - cs.reduce!min);
writeln(xs.map!(x => cast(int)(ceil(x * 0.1) * 10)).sum - m);
}
|
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);}}
enum MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long a, b;
scan(a, b);
writeln(a * b);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.