code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(int[]);
auto K = nk[1]-1;
auto S = readln.chomp.to!(char[]);
if (S[K] == 'A') {
S[K] = 'a';
} else if (S[K] == 'B') {
S[K] = 'b';
} else if (S[K] == 'C') {
S[K] = 'c';
}
writeln(S);
}
|
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() {
auto s = readln.chomp;
int ans;
foreach (i ; 0 .. s.length) {
foreach (j ; i + 1 .. s.length + 1) {
if (s[i .. j].all!(ch => "ACGT".canFind(ch))) {
ans = max(ans, j.to!int - i.to!int);
}
}
}
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.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, k;
scan(n, k);
bool ok = (n + 1) / 2 >= k;
auto ans = ok ? "YES" : "NO";
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main(){
string[] weather = ["Sunny", "Cloudy", "Rainy", "Sunny"];
string now = readln().chomp();
weather = weather.find(now);
weather[1].writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
string[] board = ["#", "."];
while (true) {
int[] tmp = readln.chomp.split.to!(int[]);
int h = tmp[0], w = tmp[1];
if (h == 0 && w == 0) break;
foreach (i; 0 .. h) {
foreach (j; 0 .. w) {
write(board[(i+j)%2]);
}
writeln;
}
writeln;
}
}
|
D
|
void main()
{
auto S = rs;
auto ltarr = new int[](S.length); // <
auto gtarr = new int[](S.length); // >
ltarr[0] = S[0] == '<' ? 1 : 0;
foreach (i; 1 .. S.length)
{
if (S[i] == '<')
ltarr[i] = ltarr[i - 1] + 1;
else
ltarr[i] = 0;
}
gtarr[$ - 1] = S[$ - 1] == '>' ? 1 : 0;
foreach_reverse (i; 0 .. S.length - 1)
{
if (S[i] == '>')
gtarr[i] = gtarr[i + 1] + 1;
else
gtarr[i] = 0;
}
debug gtarr.writeln;
debug ltarr.writeln;
ulong res;
res += gtarr[0];
foreach (i; 1 .. S.length)
{
res += max(ltarr[i - 1], gtarr[i]);
}
res += ltarr[$ - 1];
res.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;
}
long rl()
{
return readAs!long;
}
double rd()
{
return readAs!double;
}
string rs()
{
return readln.chomp;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
long P = 10^^9+7;
long[1001] F, RF;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
long inv(long x)
{
return pow(x, P-2);
}
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
long perm(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n];
auto n_k_b = RF[n-k];
return (n_b * n_k_b) % P;
}
void main()
{
init();
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
auto DP = new long[][](N+1, K+1);
DP[1][1] = 1;
foreach (n; 2..N+1) {
foreach (k; 1..K+1) {
DP[n][k] = (DP[n-1][k-1] + k * DP[n-1][k] % P) % P;
}
}
writeln(DP[N][K]);
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
int n;
scan(n);
auto ans = (n + 999) / 1000 * 1000 - n;
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
long m, a, r, c, h;
foreach (_; 0..N) {
switch (readln[0]) {
case 'M': ++m; break;
case 'A': ++a; break;
case 'R': ++r; break;
case 'C': ++c; break;
case 'H': ++h; break;
default:
}
}
writeln(
m*a*r + m*a*c + m*a*h + m*r*c + m*r*h + m*c*h +
a*r*c + a*r*h + a*c*h +
r*c*h
);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.replace(",", " "));
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void main(){
auto ip = readln.split.to!(int[]);
if(ip[0] * ip[1] % 2 == 0){
writeln("Even");
} else {
writeln("Odd");
}
}
|
D
|
import std.array : split;
import std.conv : to;
import std.stdio;
import std.string : strip;
private {
string[] temp;
}
void main() {
read();
int n = get!int(0);
int[] h = new int[n];
int[] a = new int[n];
foreach (i; 0 .. n) {
read();
h[i] = get!int(0);
a[i] = get!int(1);
}
int count = 0;
foreach (i, hi; h) {
foreach (j, aj; a) {
if (i != j && hi == aj)
++count;
}
}
stdout.write(count);
}
void read() {
temp = split(strip(stdin.readln()));
}
T get(T)(int p) {
return to!(T)(temp[p]);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto a = readln.chomp.to!int;
auto s = readln.chomp;
writeln(a >= 3200 ? s : "red");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Clue = Tuple!(long, "x", long, "y", long, "h");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
long cnt;
void main()
{
auto ary = new long[](MAX);
auto is_prime = new bool[](MAX);
is_prime[] = true;
is_prime[0] = is_prime[1] = false;
foreach (i; iota(MAX))
{
if (!is_prime[i])
continue;
foreach (j; iota(0, MAX, i))
{
is_prime[j] = false;
}
is_prime[i] = true;
}
foreach (i; iota(1, MAX))
ary[i] = ary[i - 1] + (is_prime[i] && is_prime[(i + 1) / 2] ? 1 : 0);
auto q = lread();
foreach (i; iota(q))
{
long l, r;
scan(l, r);
writeln(ary[r] - ary[l - 1]);
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
const MOD = 10^^9 + 7L;
void add(T)(ref T a, T b) { a = (a + b) % MOD; }
long calc(string s, int d) {
int n = cast(int) s.length;
auto dp = new int[][][](n + 1, 2, d);
// dp[i][j][k]
// i: 上から i 桁目まで
// j: n 未満か
// k: mod d
dp[0][0][0] = 1;
foreach (i; 0..n) {
foreach (j; 0..2) {
foreach (k; 0..d) {
int lim = j == 1 ? 9 : s[i] - '0';
foreach (m; 0..lim+1) {
add(dp[i + 1][j || m < lim][(k + m) % d], dp[i][j][k]);
}
}
}
}
auto ans = (-1L + MOD + dp[n][0][0] + dp[n][1][0]) % MOD;
return ans;
// return ans - 1; // -1 は 0 の分
}
void main() {
string s = read!string;
int d = readint;
writeln(calc(s, d) - calc("0", d));
}
|
D
|
import std;
long calc(string s) {
int n = cast(int)s.length;
auto acc = new int[][](3, n);
foreach (i; 0..n) {
acc[0][i] = (s[i] == 'R') + (i > 0 ? acc[0][i-1] : 0);
acc[1][i] = (s[i] == 'G') + (i > 0 ? acc[1][i-1] : 0);
acc[2][i] = (s[i] == 'B') + (i > 0 ? acc[2][i-1] : 0);
}
// "RGB" の使っていない文字
int get(char a, char b) {
foreach (i, c; "RGB") {
if (c != a && c != b) return cast(int)i;
}
assert(false);
}
long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n - 1; j++) {
if (s[i] == s[j]) continue;
int p = get(s[i], s[j]);
ans += acc[p][$-1] - acc[p][j-1];
int k = 2*j - i;
if (k < n && s[k] == "RGB"[p]) ans--;
}
}
return ans;
}
void main() {
readint;
string s = read!string;
writeln(calc(s));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import 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, std.datetime;
// 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 K, T;
scan(K, T);
auto A = aryread();
long ans = 0;
foreach (i; 0 .. T)
ans = ans.max(A[i] - 1 - (K - A[i]));
writeln(ans);
}
|
D
|
import std.conv;
import std.stdio;
import std.array;
import std.range;
import std.string;
import std.algorithm;
void main()
{
while(true)
{
auto n = readln.chomp.to!int;
if(n == 0) break;
auto hs = readln.split.map!(a => a.to!int()).array;
foreach(i; 0..(n+1)) {
if(isCommonDiff(hs[0..i] ~ hs[(i+1)..(n+1)])) {
writeln(hs[i]);
break;
}
}
}
}
bool isCommonDiff(int[] ary)
{
auto n = ary[1] - ary[0];
foreach(i; 2..ary.length) {
if(ary[i] - ary[i-1] != n) {
return false;
}
}
return true;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
foreach (test; 0..readln.strip.to !(int))
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
auto num = new int [] [] (n, n + 1);
foreach (i; 0..n)
{
foreach (j; 0..n)
{
num[i][j + 1] = num[i][j] + (a[i] == a[j]);
}
}
long res = 0;
foreach (i; 1..n)
{
foreach (j; i + 1..n - 1)
{
res += num[j][i] *
(num[i][n] - num[i][j + 1]);
}
}
writeln (res);
}
}
|
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[] a; readA(n, a);
auto dp = new long[][](n, n);
foreach (i; 0..n)
dp[i][i] = (n%2 == 1 ? 1 : -1) * a[i];
foreach (j; 0..n)
foreach_reverse (i; 0..j)
if ((n-(j-i+1))%2 == 0)
dp[i][j] = max(dp[i+1][j]+a[i], dp[i][j-1]+a[j]);
else
dp[i][j] = min(dp[i+1][j]-a[i], dp[i][j-1]-a[j]);
writeln(dp[0][n-1]);
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long X = lread();
if (X <= 6)
{
writeln(1);
return;
}
long ans = X / (5 + 6) * 2;
long x = X % (5 + 6);
foreach (i; 0 .. 100)
{
if (x <= 0)
{
writeln(ans);
return;
}
x -= (i & 1) ? 5 : 6;
ans++;
}
}
|
D
|
import std;
void main() {
int h1, m1, h2, m2, k; scan(h1, m1, h2, m2, k);
int a = h1 * 60 + m1;
int b = h2 * 60 + m2;
writeln(max(0, b - a - k));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
import std.stdio, std.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 modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto n = RD!int;
auto s = RD!string;
int l, cnt, ans;
foreach (i; 0..n)
{
if (s[i] == ')')
--cnt;
else
{
if (cnt == -1)
{
ans += i - l + 1;
}
++cnt;
}
if (cnt == 0)
{
l = i+1;
}
}
if (cnt != 0)
writeln(-1);
else
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
void main() {
uint n = readln.strip.to!int;
auto s = readln[0 .. n];
auto l = s.count ('L');
auto r = s.count ('R');
writeln (l + r + 1);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.map!(to!int), a = rd[0], b = rd[1];
if (a == b) writeln("a == b");
else writeln(a > b ? "a > b" : "a < b");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
bool[int] buf;
foreach (_; 0..n) {
auto a = readln.chomp.to!int;
if (a in buf) {
buf.remove(a);
} else {
buf[a] = true;
}
}
writeln(buf.length);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
int[10^^5] PXS, MXS;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
int i, j;
foreach (x; readln.split.to!(int[])) {
if (x > 0) {
PXS[i++] = x;
} else if (x < 0) {
MXS[j++] = -x;
} else {
--K;
}
}
reverse(MXS[0..j]);
int l, r;
if (j > K) {
l = K - 1;
r = -1;
} else {
l = j - 1;
r = K - j - 1;
}
auto min_ret = int.max;
while (r < K && r < i && l >= -1) {
auto ld = l == -1 ? 0 : MXS[l];
auto rd = r == -1 ? 0 : PXS[r];
auto ret = ld > rd ? ld + rd * 2 : rd + ld * 2;
min_ret = min(min_ret, ret);
--l;
++r;
}
writeln(min_ret);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto w=new int[](n), h=new int[](n);
foreach(i; 0..n) rd(w[i], h[i]);
int cur=1_000_000_000+1;
foreach(i; 0..n){
if(cur<min(w[i], h[i])){
writeln("NO");
return;
}
if(cur>=max(w[i], h[i])) cur=max(w[i], h[i]);
else cur=min(w[i], h[i]);
}
writeln("YES");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1L << 50;
void main() {
int n, k;
scan(n, k);
auto h = readln.split.to!(int[]);
auto dp = new long[](n + 1);
foreach (i ; 1 .. n) {
long t = inf;
foreach (j ; max(0, i - k) .. i) {
t = min(t, dp[j] + abs(h[i] - h[j]));
}
dp[i] = t;
}
writeln(dp[n-1]);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.math;
import std.string;
void main()
{
int[] addinput = new int[200];
int anslength;
string s;
int i = -1;
while( (s=readln()).length > 1 )
{
i ++;
anslength = i;
string[] input = split(s);
int a = to!int(input[0]);
int b = to!int(input[1]);
addinput[i] = cast(int)log10(a + b) + 1;
}
for(int j = 0; j <= anslength; j++)
{
writeln(addinput[j]);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
int n = readln.strip.to!int;
auto s = readln.strip;
foreach (c; s) {
auto i = c.to!int - 65;
i = (i + n) % 26;
write((65+i).to!char);
}
writeln;
}
|
D
|
void main()
{
long n = rdElem;
long k = rdElem;
long[] x = rdRow;
long total;
foreach (y; x)
{
total += 2 * min(y, k-y);
}
total.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "damage", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long h, n;
scan(h, n);
auto magic = new Pair[](n);
foreach (ref e; magic)
{
long a, b;
scan(a, b);
e = Pair(a, b);
}
auto d = new long[](MAX);
d[] = INF;
d[0] = 0;
foreach (i; iota(h + 1))
{
foreach(j; iota(n))
{
d[i + magic[j].damage] = min(d[i] + magic[j].cost, d[i + magic[j].damage]);
}
}
long ans = INF;
foreach (i; iota(h, MAX))
{
ans = min(d[i], ans);
}
ans.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split, n = rd[0].to!size_t, a = rd[1].to!long, b = rd[2].to!long, c = a-b;
auto h = new long[](n);
foreach (i; 0..n) h[i] = readln.chomp.to!long;
auto calc(long x)
{
auto y = 0;
foreach (hi; h) {
auto g = hi - b * x;
if (g > 0) {
y += (g + c - 1) / c;
if (y > x) return false;
}
}
return true;
}
struct XR { long x; bool r; }
auto ma = (h.reduce!max + b - 1) / b;
auto r = iota(1L, ma+1)
.map!(x => XR(x, calc(x)))
.assumeSorted!"a.r < b.r"
.equalRange(XR(0L, true))
.front.x;
writeln(r);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto s = readln.chomp;
auto nc = s.count('N');
auto wc = s.count('W');
auto sc = s.count('S');
auto ec = s.count('E');
auto f = true;
if ((!nc && sc) || (nc && !sc)) {
f = false;
}
if ((!wc && ec) || (wc && !ec)) {
f = false;
}
if (f) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
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, std.numeric;
} else {
import std;
}
// }}}
// 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(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// ModInt {{{
struct ModInt(ulong modulus) {
import std.traits : isIntegral, isBoolean;
import std.exception : enforce;
private {
ulong val;
}
this(const ulong n) {
val = n % modulus;
}
this(const ModInt n) {
val = n.value;
}
@property {
ref inout(ulong) value() inout {
return val;
}
}
T opCast(T)() {
static if (isIntegral!T) {
return cast(T)(val);
} else if (isBoolean!T) {
return val != 0;
} else {
enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ ".");
}
}
ModInt opAssign(const ulong n) {
val = n % modulus;
return this;
}
ModInt opOpAssign(string op)(ModInt rhs) {
static if (op == "+") {
val += rhs.value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs.value) {
val += modulus;
}
val -= rhs.value;
} else if (op == "*") {
val = val * rhs.value % modulus;
} else if (op == "/") {
this *= rhs.inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs.value > 0) {
if (rhs.value % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opOpAssign(string op)(ulong rhs) {
static if (op == "+") {
val += ModInt(rhs).value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
auto r = ModInt(rhs);
if (val < r.value) {
val += modulus;
}
val -= r.value;
} else if (op == "*") {
val = val * ModInt(rhs).value % modulus;
} else if (op == "/") {
this *= ModInt(rhs).inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs > 0) {
if (rhs % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opUnary(string op)() {
static if (op == "++") {
this += 1;
} else if (op == "--") {
this -= 1;
} else {
enforce(false, op ~ " is not implemented.");
}
return this;
}
ModInt opBinary(string op)(const ulong rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(ref const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinaryRight(string op)(const ulong lhs) const {
mixin("return ModInt(this) " ~ op ~ "= lhs;");
}
long opCmp(ref const ModInt rhs) const {
return cast(long)value - cast(long)rhs.value;
}
bool opEquals(const ulong rhs) const {
return value == ModInt(rhs).value;
}
ModInt inv() const {
ModInt ret = this;
ret ^^= modulus - 2;
return ret;
}
string toString() const {
import std.format : format;
return format("%s", val);
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// 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!(Array!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);
int[][] h = new int[][](3, n);
foreach (i; 0 .. n) {
cin.scan(h[0][i], h[1][i], h[2][i]);
}
int[][] dp = new int[][](n + 1, 3);
foreach (i; 0 .. n) {
foreach (j; 0 .. 3) {
foreach (k; 0 .. 3) {
if (j == k) continue;
dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + h[k][i]);
}
}
}
writeln(max(dp[n][0], max(dp[n][1], dp[n][2])));
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b; readV(a, b);
auto c = b-a;
writeln(c*(c+1)/2-b);
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string S;
sc.scan(S);
int min = abs(753 - to!int(S[0 .. 3]));
foreach (i; 1 .. S.length - 2) {
if (min > abs(753 - to!int(S[i .. i + 3])))
min = abs(753 - to!int(S[i .. i + 3]));
}
writeln(min);
}
|
D
|
import std.stdio, std.string, std.algorithm, std.range, std.conv;
void main(){
while(true){
auto input = readln.split;
int n = input[0].to!int, k = input[1].to!int;
if(n == 0 && k == 0) break;
auto G = new int[][](n + 1, n + 1);
foreach(ref g; G){
g[] = int.max;
}
foreach(Unused; 0..k){
input = readln.split;
int op = input[0].to!int;
if(op == 1){
int a = input[1].to!int, b = input[2].to!int, cost = input[3].to!int;
if(cost < G[a][b]){
G[a][b] = cost;
G[b][a] = cost;
}
}else if(op == 0){
int start = input[1].to!int, end = input[2].to!int;
auto d = new int[](n + 1);
auto MST = new bool[](n + 1);
d[] = int.max;
d[start] = 0;
while(true){
int min = int.max;
int u;
foreach(i; 1..n + 1){
if(!MST[i] && min > d[i]){
min = d[i];
u = i;
}
}
if(u == 0) break;
MST[u] = true;
foreach(v; 1..n + 1){
if(!MST[v] && G[u][v] != int.max && G[u][v] + d[u] < d[v]){
d[v] = G[u][v] + d[u];
}
}
}
if(d[end] == int.max) (-1).writeln;
else d[end].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;
// 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();
auto A = aryread!ulong();
ulong ans;
long[62][2] cnt;
foreach (ulong i; 0 .. 61)
{
foreach (j; 0 .. N)
{
cnt[(A[j] & (1UL << i)) != 0][i]++;
}
}
foreach (ulong i; 0 .. 61)
{
foreach (j; 0 .. N)
{
ans += ((1UL << i) % MOD) * cnt[(A[j] & (1UL << i)) == 0][i];
ans %= MOD;
}
}
writeln(ans * invmod!ulong(2) % MOD);
}
// x^^n % m
T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7)
{
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1, m) % m;
}
T tmp = powmod(x, n / 2, m);
return tmp * tmp % m;
}
T invmod(T = long)(T x, T m = 10 ^^ 9 + 7)
{
return powmod(x, m - 2, m);
}
|
D
|
unittest
{
assert( [ "atcoder beginner contest" ].parse.expand.solve == "ABC" );
assert( [ "resident register number" ].parse.expand.solve == "RRN" );
assert( [ "k nearest neighbor" ].parse.expand.solve == "KNN" );
assert( [ "async layered coding" ].parse.expand.solve == "ALC" );
}
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto ss = input.front.split;
return tuple( ss );
}
auto solve( string[] ss )
{
auto r = "";
foreach( s; ss )
{
r ~= s[ 0 ].toUpper;
}
return r;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (true) {
int n, p;
scan(n, p);
if (n == 0 && p == 0) return;
auto m = new int[](n);
int pos;
int wan = p;
while (true) {
if (wan == 0) {
wan += m[pos];
m[pos] = 0;
}
else {
wan--;
m[pos]++;
}
if (m[pos] == p) break;
(++pos) %= n;
}
writeln(pos);
}
}
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.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional;
void main() {
auto tmp = readln.split.to!(int[]);
auto A = tmp[0], B = tmp[1];
iota(1,4).any!(C => A * B * C % 2 == 1).pipe!(b => b ? "Yes" : "No").writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias CSF = Tuple!(long, "c", long, "s", long, "f");
void main()
{
auto N = readln.chomp.to!int;
CSF[] ss;
foreach (_; 0..N-1) {
auto csf = readln.split.to!(long[]);
ss ~= CSF(csf[0], csf[1], csf[2]);
}
long[] rs;
foreach (i; 0..N-1) {
long s;
foreach (j; i..N-1) {
s = max(s, ss[j].s);
s += ss[j].c + ((s%ss[j].f == 0) ? 0 : (ss[j].f - s%ss[j].f));
}
rs ~= s;
}
rs ~= 0;
foreach (r; rs) writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
long K = nk[1];
auto AS = readln.split.to!(int[]);
long[] xs, ys;
xs.length = N;
ys.length = N;
foreach (i, a; AS) {
long x, y;
foreach (j, b; AS) {
if (b > a && j < i) ++x;
if (b > a) ++y;
}
xs[i] = x;
ys[i] = y;
}
long r;
auto k = (K * (K - 1) / 2) % P;
foreach (i; 0..N) {
r = (r + (K *xs[i]) % P + (k * ys[i]) % P) % P;
}
writeln(r);
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
struct Input
{
int h;
int n;
int[] a;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto array = readln().strip().split();
h = array[0].to!int;
n = array[1].to!int;
a = readln().strip().split().map!(to!int).array;
}
}
struct Output
{
}
auto main2(Input* input)
{
auto sum = sum(input.a);
return input.h <= sum ? "Yes" : "No";
}
unittest { writeln("begin unittest"); }
unittest // example1
{
string example =
`10 3
4 5 6`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == "Yes");
}
unittest // example2
{
string example =
`20 3
4 5 6`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == "No");
}
unittest // example3
{
string example =
`211 5
31 41 59 26 53`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == "No");
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
void main()
{
Input input = void;
parseInput(input, stdin);
auto result = main2(&input);
writeln(result);
}
|
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;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
string s = readln.chomp;
long w = read.to!long;
string ans;
foreach(i, c; s){
if(i % w == 0) ans ~= c;
}
ans.writeln;
}
|
D
|
import std.stdio : readln, writeln;
import std.array : split;
import std.conv : to;
import std.typecons;
import std.algorithm : map;
import std.string : chomp;
char[Tuple!(int, int)] field;
int W, H;
void dfs(int x, int y, char f) {
field[tuple(x, y)] = '.';
foreach (dx; [-1, 1]) {
int nx = x + dx;
if (0 <= nx && nx < W
&& field[tuple(nx, y)] == f) dfs(nx, y, f);
}
foreach (dy; [-1, 1]) {
int ny = y + dy;
if (0 <= ny && ny < H
&& field[tuple(x, ny)] == f) dfs(x, ny, f);
}
}
void main() {
while (1) {
auto input = map!(to!int)(split(chomp(readln()))[0 .. 2]);
H = input[0], W = input[1];
if (W == 0 && H == 0) break;
field = field.init;
foreach (int y; 0 .. H)
foreach (int x, char f; readln()[0 .. W])
field[tuple(x, y)] = f;
int res = 0;
foreach (xy; field.keys) {
char f = field[tuple(xy[0], xy[1])];
switch (f) {
case '#': case '@': case '*':
dfs(xy[0], xy[1], f);
res++;
break;
default:
break;
}
}
writeln(res);
}
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;}
alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;}
struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}}
struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}}
//END OF TEMPLATE
void main(){
auto n=scan!ulong;
bool[string] s;
foreach(i;0..n)
s[scan!string()]=true;
s.keys.length.writeln;
}
|
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 S = N.iota.map!(_ => readln.chomp).array;
int[string] cnt;
foreach (s; S) {
bool[string] used;
foreach (i; 0..9) {
foreach (j; i..10) {
if (s[i..j] in used)
continue;
cnt[s[i..j]] += 1;
used[s[i..j]] = true;
}
}
}
void solve(int ind) {
foreach (len; 1..10)
foreach (i; 0..10-len)
if (cnt[S[ind][i..i+len]] == 1) {
writeln(S[ind][i..i+len]);
return;
}
}
foreach (i; 0..N)
solve(i);
}
|
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);
writeln(1 + iota(1, n).count!(i => h[0..i].all!(hi => hi <= h[i])));
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
int n;
cin.scan(n);
char[] s = cin.next!(char[]);
int[Tuple!(string, string)] chie;
reverse(s[n .. $]);
foreach (S; 0 .. 1 << n)
{
string a, b;
foreach (i; 0 .. n)
{
if ((S >> i) & 1)
{
a ~= s[i];
}
else
{
b ~= s[i];
}
}
chie[tuple(a, b)]++;
}
long ans;
foreach (S; 0 .. 1 << n)
{
string a, b;
foreach (i; 0 .. n)
{
if ((~S >> i) & 1)
{
a ~= s[i + n];
}
else
{
b ~= s[i + n];
}
}
if (tuple(b, a) in chie)
ans += chie[tuple(b, a)];
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
readln.chomp.count!"a=='1'".writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
int n;
cin.scan(n);
writeln("ABC" ~ n.to!string);
}
|
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 + 1);
foreach (i ; 0 .. m) {
int li, ri;
scan(li, ri);
li--;
cnt[li]++;
cnt[ri]--;
}
foreach (i ; 0 .. n) {
cnt[i + 1] += cnt[i];
}
int ans;
foreach (i ; 0 .. n + 1) {
if (cnt[i] == m) ans++;
}
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm;
long lcm(long x, long y) {
import std.numeric;
return x * y / gcd(x, y);
}
void main() {
long[] tmp = readln.split.to!(long[]);
long a = tmp[0] - 1, b = tmp[1], c = tmp[2], d = tmp[3];
long l = lcm(c, d);
long n = b - b / c - b / d + b / l;
long m = a - a / c - a / d + a / l;
writeln(n - m);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.algorithm;
import std.math;
import std.regex;
void main() {
auto s = readln.chomp.map!(a => a.to!int).array;
auto t = s.sort().uniq.array;
if(s.length != t.length) "no".writeln;
else "yes".writeln;
}
|
D
|
void main() {
auto S = readln.split;
int[] stack;
foreach(i; S) {
int num;
try {
num = i.to!int;
stack ~= num;
} catch(Exception e) {
auto a = stack.back;
stack.popBack;
auto b = stack.back;
stack.popBack;
stack ~= i.predSwitch("+", b+a, "-", b-a, "*", b*a);
}
}
stack.back.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto s = new int[](n);
foreach (i; 0..n) {
s[i] = readln.chomp.to!int;
}
s = sort(s).array;
int t;
foreach (i; 0..n) {
if (s[i] % 10) {
t = s[i];
break;
}
}
auto sum = s.sum;
if (sum % 10) {
sum.writeln;
} else if (t) {
writeln(sum - t);
} else {
writeln("0");
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
void main(){
int N, M, l, r, x;
readVars(N, M);
auto p = readln.split.to!(int[]);
int cnt;
foreach(i ; 0 .. M){
readVars(l, r, x);
l--;
x--;
cnt = 0;
foreach(j ; l .. r){
cnt += (p[j] < p[x]);
}
if (cnt == x - l) {
writeln("Yes");
} else {
writeln("No");
}
}
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
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 S = RD!string;
auto cnt = [0, 0, 0];
foreach (c; S)
{
auto i = c - 'a';
++cnt[i];
}
cnt.sort();
auto ans = cnt[2] - cnt[0] <= 1;
if (cnt[0] == 0 || cnt[1] == 0 || cnt[2] == 0)
ans = false;
if (S.length == 1)
writeln("YES");
else if (S.length == 2)
writeln(S[0] != S[1] ? "YES" : "NO");
else
writeln(ans ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
void solve(){
}
void main(){
string s = instr();
(700+s.count('o')*100).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
alias instr = () => readln().chomp();
T inelm(T= int)(){ return to!(T)( readln().chomp() ); }
T[] inary(T = int)(){ return readln().chomp().split().to!(T[])(); }
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
import std.range;
void main() {
auto w = readln.chomp.to!(dchar[]);
auto s = w.sort().uniq;
foreach(s_i; s) {
if(w.count(s_i) % 2 == 1) {
"No".writeln;
return;
}
}
"Yes".writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main(){
int[] str = readln.chomp.split.to!(int[]);
int time = str[0];
int qty = str[1];
int duration = str[2];
writeln(duration / time * qty);
}
|
D
|
void main()
{
long n = rdElem;
Graph[] g;
foreach (i; 1 .. n)
{
foreach (j; i+1 ..n+1)
{
if (i + j != (n + !(n & 1))) g ~= Graph(i, j);
}
}
g.length.writeln;
foreach (x; g)
{
writeln(x.a, " ", x.b);
}
}
struct Graph
{
long a, b;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
auto h = RDA;
long ans;
foreach (i; 0..N)
{
if (h[i] >= K)
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "C"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
long calc(long[] a) {
int n = a.length.to!int;
if (a[0]) return -1;
long sm = 0;
foreach (i; 1..n) {
if (a[i-1]+1 < a[i]) return -1;
if (a[i-1]+1 == a[i]) {
sm++;
} else {
sm += a[i];
}
}
return sm;
}
int main() {
Scanner sc = new Scanner(stdin);
int n;
sc.read(n);
long[] a = new long[n];
foreach (i; 0..n) sc.read(a[i]);
writeln(calc(a));
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
writeln(180 * (n - 2));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto Z=readln.split.to!(int[]),a=Z[0],b=Z[1];
if(a<=b)writeln(a);
else writeln(a-1);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
/// n 番目のフィボナッチ数を求める
/// F(0) = 0, F(1] = 1, F(2) = 1, F(3) = 2, F(4) = 3
int fib(int n) {
if (n == 0) return 0;
auto mat = new Mat2x2([[1, 1], [1, 0]]);
auto m = mat.pow(n, int.max);
return m.get(0, 1);
}
void main() {
int n = readint;
// 問題文の定義だとF(0) = 1, F(1) = 1 なので値を 1 だけずらす
writeln(fib(n + 1));
}
class Mat2x2 {
private const N = 2;
private const int[][] _mat;
this(const int[][] init) {
assert(init.length == N);
assert(init[0].length == init[1].length && init[0].length == N);
_mat = init.dup;
}
int get(int r, int c) const {
return _mat[r][c];
}
Mat2x2 mul(const Mat2x2 m, int mod) const {
auto t = new int[][](N, N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int x = 0;
for (int k = 0; k < N; k++) {
x = (x + (_mat[i][k] * m.get(k, j)) % mod) % mod;
}
t[i][j] = x;
}
}
return new Mat2x2(t);
}
Mat2x2 copy() const {
return new Mat2x2(_mat);
}
Mat2x2 pow(int k, int mod) const {
assert(k > 0);
if (k == 1) return this.copy();
auto m = this.pow(k / 2, mod);
return k % 2 == 0
? m.mul(m, mod)
: this.mul(m.mul(m, mod), mod);
}
void dump() const {
writeln(_mat[0][0], " ", _mat[0][1]);
writeln(_mat[1][0], " ", _mat[1][1]);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto head = S[0..7];
auto tail = S[$-7..$];
foreach (i; 0..8) {
if (head[0..i] ~ tail[i..7] == "keyence") {
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
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;
}
}
auto manhattan(long x1, long y1, long x2, long y2)
{
return abs(y2 - y1) + abs(x2 - x1);
}
void main()
{
auto n = lread();
auto transport = new long[](5);
foreach (i; 0 .. 5)
{
transport[i] = lread();
}
auto minimam = transport.reduce!(min);
auto most = (n + (minimam - 1)) / minimam;
writeln(most + 4);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto bs = new int[][](K+2, K+2);
auto ws = new int[][](K+2, K+2);
foreach (_; 0..N) {
auto xyc = readln.split;
auto x = xyc[0].to!int;
auto y = xyc[1].to!int;
auto c = xyc[2];
if ((c == "B" && (x/K + y/K)%2 == 0) || (c == "W" && (x/K + y/K)%2 == 1)) {
bs[y%K+1][x%K+1] += 1;
} else {
ws[y%K+1][x%K+1] += 1;
}
}
foreach (i; 1..K+1) foreach (j; 1..K+1) {
bs[i][j] += bs[i-1][j];
ws[i][j] += ws[i-1][j];
bs[i][j] += bs[i][j-1];
ws[i][j] += ws[i][j-1];
bs[i][j] -= bs[i-1][j-1];
ws[i][j] -= ws[i-1][j-1];
}
long r;
foreach (i; 1..K+1) foreach (j; 1..K+1) {
r = max(r, bs[K][K] - bs[i][K] - bs[K][j] + bs[i][j]*2 + ws[K][j] + ws[i][K] - ws[i][j]*2);
r = max(r, ws[K][K] - ws[i][K] - ws[K][j] + ws[i][j]*2 + bs[K][j] + bs[i][K] - bs[i][j]*2);
}
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto x = RD;
auto n = RD!int;
auto m = RD!int;
foreach (i; 0..n)
{
if (x <= 19) break;
x = x / 2 + 10;
}
ans[ti] = x <= m*10;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, core.bitop;
enum P = 998244353;
void main()
{
auto ns = readln.split.to!(long[]);
auto N = ns[0];
auto S = ns[1];
auto as = readln.split.to!(long[]);
auto DP = new long[][](N, S+1);
foreach (ref dp; DP) dp[] = -1;
long solve(int i, long s) {
if (s == 0) {
return N-i+1;
} else if (s < 0 || i == N) {
return 0;
}
if (DP[i][s] == -1) {
DP[i][s] = (solve(i+1, s-as[i]) + solve(i+1, s)) % P;
}
return s == S ? 0 : DP[i][s];
}
solve(0, S);
long r;
foreach (i; 0..N) r = (r + (i+1) * DP[i][S]) % P;
writeln(r);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
int ans = a[0];
foreach (i ; 1 .. n) {
ans += max(0, a[i] - a[i-1]);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
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() {
writeln(readln.chomp.to!int^^3);
}
|
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 nx, ny, nt;
foreach (i; 0 .. n) {
int t = cin.next!int - nt;
int x = cin.next!int, y = cin.next!int;
int d = abs(x - nx) + abs(y - ny);
if (t < d) {
writeln("No");
return;
}
if (!(d % 2 == t % 2)) {
writeln("No");
return;
}
}
writeln("Yes");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
long ans = 0;
foreach (a; A) {
while (a % 2 == 0) ans += 1, a /= 2;
}
ans.writeln;
}
|
D
|
void main()
{
int n = readln.chomp.to!int;
string s = readln.chomp;
writeln(s.count('R') > s.count('B') ? "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;
/* Hello World Program in D Programming */
void main(string[ ] args)
{
int n;
scanf("%d", &n);
int[211111] a;
int a_len = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; ++i) {
a[a_len] = a[i];
++a_len;
while (a_len > 1 && a[a_len - 1] == a[a_len - 2]) {
--a_len;
++a[a_len - 1];
}
}
printf("%d\n", a_len);
for (int i = 0; i < a_len; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
int a, b, c;
cin.scan(a, b, c);
if (a + b >= c)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
|
D
|
module main;
import core.stdc.stdio;
int max(int a,int b){
if(a>b) return a; else return b;
}
int main(){
int n;
scanf("%d",&n);
if(n==1){
printf("23:59");
return 0;
}
int [] arr=new int[105];
for(int i=0;i<n;i++){
int a,b;
scanf("%d:%d",&a,&b);
//printf("%d %d",a,b);
arr[i]=a*60+b;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(arr[i]<arr[j]){
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
// for(int i=0;i<n;i++){
// printf("%d ",arr[i]);
// }
int ans=arr[0]+(24*60-arr[n-1]);
for(int i=0;i<n-1;i++){
ans=max(ans,arr[i+1]-arr[i]);
}
ans--;
printf("%02d:%02d",ans/60,ans%60);
return 0;
}
|
D
|
import std;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, K;
scan(N, K);
long base = powmod(10, 100, MOD);
long ans;
long a;
foreach (i; 0 .. K)
{
a = (a + i);
}
long b;
foreach (i; 0 .. K)
{
b = (b + N - i);
}
foreach (i; K .. N + 1)
{
ans = (MOD + ans + b - a + 1) % MOD;
// writefln("%s %s", i, N - i);
// writeln(b - a + 1);
a += i;
b += N - i;
}
writeln(ans + 1);
}
/// x^^n % m
T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7)
{
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1, m) % m;
}
T tmp = powmod(x, n / 2, m);
return tmp * tmp % m;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int x, a, b;
scan(x, a, b);
writeln(abs(x - a) < abs(x - b) ? "A" : "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;
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.split.to!(int[]);
min(a[0]*a[1], a[2]).writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
auto s = readln.chomp;
auto mae = s[0..2].to!int;
auto ato = s[$-2..$].to!int;
if (1 <= mae && mae <= 12 && 1 <= ato && ato <= 12) {
writeln("AMBIGUOUS");
}
else if (1 <= mae && mae <= 12) {
writeln("MMYY");
}
else if (1 <= ato && ato <= 12) {
writeln("YYMM");
}
else {
writeln("NA");
}
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.array;
import std.conv;
import std.string;
import std.algorithm;
import std.stdio;
void main() {
auto length = readln.chomp.to!uint;
auto a = readln.chomp.split(" ").map!(to!uint).array;
assert(length >= 1);
assert(a.length == length);
auto sieve = new uint[1000008];
foreach (size_t i, e;a) {
if (sieve[e] >= 2) {
continue;
}
for (uint j = e;j < sieve.length;j += e) {
++sieve[j];
}
}
uint answer;
foreach (e;a) {
if (sieve[e] == 1) {
++answer;
}
}
writeln(answer);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto hi = new int[](s.length + 1);
foreach (i, c; s)
hi[i + 1] = hi[i] + c.predSwitch('\\', -1, '_', 0, '/', 1);
auto maxH = hi.reduce!(max);
auto fs1 = hi.findSplitBefore([maxH]), hiL = fs1[0], hiMR = fs1[1];
hiMR.reverse();
auto fs2 = hiMR.findSplitBefore([maxH]), hiR = fs2[0], hiM = fs2[1];
auto aiL = hiL.calcAreas;
auto aiM = hiM.calcAreas; aiM.reverse();
auto aiR = hiR.calcAreas; aiR.reverse();
auto ai = aiL ~ aiM ~ aiR;
writeln(ai.sum);
write(ai.length);
ai.each!(a => write(" ", a));
writeln;
}
int[] calcAreas(int[] hi)
{
int[] areas;
int maxH = int.min, area = 0;
foreach (i, h; hi) {
if (h < maxH) {
area += 2 * (maxH - h) + hi[i - 1] - h;
} else if (h == maxH) {
if (area > 0) {
areas ~= area / 2;
area = 0;
}
} else {
maxH = h;
}
}
return areas;
}
|
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 = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto L = RD;
auto a = RDA;
long pos = -1;
foreach (i; 0..N-1)
{
if (a[i] + a[i+1] >= L)
{
pos = i;
break;
}
}
if (pos == -1)
writeln("Impossible");
else
{
writeln("Possible");
foreach (i; 0..pos)
{
writeln(i+1);
}
foreach_reverse (i; pos+1..N-1)
{
writeln(i+1);
}
writeln(pos+1);
}
stdout.flush();
debug readln();
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, m;
rd(n, m);
struct E {
int u, v;
}
auto edges = new E[](m);
foreach (i; 0 .. m) {
rd(edges[i].u, edges[i].v);
edges[i].u--;
edges[i].v--;
}
int b = 0;
foreach (i; 0 .. m) {
auto g = new int[][](n);
foreach (j; 0 .. m) {
if (i != j) {
g[edges[j].u] ~= edges[j].v;
g[edges[j].v] ~= edges[j].u;
}
}
auto vis = new bool[](n);
void f(int i) {
vis[i] = true;
foreach (j; g[i]) {
if (!vis[j]) {
f(j);
}
}
}
f(0);
if (!reduce!((res, v) => (res && v))(true, vis)) {
b++;
}
}
writeln(b);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string n = chomp(readln());
int a = to!int(n);
if(a/100%10 == a/10%10 && (a/100%10 == a/1000 || a/100%10 == a%10)){
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
void main() {
auto s = rs, t = rs;
size_t[][char] m;
foreach(i, c; s) m[c] ~= i + 1;
long last, loopc;
auto N = s.length;
foreach(c; t) {
if(c !in m) {
writeln(-1);
return;
}
auto k = m[c].assumeSorted.upperBound(last);
debug writefln("%s %s", m[c], k);
if(k.empty) {
loopc++;
last = m[c][0];
} else {
last = k[0];
}
debug writefln("%d %d", loopc, last);
}
writeln(loopc * N + last);
}
// ===================================
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;
}
long rl() {
return readAs!long;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
int[31] stu;
foreach (i; 0..28) {
stu[readln.chomp.to!int]++;
}
foreach (i; 1..31) {
if (!stu[i]) i.writeln;
}
}
|
D
|
import std.algorithm;
import std.bigint;
import std.bitmanip;
import std.concurrency;
import std.container;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
void main() {
writeln(readln.chomp.split.map!(to!long).take(2).reduce!"a * b" / 2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp;
writeln(s[0..4]~" "~s[4..$]);
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
final T[] nextA(T) (int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader;
auto nt = r.next!uint;
foreach (tid; 0 .. nt) {
immutable n = r.next!uint;
immutable m = r.next!uint;
auto a = r.nextA!uint (n);
auto b = r.nextA!uint (m);
a[] -= 1;
b[] -= 1;
auto rank = new uint[n];
foreach (i; 0 .. n) {
rank[a[i]] = i;
}
long res = m;
int e;
int cur;
foreach (i; 0 .. m) {
if (rank[b[i]] > cur) {
cur = rank[b[i]];
res += 2 * (rank[b[i]] + e);
}
--e;
}
writeln (res);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(real[]);
auto A = ab[0];
auto B = ab[1];
writeln((A * B).to!long);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp.dup, ns = s.length;
auto t = readln.chomp, nt = t.length;
loop: foreach_reverse (i; 0..ns-nt+1) {
foreach (j; 0..nt)
if (s[i+j] != '?' && s[i+j] != t[j]) continue loop;
s[i..i+nt][] = t[];
writeln(s.replace("?", "a"));
return;
}
writeln("UNRESTORABLE");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto r = readln.chomp.to!int;
auto g = readln.chomp.to!int;
writeln(2*g-r);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.