code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
long n, a, b;
sc.read(n, a, b);
if (a > b) {
writeln(0);
return 0;
}
if (a == b) {
writeln(1);
return 0;
}
if (n == 1) {
writeln(0);
return 0;
}
if (n == 2) {
writeln(1);
return 0;
}
writeln((b-a)*(n-2)+1);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
const N = tmp[0], K = tmp[1];
const a = N/K;
const b = (K&1) ? 0 : (N+K/2)/K;
writeln(a^^3 + b^^3);
}
|
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 a = RD;
auto b = RD;
auto c = RD;
auto d = RD;
long ans = a * c;
ans.chmax(a*d);
ans.chmax(b*c);
ans.chmax(b*d);
writeln(ans);
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 inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, c;
scan(n, c);
auto w = new int[](n);
auto v = new long[](n);
foreach (i ; 0 .. n) {
scan(w[i], v[i]);
}
auto b = w[0];
w[] -= w[0];
auto dp = new long[][][](n + 1, n + 1, 3*n + 1);
fillAll(dp, -inf6);
foreach (wi ; 0 .. 3*n + 1) {
dp[0][0][wi] = 0;
}
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
foreach (wi ; 0 .. 3*n + 1) {
if (dp[i][j][wi] == -inf6) continue;
dp[i + 1][j][wi] = max(dp[i + 1][j][wi], dp[i][j][wi]);
if (wi + w[i] <= 3*n) {
dp[i + 1][j + 1][wi + w[i]] = max(dp[i + 1][j + 1][wi + w[i]], dp[i][j][wi] + v[i]);
}
}
}
}
long ans;
foreach (j ; 1 .. n + 1) {
foreach (wi ; 0 .. 3*n + 1) {
if (1L * b * j + wi > c) break;
ans = max(ans, dp[n][j][wi]);
}
}
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;
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;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
auto nt = aryread;
long r = long.max;
foreach (i; 0 .. nt[0])
{
auto tmp = aryread;
if (tmp[1] <= nt[1])
{
r = min(r,tmp[0]);
}
}
if (r == long.max)
{
writeln("TLE");
return;
}
writeln(r);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() { // 日本語文字列のテスト
int w = readint;
int n = readint;
auto xs = new int[w + 1];
for (int i = 0; i < xs.length; i++)
xs[i] = i;
for (int i = 0; i < n; i++) {
auto ab = readln.chomp.split(",").to!(int[]);
int a = ab[0], b = ab[1];
swap(xs[a], xs[b]);
}
foreach (x; xs[1 .. $])
writeln(x);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(ulong[]);
size_t i, j;
ulong res, sum = AS[j], xor = AS[j];
while (i < N)
{
if (sum == xor && j < N)
{
if (++j == N)
continue;
sum += AS[j];
xor ^= AS[j];
}
else
{
res += j - i;
sum -= AS[i];
xor ^= AS[i];
++i;
}
}
writeln(res);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto x = RD;
auto a = RDA;
bool allX = true;
bool hasX;
foreach (i; 0..n)
{
if (a[i] != x)
allX = false;
else
hasX = true;
}
if (allX) continue;
if (hasX)
{
ans[ti] = 1;
}
else
{
long tot;
foreach (i; 0..n)
tot += a[i];
if (tot / n == x && tot % n == 0)
ans[ti] = 1;
else
ans[ti] = 2;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
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, k; readV(n, k);
int[] a; readA(n, a);
auto b = new bool[](k+1);
foreach (i; 1..k+1)
b[i] = a.any!(ai => i-ai >= 0 && !b[i-ai]);
writeln(b[$-1] ? "First" : "Second");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void get(Args...)(ref Args args)
{
import std.traits, std.meta, std.typecons;
static if (Args.length == 1) {
alias Arg = Args[0];
static if (isArray!Arg) {
static if (isSomeChar!(ElementType!Arg)) {
args[0] = readln.chomp.to!Arg;
} else {
args[0] = readln.split.to!Arg;
}
} else static if (isTuple!Arg) {
auto input = readln.split;
static foreach (i; 0..Fields!Arg.length) {
args[0][i] = input[i].to!(Fields!Arg[i]);
}
} else {
args[0] = readln.chomp.to!Arg;
}
} else {
auto input = readln.split;
assert(input.length == Args.length);
static foreach (i; 0..Args.length) {
args[i] = input[i].to!(Args[i]);
}
}
}
void get_lines(Args...)(size_t N, ref Args args)
{
import std.traits, std.range;
static foreach (i; 0..Args.length) {
static assert(isArray!(Args[i]));
args[i].length = N;
}
foreach (i; 0..N) {
static if (Args.length == 1) {
get(args[0][i]);
} else {
auto input = readln.split;
static foreach (j; 0..Args.length) {
args[j][i] = input[j].to!(ElementType!(Args[j]));
}
}
}
}
void main()
{
int T; get(T);
while (T--) {
int N; get(N);
int[] RS; get(RS);
RS = [0] ~ RS;
foreach (i; 0..N) RS[i+1] += RS[i];
int M; get(M);
int[] BS; get(BS);
BS = [0] ~ BS;
foreach (i; 0..M) BS[i+1] += BS[i];
int r;
foreach (i; 0..N+1) foreach (j; 0..M+1) r = max(r, RS[i] + BS[j]);
writeln(r);
}
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.numeric;
int n;
long[] t;
void main() {
scan(n);
t = new long[](n);
iota(n).each!(i => scan(t[i]));
if (n == 1) {
writeln(t[0]);
return;
}
long ans = lcm(t[0], t[1]);
foreach (i ; 2 .. n) {
ans = lcm(ans, t[i]);
}
writeln(ans);
}
long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
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;
import std.array, std.algorithm, std.range;
void main()
{
iota(readln().chomp().to!int()).map!(_=>readln().chomp().replace("Hoshino","Hoshina")).array().join("\n").writeln();
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str, T fix) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool INSIDE(T)(T x, T b, T e) { return x >= b && x < e; }
long mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
long[] dijkstra(long from, long to, long[][] edges)
{
long[] path;
auto cost = new long[](edges.length); fill(cost, long.max); cost[from] = 0;
long[][] open = [[from]];
while (!open.empty)
{
auto n = open.front; open.popFront;
auto p = n[0];
foreach (i; 0..edges.length)
{
if (edges[p][i] == -1) continue;
auto c = cost[p] + edges[p][i];
if (c < cost[to] && c < cost[i]) { cost[i] = c; if (i == to) path = i ~ n; else open ~= i ~ n; }
}
}
return cost[to] ~ path;
}
void main()
{
auto N = RD!long;
auto p = RDR.ARR!long(-1);
long ans;
bool hasLast = false;
foreach (i; 0..N)
{
if (p[i] == i)
{
if (hasLast)
{
hasLast = false;
}
else
{
++ans;
hasLast = true;
}
}
else
{
hasLast = false;
}
}
writeln(ans);
stdout.flush();
}
|
D
|
// import chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
int n = cin.next!int;
int[] a1 = cin.nextArray!int(n);
int[] a2 = cin.nextArray!int(n);
int ma = -1;
foreach (i; 0 .. n) {
int sum;
foreach (j; 0 .. n) {
sum += j == i ? a1[j] + a2[j] : j > i ? a2[j] : a1[j];
}
ma = max(sum, ma);
}
writeln(ma);
}
|
D
|
void main()
{
int[] abc = readln.split.to!(int[]);
writeln(abc.all!(x => x == abc[0]) ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
int[] maps = new int[256];
int[] mapt = new int[256];
int sn = 1, tn = 1;
bool check(char sc, char tc) {
if(maps[sc]==0) maps[sc] = sn++;
if(mapt[tc]==0) mapt[tc] = tn++;
return maps[sc]==mapt[tc];
}
bool solve() {
auto s = readln.chomp;
auto t = readln.chomp;
for(int i=0; i<s.length; i++) if(!check(s[i], t[i])) return false;
return true;
}
void main() {
if(solve()) 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 = 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 r = RD!string;
auto b = RD!string;
long c1, c2;
foreach (i; 0..n)
{
if (r[i] > b[i])
++c1;
else if (r[i] < b[i])
++c2;
}
if (c1 > c2)
ans[ti] = -1;
else if (c1 < c2)
ans[ti] = 1;
}
foreach (e; ans)
{
writeln(e == -1 ? "RED" : e == 1 ? "BLUE" : "EQUAL");
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long n = read.to!long;
long k = read.to!long;
if(n >= k * 2 - 1) writeln("YES");
else writeln("NO");
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto s=readln.chomp.to!(char[]);
auto vo=['a', 'e', 'i', 'o', 'u', 'y'],
used=new bool[](s.length);
while(true){
auto found=false;
foreach(i; 0..(s.length)){
if(used[i]) continue;
if(canFind(vo, s[i])==false) continue;
if(i+1<s.length){
if(used[i+1]) continue;
if(canFind(vo, s[i+1])){
s=s[0..(i+1)]~s[(i+2)..$];
found=true; break;
}
}
}
if(found==false) break;
}
writeln(s);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
void main() {
problem();
}
void problem() {
auto X = scan!long;
const DELTA = 0.000001L;
long solve() {
long theta;
real x = sin(PI*theta/180L);
real y = cos(PI*theta/180L);
while(true) {
theta += X;
x += sin(PI*theta/180L);
y += cos(PI*theta/180L);
[x, y, PI].deb;
if (-DELTA <= x && x <= +DELTA && -DELTA <= y && y <= +DELTA) break;
}
return theta / X + 1;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.numeric, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.stdio;
import std.math;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
import std.range;
import std.file;
import std.datetime;
void main() {
int elemanSayisi = stdin.readln.strip.to!int;
int[] elemanlar = stdin.readln.strip.split.map!(a=> a.to!int()).array;
int toplam = 0;
for ( int i = 0; i < elemanlar.length - 1; i++ )
{
if ( elemanlar[i] == 1 && elemanlar[i+1] == 2 )
{
if ( i > 0 && elemanlar[i - 1] == 3 )
toplam += 2;
else
toplam += 3;
}
else if ( elemanlar[i] == 2 && elemanlar[i+1] == 1 )
toplam += 3;
else if ( elemanlar[i] == 3 && elemanlar[i+1] == 1 )
toplam += 4;
else if ( elemanlar[i] == 1 && elemanlar[i+1] == 3 )
toplam += 4;
else
{
return writeln("Infinite");
}
}
writeln("Finite");
writeln(toplam);
}
|
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 na = scan!int, nb = scan!int, m = scan!int;
long[] as = scan!long(na);
long[] bs = scan!long(nb);
S[] ss;
foreach(j; 0 .. m) ss ~= S(scan!int - 1, scan!int - 1, scan!long);
long amin = as[0], bmin = bs[0];
foreach(a; as) amin.lowerTo(a);
foreach(b; bs) bmin.lowerTo(b);
long ans = amin + bmin;
foreach(s; ss){
long tmp = as[s.x] + bs[s.y] - s.c;
ans.lowerTo(tmp);
}
ans.writeln;
}
struct S{
int x, y;
long c;
}
|
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 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; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (i; 0..t)
{
auto x = RD;
auto y = RD;
bool[long] set;
set[x] = true;
while (x < y)
{
if (x % 2 == 0)
x = x * 3 / 2;
else
--x;
if (set.get(x, false))
break;
set[x] = true;
}
ans[i] = x >= y;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto march = new long[](5);
long ans;
foreach (_; 0 .. n)
{
auto s = sread();
auto head = s[0];
switch (head)
{
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:
break;
}
}
foreach (i; 0 .. (1 << 5))
{
long tmp = 1;
if (i.popcnt == 3)
{
foreach (j; 0 .. march.length)
{
if(i & (1 << j))
tmp *= march[j];
}
ans += tmp;
}
}
ans.writeln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string s; readV(s);
writeln(s[0..5], " ", s[6..13], " ", s[14..19]);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
long H = lread();
long W = lread();
writeln((N - W + 1) * (N - H + 1));
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto L = scanElem+scanElem;
auto R = scanElem+scanElem;
if(L<R){
writeln("Right");
}else if(L>R){
writeln("Left");
}else{
writeln("Balanced");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main(){
auto q=readln.split.to!(int[]),a=q[0],b=q[1];
if(a*b%2==0)writeln("Even");
else writeln("Odd");
}
|
D
|
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
writeln(a.reduce!max - a.reduce!min);
}
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 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;
}
}
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(T[], "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n;
cin.scan(n);
string[] res;
uint size;
DList!string q;
foreach (i; 1 .. 10) {
q ~= "" ~ cast(char)(i + '0');
}
while (!q.empty) {
auto t = q.front;
res ~= t;
if (res.length >= n) break;
q.removeFront;
if (t[$ - 1] == '0') {
q ~= t ~ "0";
q ~= t ~ "1";
} else if (t[$ - 1] == '9') {
q ~= t ~ "8";
q ~= t ~ "9";
} else {
q ~= t ~ cast(char)(t[$ - 1] - 1);
q ~= t ~ t[$ - 1];
q ~= t ~ cast(char)(t[$ - 1] + 1);
}
}
writeln(res[n - 1]);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional;
import std.algorithm, std.container;
struct Katana{
long a, b;
}
void main()
{
long Q = scanElem;
bool[] prime;
prime.length = 10^^5+1;
foreach(n; primes(10^^5))
{
prime[n] = true;
}
long[] nList;
nList.length = prime.length;
foreach(i; 3..nList.length)
{
if(i%2==0){
nList[i] = nList[i-1];
continue;
}
nList[i] = nList[i-2] + ((prime[i] && prime[(i+1)/2]) ? 1 : 0);
}
foreach(i; 0..Q)
{
auto l = scanElem;
auto r = scanElem;
writeln(nList[r]-nList[l-1]);
}
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
auto input = readln.split.to!(ulong[]);
ulong L = input[0], R = input[1];
int ans = int.max;
ulong end = L + min(R - L, 2019) + 1;
foreach(i; L..(end - 1)) {
foreach(j; (i+1)..end) {
ans = min(ans, ((i % 2019) * (j % 2019)) % 2019);
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
string s;
scan(N);
scan(s);
auto r = new long[](N + 1);
auto g = new long[](N + 1);
auto b = new long[](N + 1);
foreach (i ; 1 .. N + 1) {
r[i] = r[i - 1] + (s[i - 1] == 'R');
g[i] = g[i - 1] + (s[i - 1] == 'G');
b[i] = b[i - 1] + (s[i - 1] == 'B');
}
long ans;
foreach (i ; 0 .. N) {
if (s[i] == 'R') {
ans += g[i] * (b[N] - b[i + 1]);
ans += b[i] * (g[N] - g[i + 1]);
}
if (s[i] == 'G') {
ans += r[i] * (b[N] - b[i + 1]);
ans += b[i] * (r[N] - r[i + 1]);
}
if (s[i] == 'B') {
ans += r[i] * (g[N] - g[i + 1]);
ans += g[i] * (r[N] - r[i + 1]);
}
foreach (j ; 1 .. N) {
if (i - j < 0 || i + j >= N) break;
if (s[i - j] != s[i] && s[i] != s[i + j] && s[i - j] != s[i + j]) {
ans--;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
auto c = readln.chomp.to!int;
auto d = readln.chomp.to!int;
writeln(min(a,b) + min(c,d));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
long manhattan(long x1, long y1, long x2, long y2)
{
return abs(x2 - x1) + abs(y2 - y1);
}
void main()
{
auto s = sread();
if(s[0] == s[1] || s[1] == s[2] || s[2] == s[3])
writeln("Bad");
else
writeln("Good");
}
|
D
|
import std.stdio, std.conv, std.math, std.string, std.range, std.array, std.algorithm, std.typecons;
void main(){
int[4] m;
foreach(immutable int i; 0 .. 3) {
auto buf = readln().strip().split().map!(to!int)();
m[buf[0]-1] ++;
m[buf[1]-1] ++;
}
foreach(immutable i; m) {
if(i >= 3) {
writeln("NO");
return;
}
}
writeln("YES");
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
string s = readln.chomp;
int scount;
int tcount;
foreach(c; s){
if(c == 'S') scount += 1;
else{
if(scount > 0) scount -= 1;
else tcount += 1;
}
}
int ans = tcount * 2;
ans.writeln;
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto A = scan!long(N);
long solve() {
long[long] dp;
dp[0] = 1_000;
foreach(price; A) {
foreach(kabu; dp.keys) {
auto kabuToBuy = dp[kabu] / price;
auto maxKabu = kabu + kabuToBuy;
auto boughtMoney = dp[kabu] - kabuToBuy*price;
if (maxKabu in dp) {
if (dp[maxKabu] < boughtMoney) dp[maxKabu] = boughtMoney;
} else {
dp[maxKabu] = boughtMoney;
}
auto selledMoney = dp[maxKabu] + maxKabu*price;
if (selledMoney > dp[0]) dp[0] = selledMoney;
}
}
return dp[0];
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
void main() {
int N, M; scanf("%d %d\n", &N, &M);
auto A = readln.chomp.split(" ").map!(to!int).array;
auto B = readln.chomp.split(" ").map!(to!int).array;
assert(N == A.size);
assert(M == B.size);
int calc(int a, int b, int p, int q) {
int a_score = 0, b_score = 0;
for (int i = p; i < a; i++) {
if (A[i] < 0) continue;
a_score += A[i];
}
for (int i = q; i < b; i++) {
if (B[i] < 0) continue;
b_score += B[i];
}
//log([a, b, p, q], [a_score, b_score]);
return a_score - b_score;
}
int[Tuple!(int, int, int, int, int, int)] cache;
int dfs(int w, int a, int b, int p, int q, int pass_count) {
if (pass_count == 2) return 0;
auto key = tuple(w, a, b, p, q, pass_count);
if (key in cache) return cache[key];
int ret = (w == 0 ? int.min / 4 : int.max / 4);
if (w == 0) {
ret = max(ret, dfs( (w + 1) % 2, a, b, a, b, pass_count + (a == p && b == q) ) + calc(a, b, p, q));
if (a < A.size)
ret = max(ret, dfs( (w + 1) % 2, a + 1, b, p, A[a] == -1 ? b : q, 0 ));
} else {
ret = min(ret, dfs( (w + 1) % 2, a, b, a, b, pass_count + (a == p && b == q) ) + calc(a, b, p, q));
if (b < B.size)
ret = min(ret, dfs( (w + 1) % 2, a, b + 1, B[b] == -1 ? a : p, q, 0 ));
}
return cache[key] = ret;
}
writeln(dfs(0, 0, 0, 0, 0, false));
/*
foreach (k, v; cache) {
writeln(k, " -> ", v);
}
*/
}
|
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 t = Tuple!(string, "name", long, "y");
void main()
{
auto x = lread();
auto n = (x + 5 + 6 - 1) / (5 + 6);
if (5 * (n - 1) + 6 * n >= x)
{
writeln(2 * n - 1);
}
else
{
writeln(2 * n);
}
}
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;
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.exception;
long x, y; rd(x, y);
if(x==y) writeln(-1);
else if(x<y) writeln(x);
else writeln(x%y==0 ? -1 : x);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
import std.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 f = false;
foreach (i; 1..n+1) {
auto d = (i * 1.08).to!int;
if (d == n) {
writeln(i);
return;
}
}
writeln(":(");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
const auto ri = [[-1, 2, 4, 1, 3,-1],
[ 3,-1, 0, 5,-1, 2],
[ 1, 5,-1,-1, 0, 4],
[ 4, 0,-1,-1, 5, 1],
[ 2,-1, 5, 0,-1, 3],
[-1, 3, 1, 4, 2,-1]];
void main()
{
auto di = readln.split.map!(to!int).array;
auto q = readln.chomp.to!size_t;
foreach (_; 0..q) {
auto rd = readln.split.map!(to!int), t = rd[0], f = rd[1];
auto ti = di.countUntil(t), fi = di.countUntil(f);
writeln(di[ri[ti][fi]]);
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int n; readV(n);
int[int] h;
foreach (_; 0..n) {
int a; readV(a);
h[a]++;
}
auto r = 0;
foreach (v; h.values)
if (v%2 == 1) ++r;
writeln(r);
}
|
D
|
import std.stdio, std.string, std.conv;
bool isPrime(int x) {
if (x < 2) return false;
if (x == 2) return true;
if (x % 2 == 0) return false;
for (int i = 3; i * i <= x; i += 2) {
if (x % i == 0) return false;
}
return true;
}
void main() {
int n = readln.chomp.to!int;
int cnt = 0;
foreach (i; 0 .. n) {
int x = readln.chomp.to!int;
if (isPrime(x)) ++cnt;
}
cnt.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
class Process{
private:
string _name;
int _time;
public:
@property{
string name() const { return _name; }
int time() const { return _time; }
}
this(string n, int t){
_name = n;
_time = t;
}
void print(int t) {
writeln(name, " ", t);
}
void sub(int a){
_time -= a;
}
}
int main(char[][]argv)
{
string[] str = readln.split();
int n = str[0].to!int(), q = str[1].to!int(), t = 0;
Process[] p;
for(int i = 0; i < n; ++i){
str = readln.split();
p ~= new Process(str[0], str[1].to!int());
}
while(p.length != 0){
//writeln("t : ", t);
//foreach(e; p){
// writeln("p.name : ", e.name, " p.time : ", e.time);
//}
//writeln();
p[0].sub(q);
if(p[0].time <= 0){
p[0].print(p[0].time + t + q);
t += p[0].time + q;
p = p[1..$];
}
else{
t += q;
auto tmp = p[0];
p = p[1..$];
p ~= tmp;
}
}
return 0;
}
|
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);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N, M, C;
scan(N, M, C);
auto B = aryread();
long cnt;
foreach (_; 0 .. N)
{
auto A = aryread();
long sum;
foreach (i, a; A)
{
sum += a * B[i];
}
sum += C;
if (0 < sum)
{
cnt++;
}
}
writeln(cnt);
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long h = read.to!long;
long w = read.to!long;
long ans = long.max;
long p;
p = p1(h, w); if(p < ans) ans = p;
p = p2(h, w); if(p < ans) ans = p;
p = p3(h, w); if(p < ans) ans = p;
p = p4(h, w); if(p < ans) ans = p;
p = p5(h, w); if(p < ans) ans = p;
p = p6(h, w); if(p < ans) ans = p;
p = p7(h, w); if(p < ans) ans = p;
p = p8(h, w); if(p < ans) ans = p;
ans.writeln;
}
long p1(long h, long w){
long a = (h / 3) * w;
long b = (h / 3) * w;
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p2(long h, long w){
long a = h * (w / 3);
long b = h * (w / 3);
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p3(long h, long w){
long a = (h / 3) * w;
long b = (h - h / 3) * (w / 2);
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p4(long h, long w){
long a = h * (w / 3);
long b = (h / 2) * (w - w / 3);
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p5(long h, long w){
long a = ((h + 2) / 3) * w;
long b = (h - (h + 2) / 3) * (w / 2);
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p6(long h, long w){
long a = h * ((w + 2) / 3);
long b = (h / 2) * (w - (w + 2) / 3);
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p7(long h, long w){
long a = ((h + 2) / 3) * w;
long b = ((h + 2) / 3) * w;
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
long p8(long h, long w){
long a = h * ((w + 2) / 3);
long b = h * ((w + 2) / 3);
long c = h * w - a - b;
return max(a, b, c) - min(a, b, c);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
void main() {
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
int res;
foreach(x_i; readln.split.to!(int[])) {
res += min(abs(K - x_i), x_i)*2;
}
writeln(res);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
void main(){
auto s = readln().chomp();
int[char] m;
foreach(si; s) {
m[si] = 1;
}
bool f = true;
int v = m.get('S', 0) + m.get('N', 0);
int h = m.get('E', 0) + m.get('W', 0);
if(v % 2 == 1 || h % 2 == 1) {
writeln("No");
} else {
writeln("Yes");
}
}
|
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;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string picture(bool[] xs){ return xs.array.map!(x => x? '#': '.').array; } string picture(bool[][] xf){ return xf.map!(xs => xs.picture).array.join("\n"); }
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 gen(){
}
void jury(){
}
void solve(){
int k = scan!int;
foreach(i; 0 .. k) "ACL".write;
"".writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
if (N & 1)
{
writeln(0);
return;
}
long x = 10;
long ans;
while (x <= N)
{
ans += N / x;
x *= 5;
}
writeln(ans);
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
// 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 x;
sc.scan(x);
int resNum;
int min = to!int(x[0 .. 2]);
foreach (i; 0 .. x.length - 2) {
string res;
res = x[i .. i + 3];
int tmp = to!int(res);
if (abs(tmp - 753) < abs(min - 753)) {
min = tmp;
}
}
writeln(abs(min - 753));
}
|
D
|
void main() {
int n = readln.chomp.to!int;
int people;
foreach (i; 0 .. n) {
int[] tmp = readln.split.to!(int[]);
int l = tmp[0], r = tmp[1];
people += r - l + 1;
}
people.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
readln()[0].write;
readln()[1].write;
readln()[2].writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!string;
long ans;
auto cnt = new long[](26);
foreach (i; 0..26)
{
auto tokens = s.split(['a'+i]);
long len;
foreach (token; tokens)
{
len = max(len, token.length);
}
cnt[i] = len;
}
char c = cast(char)('a' + MIN_POS(cnt));
while (true)
{
bool ok = true;
foreach (cc; s)
{
if (cc != c)
{
ok = false;
break;
}
}
if (ok) break;
++ans;
string ns;
auto streaks = new long[](s.length);
long streak;
foreach (i; 0..s.length-1)
{
if (s[i] == c || s[i+1] == c)
{
ns ~= c;
streaks[i] = streak;
streak = 0;
}
else
{
ns ~= s[i];
++streak;
}
}
/*if (s[$-1] == c)
{
ns ~= c;
long pos = MIN_POS!"a > b"(streaks);
long mid = max(pos-1,0);
s = ns[0..mid] ~ ns[mid+1..$];
}
else*/
{
s = ns;
}
debug writeln(s);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
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;
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
void main() {
auto N = readln.chomp.to!int;
auto X = new long[](N);
auto Y = new long[](N);
foreach (i; 0..N) {
auto s = readln.split.map!(to!long);
X[i] = s[0];
Y[i] = s[1];
}
bool line(int i, int j, int k) {
if (X[i] == X[j]) return X[i] == X[k];
if (Y[i] == Y[j]) return Y[i] == Y[k];
if (X[i] == X[k]) return X[i] == X[j];
if (Y[i] == Y[k]) return Y[i] == Y[j];
return (Y[k] - Y[i]) * (X[j] - X[i]) == (Y[j] - Y[i]) * (X[k] - X[i]);
}
const long MOD = 998244353;
auto used = new bool[][](N, N);
long ans = 1 + N;
foreach (i; 0..N) {
foreach (j; i+1..N) {
if (used[i][j])
continue;
long cnt = 2;
int[] v = [i, j];
foreach (k; j+1..N) {
if (used[i][k] || !line(i, j, k))
continue;
cnt += 1;
v ~= k;
}
foreach (a; 0..v.length)
foreach (b; a+1..v.length)
used[v[a]][v[b]] = true;
ans += powmod(2, cnt, MOD) - cnt - 1;
ans %= MOD;
}
}
ans = powmod(2, N, MOD) - ans;
ans = (ans % MOD + MOD) % MOD;
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 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 t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int-1;
auto k = RD!int;
auto a = RDA;
auto l = min(m, k);
auto len = n - m;
foreach (i; 0..l+1)
{
long tmp1 = long.max;
foreach (j; 0..m-l+1)
{
long tmp2;
tmp2.chmax(a[i+j]);
tmp2.chmax(a[i+j+len-1]);
//debug writeln(i, ":", j, " ", tmp2);
tmp1.chmin(tmp2);
}
ans[ti].chmax(tmp1);
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.random;
void main() {
while (1) {
auto x = readln.chomp.split;
if (x[1] == "?") break;
if (x[1] == "+") writeln(x[0].to!int+x[2].to!int);
if (x[1] == "-") writeln(x[0].to!int-x[2].to!int);
if (x[1] == "*") writeln(x[0].to!int*x[2].to!int);
if (x[1] == "/") writeln(x[0].to!int/x[2].to!int);
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
string s;
scan(s);
int n = s.length.to!int;
auto tp = new int[](n + 1);
tp[0] = 1;
foreach (i; 1 .. n + 1) {
tp[i] = tp[i-1] * 10 % 2019;
}
long ans;
int t;
auto cnt = new int[](2020);
cnt[0] = 1;
foreach (i; 0 .. n) {
(t += (s[n - 1 - i] - '0') * tp[i]) %= 2019;
ans += cnt[t];
cnt[t]++;
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, m;
scan(n, m);
auto cnt = new int[](n);
cnt[] = 1;
auto canRed = new bool[](n);
canRed[0] = 1;
foreach (i ; 0 .. m) {
int xi, yi;
scan(xi, yi);
xi--, yi--;
if (canRed[xi]) {
canRed[yi] = true;
}
cnt[xi]--;
cnt[yi]++;
if (cnt[xi] == 0) {
canRed[xi] = false;
}
debug {
writefln("cnt: %(%s %)", cnt);
writefln("red: %(%b %)", canRed);
}
}
int ans = canRed.count!"a".to!int;
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;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto a = aryread();
// writeln(a);
long cnt;
foreach (i; 0 .. n)
{
if (a[a[i] - 1] == i + 1)
{
// writeln(a[a[i] - 1]);
cnt += 1;
}
}
writeln(cnt / 2);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
//import std.math;
import std.container;
import std.numeric;
import std.bigint;
import core.stdc.stdlib;
alias Tuple!(int, "lower", int, "upper") bound;
int N, K;
int[][] adj;
bound[] nums;
int[] ans;
bound bfs(int node, int prev, bound b) {
if (nums[node].lower != int.min &&
b.lower != int.min &&
(b.lower > nums[node].upper ||
b.upper < nums[node].lower ||
abs(b.upper%2) != abs(nums[node].upper%2))) {
//writeln(node, " ", b, " ", nums[node]);
//writeln(nums);
writeln("No");
exit(0);
}
nums[node].lower = max(nums[node].lower, b.lower);
nums[node].upper = min(nums[node].upper, b.upper);
foreach (child; adj[node]) {
if (child == prev) continue;
auto ret_b = bfs(child, node, bound(nums[node].lower-1, nums[node].upper+1));
if (nums[node].lower != int.min &&
ret_b.lower != int.min &&
(ret_b.lower > nums[node].upper+1 ||
ret_b.upper < nums[node].lower-1 ||
abs(ret_b.upper%2) == abs(nums[node].upper%2))) {
//writeln(child, " ", ret_b, " ", nums[node]);
//writeln(nums);
writeln("No");
exit(0);
}
nums[node].lower = max(nums[node].lower, ret_b.lower-1);
nums[node].upper = min(nums[node].upper, ret_b.upper+1);
}
return nums[node];
}
void dfs(int node, int prev, int prev_n) {
if (nums[node].lower == nums[node].upper)
ans[node] = nums[node].lower;
else if (prev_n + 1 <= nums[node].upper)
ans[node] = prev_n + 1;
else
ans[node] = prev_n - 1;
foreach (child; adj[node])
if (child != prev)
dfs(child, node, ans[node]);
}
void main() {
N = readln.chomp.to!int;
adj = new int[][](N);
foreach (i; 0..N-1) {
auto input = readln.split.map!(to!int);
adj[input[0]-1] ~= input[1]-1;
adj[input[1]-1] ~= input[0]-1;
}
K = readln.chomp.to!int;
nums = new bound[](N);
foreach (i; 0..N) nums[i] = bound(int.min, int.max);
int root;
foreach (i; 0..K) {
auto input = readln.split.map!(to!int);
nums[input[0]-1] = bound(input[1], input[1]);
root = input[0]-1;
}
bfs(root, -1, bound(int.min, int.max));
ans = new int[](N);
dfs(root, -1, nums[root].lower);
"Yes".writeln;
ans.each!(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;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
void main() {
readln.split.join.to!int.pipe!(
a => a%4==0 ? "YES":"NO"
).writeln;
}
// ----------------------------------------------
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio;
import std.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 s = readln.chomp.map!(to!char).array;
auto k = readln.chomp.to!int;
foreach (i; 0..s.length-1) {
if (s[i] == 'a') continue;
if (26 - (s[i] - 'a') > k) continue;
k -= 26 - (s[i] - 'a');
s[i] = 'a';
}
s[$-1] = ('a' + (s[$-1] - 'a' + k) % 26).to!char;
s.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
readln.chomp.count!(x=>x=='1').writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto NM = readln.split.to!(int[]);
auto N = NM[0], M = NM[1];
auto H = readln.split.to!(int[]);
int[] AB_max;
AB_max.length = N;
foreach (i; 0 .. M) {
auto ab = readln.split.to!(int[]);
AB_max[ab[0]-1] = max(AB_max[ab[0]-1], H[ab[1]-1]);
AB_max[ab[1]-1] = max(AB_max[ab[1]-1], H[ab[0]-1]);
}
int ans;
foreach (i; 0 .. N) {
if (AB_max[i] < H[i]) ans++;
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
long gd(long x,long y){
return y ? gd(y,x % y) : x;
}
void main(){
while(true){
long x,y,z;
long x1,y1,z1;
long l,m,n;
auto s = readln().split().to!(long[]);
if(s[0] == 0 && s[1] == 0 && s[2] == 0 && s[3] == 0 && s[4] == 0 && s[5] == 0 ) break;
x1 = s[0];
l = s[1];
y1 = s[2];
m = s[3];
z1 = s[4];
n = s[5];
x = x1 % l;
y = y1 % m;
z = z1 % n;
long ans;
long a,b,c;
a = 1;
b = 1;
c = 1;
while(x != 1){
x = (x1 * x) % l;
a++;
}
while(y != 1){
y = (y1 * y) % m;
b++;
}
while(z != 1){
z = (z1 * z) % n;
c++;
}
ans = (a * b) / gd(a,b);
ans = (ans * c) / gd(ans,c);
writeln(ans);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto S = RD!string;
/*long N = 2*(10^^5);
auto SS = new char[][](1);
foreach (i; 0..1)
{
SS[i] = new char[](N);
foreach (j; 0..N)
SS[i][j] = dice(0.5, 0.5) == 0 ? '.' : '#';
}
foreach (S; SS)
{ writeln(S);*/
long[] cnt = [0];
char last = S[0];
foreach (i; 0..N)
{
if (S[i] == last)
{
++cnt[$-1];
}
else
{
cnt ~= 1;
last = S[i];
}
}
debug writeln(cnt);
auto b_pos = S[0] == '#' ? 0 : 1;
auto b = new long[](cnt.length+100);
auto w = new long[](cnt.length+100);
foreach (i, e; cnt)
{
b[i+1] = b[i];
if (i % 2 == b_pos)
b[i+1] += e;
}
foreach_reverse (i, e; cnt)
{
w[i] = w[i+1];
if (i % 2 != b_pos)
w[i] += e;
}
long ans = long.max;
foreach (i; 0..cnt.length+1)
{
ans = min(ans, w[i] + b[i]);
}
writeln(ans);
stdout.flush();
//}
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
readln;
readln.split.reverse.join(" ").writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
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;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, M;
scan(N, M);
auto e = new bool[][](N, N);
foreach (i ; 0 .. M) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
e[ai][bi] = 1;
e[bi][ai] = 1;
}
auto p = iota(N).array;
int ans;
do {
ans += isValid(e, p);
} while (nextPermutation(p));
ans.writeln;
}
bool isValid(bool[][] e, int[] p) {
if (p[0] != 0) {
return false;
}
foreach (i ; 0 .. p.length.to!int - 1) {
int u = p[i], v = p[i + 1];
if (!e[u][v]) {
return false;
}
}
return true;
}
|
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 = new int[][](N, N);
foreach (i; 0..N) A[i][] = -1;
foreach (i; 0..N) {
int a = readln.chomp.to!int;
foreach (j; 0..a) {
auto s = readln.split.map!(to!int);
auto x = s[0] - 1;
auto y = s[1];
A[i][x] = y;
}
}
int ans = 0;
foreach (mask; 0..1<<N) {
bool ok = true;
foreach (i; 0..N) {
if (mask & (1 << i)) {
foreach (j, a; A[i]) {
if (a == 0) {
if (mask & (1 << j)) {
ok = false;
break;
}
} else if (a == 1) {
if (!(mask & (1 << j))) {
ok = false;
break;
}
}
}
if (!ok) {
break;
}
}
}
if (ok) ans = max(ans, mask.popcnt);
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
int c = 0;
while(true){
auto s = split(readln());
int a = to!int(s[0]);
int b = to!int(s[1]);
if(a==0&&b==0) break;
bool ifna = true;
if(c)
writeln("");
c++;
for(int i=a;i<=b;i++){
bool flg = false;
if(i%4==0){
if(i%100==0){
if(i%400==0) flg = true;
}else flg = true;
}
if(flg){
writeln(i);
ifna = false;
}
}
if(ifna) writeln("NA");
}
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long n, k;
scan(n, k);
writeln(k * (k - 1) ^^ (n - 1));
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
void main() {
(rs.count("o")*100 + 700).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.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 w = RD!string;
auto cnt = new long[](26);
foreach (e; w)
++cnt[e-'a'];
bool ans = true;
foreach (e; cnt)
if (e % 2 != 0)
ans = false;
writeln(ans ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
void main()
{
auto reader = readln.split;
int A = reader[0].to!int;
int B = reader[1].to!int;
int T = reader[2].to!int;
writeln(T / A * B);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, core.bitop;
void main()
{
auto N = readln.chomp.to!long;
auto T = new int[][](N, N);
foreach (i; 1..N.to!int) {
auto ab = readln.split.to!(int[]);
auto a = ab[0]-1;
auto b = ab[1]-1;
T[a][b] = i;
T[b][a] = i;
}
auto M = readln.chomp.to!long;
auto RS = new ulong[](M);
foreach (i; 0..M) {
auto uv = readln.split.to!(int[]);
auto u = uv[0]-1;
auto v = uv[1]-1;
int[] walk(int i, int p) {
if (i == v) return [-1];
foreach (j; 0..N.to!int) if (j != p && T[i][j]) {
auto s = walk(j, i);
if (!s.empty) {
return s ~ T[i][j];
}
}
return [];
}
foreach (e; walk(u, -1)[1..$]) RS[i] |= (1L<<e);
}
long res = 2L^^(N-1);
foreach (x; 1..(1<<M)) {
ulong s;
foreach (i; 0..M) if (x & (1<<i)) s |= RS[i];
res += 2L^^(N-1-popcnt(s)) * (-1)^^popcnt(x);
}
writeln(res);
}
|
D
|
void main()
{
long k = rdElem - 1;
long[] a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51];
a[k].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
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
void dfs(long len, long m, string s)
{
if (len == N)
{
writeln(s);
return;
}
foreach (x; 0 .. m)
dfs(len + 1, m, s ~ cast(char)('a' + x));
dfs(len + 1, m + 1, s ~ cast(char)('a' + m));
}
dfs(0, 0, "");
}
|
D
|
import std.stdio;
import std.string, std.conv, std.array, std.algorithm;
import std.uni, std.math, std.container;
import core.bitop, std.datetime;
void main(){
auto N = readln.chomp.to!int;
auto A = readln.split.to!(int[]);
auto ans = int.max;
foreach(x; -100 .. 101){
ans = min(ans, A.map!(a => (a - x)^^2).sum);
}
ans.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = new long[](N);
foreach (i; 0 .. N)
{
A[i] = lread();
}
if (A[0] != 0)
{
writeln(-1);
return;
}
long ans;
foreach_reverse (i; 1 .. N)
{
long diff = A[i] - A[i - 1];
if (diff == 1)
{
ans++;
continue;
}
if (1 < diff)
{
writeln(-1);
return;
}
ans += A[i];
}
writeln(ans);
}
|
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 nm = readln.chomp.split.map!(to!long);
long n = nm[0];
long m = nm[1];
long atom = min(n, m / 2);
m -= atom * 2;
long next = m / 4;
writeln = atom + next;
}
|
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 X = RD;
auto A = RD;
auto B = RD;
writeln(B <= A ? "delicious" : B - A <= X ? "safe" : "dangerous");
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long A, B, C;
scan(A, B, C);
bool b = B - A == C - B;
writeln(b?"YES":"NO");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
writeln("YES");
if (A.reduce!gcd > 1) {
writeln(0);
return;
}
int ans = 0;
foreach (i; 0..N) {
if (A[i] % 2 == 0) continue;
else if (i == N-1) ans += 2;
else if (A[i+1] % 2 == 0) ans += 2;
else {
ans += 1;
A[i+1] = 2;
}
}
ans.writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int a, b; readV(a, b);
auto c = new bool[][](100, 100);
foreach (i; 50..100) c[i][] = true;
foreach (i; 0..a-1) {
auto y = i/50*2, x = i%50*2;
c[y][x] = true;
}
foreach (i; 0..b-1) {
auto y = i/50*2+51, x = i%50*2;
c[y][x] = false;
}
writeln("100 100");
foreach (i; 0..100) {
foreach (j; 0..100)
write(c[i][j] ? "." : "#");
writeln;
}
}
|
D
|
import std;
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}
T[] lreads(T=long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}
void arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;}
void scan(TList...)(ref TList Args){auto line = readln.split;
foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}}
void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length);
static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}}
alias sread=()=>readln.chomp();enum MOD =10^^9+7;
alias PQueue(T,alias less="a<b") = BinaryHeap!(Array!T,less);
// dfmt on
void main()
{
long A, B, C, K;
scan(A, B, C, K);
long ans;
ans += min(K, A);
K -= min(K, A);
K -= min(K, B);
ans -= min(K, C);
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main() {
string input;
while ((input = readln.chomp).length != 0) {
auto line = input.map!(a => a.to!int - '0').array;
while (line.length > 1) {
for (int i = 0; i < (line.length - 1); i++) {
line[i] = line[i] + line[i+1];
}
line.popBack;
}
writeln(line.front % 10);
}
}
|
D
|
import std.stdio,std.string,std.conv,std.algorithm;
int main()
{
int n = readln.chomp.to!int;
while(n--)
{
string s = readln.chomp;
int[] a;
a.length = s.length;
foreach(i;0..s.length)
{
a[i] = cast(int)(s[i] - '0');
}
sort(a);
int count = 1;
int num_max = 0,num_min = 0;
foreach(i;0..a.length)
{
num_max += a[i]*count;
num_min += a[a.length - i -1]*count;
count *= 10;
}
writeln(num_max - num_min);
}
return 0;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new string[](t);
foreach (ti; 0..t)
{
auto S = RD!string;
auto T = RD!string;
auto cnt = new long[](26);
foreach (c; S)
++cnt[c-'a'];
if (T == "abc")
{
if (cnt[0] == 0 || cnt[1] == 0 || cnt[2] == 0)
{
foreach (i; 0..26)
foreach (_; 0..cnt[i])
ans[ti] ~= cast(char)('a'+i);
}
else
{
foreach (_; 0..cnt[0])
ans[ti] ~= "a";
foreach (_; 0..cnt[2])
ans[ti] ~= "c";
foreach (_; 0..cnt[1])
ans[ti] ~= "b";
foreach (i; 3..26)
foreach (_; 0..cnt[i])
ans[ti] ~= cast(char)('a'+i);
}
}
else
{
foreach (i; 0..26)
foreach (_; 0..cnt[i])
ans[ti] ~= cast(char)('a'+i);
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto S = RD!string;
auto T = RD!string;
string ans;
foreach (i; 0..S.length)
{
ans ~= [S[i], T[i]];
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] h; readA(n, h);
auto dp = new int[](n);
dp[0] = 0; dp[1] = (h[1]-h[0]).abs;
foreach (i; 2..n)
dp[i] = min(dp[i-1] + (h[i]-h[i-1]).abs, dp[i-2] + (h[i]-h[i-2]).abs);
writeln(dp[$-1]);
}
|
D
|
void main() {
long[] tmp = readln.split.to!(long[]);
long a = tmp[0], b = tmp[1], x = tmp[2];
writeln(b / x - (a == 0 ? -1 : (a - 1) / x));
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int[] reads() { return readln.split.to!(int[]); }
int f(int x) {
int t;
while(x%2 == 0) x/=2, t++;
return t;
}
void main() {
readln;
writeln(reads.map!(f).sum);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto X = s[1] - 1;
auto Y = s[2] - 1;
auto D = new int[][](N, N);
foreach (i; 0..N) foreach (j; 0..N) D[i][j] = abs(i - j);
D[X][Y] = D[Y][X] = 1;
foreach (i; 0..N) foreach (j; 0..N) D[i][j] = min(D[i][j], abs(i - X) + 1 + abs(j - Y));
foreach (i; 0..N) foreach (j; 0..N) D[i][j] = min(D[i][j], abs(i - Y) + 1 + abs(j - X));
auto ans = new int[](N+1);
foreach (i; 0..N) foreach (j; i+1..N) ans[D[i][j]] += 1;
ans[1..N].each!writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
max(0, scanElem-scanElem).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 A, B, C;
scan(A, B, C);
bool b = C <= A + B;
writeln(b ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
struct UFTree(T)
{
struct Node
{
T parent;
T rank = 1;
}
///
T min_size, max_size;
///
this(T n)
{
nodes.length = n;
sizes.length = n;
foreach (i, ref node; nodes) {
node = Node(i.to!T);
sizes[i] = 1;
}
min_sizes.length = n + 1;
min_sizes[1] = n;
min_size = 1;
max_size = 1;
}
///
bool unite(T a, T b)
{
a = root(a);
b = root(b);
if (a == b) return false;
auto a_size = sizes[a];
auto b_size = sizes[b];
--min_sizes[a_size];
--min_sizes[b_size];
++min_sizes[a_size + b_size];
foreach (nxt_size; min(a_size, b_size)..min_sizes.length) if (min_sizes[nxt_size] != 0) {
min_size = nxt_size.to!T;
break;
}
max_size = max(max_size, a_size + b_size);
if (nodes[a].rank < nodes[b].rank) {
sizes[a] += sizes[b];
nodes[b].parent = a;
} else {
sizes[b] += sizes[a];
nodes[a].parent = b;
if (nodes[a].rank == nodes[b].rank) ++nodes[b].rank;
}
return true;
}
///
bool is_same(T a, T b)
{
return root(a) == root(b);
}
///
T size(T i)
{
return sizes[root(i)];
}
private:
Node[] nodes;
T[] sizes;
T[] min_sizes;
T root(T i)
{
if (nodes[i].parent == i) return i;
return nodes[i].parent = root(nodes[i].parent);
}
}
///
UFTree!T uftree(T)(T n)
{
return UFTree!T(n);
}
void main()
{
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
long[] as, bs;
foreach (_; 0..M) {
auto ab = readln.split.to!(long[]);
as ~= ab[0]-1;
bs ~= ab[1]-1;
}
auto uft = uftree(N);
auto r = N * (N-1) / 2;
long[] rs;
foreach_reverse (i; 0..M) {
rs ~= r;
auto a = as[i];
auto b = bs[i];
if (uft.is_same(a, b)) continue;
r -= uft.size(a) * uft.size(b);
uft.unite(a, b);
}
foreach_reverse (rr; rs) writeln(rr);
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!long;
const K = scan!long;
long solve() {
long ans;
long[long] counts;
long minimal = 0;
long maximam = N;
foreach(i; 1..N+2) {
counts[i] = maximam - minimal + 1;
minimal += i;
maximam += (N - i);
}
foreach(n; K..N+2) {
ans = (ans + counts[n]) % MOD;
}
return ans;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
const MOD = 100_000_000_7;
// -----------------------------------------------
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.