code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, k;
scan(n, k);
auto c = new long[](k);
foreach (i ; 1 .. n + 1) {
c[i % k]++;
}
long ans;
foreach (i ; 0 .. k) {
if (2*i % k == 0) {
ans += c[i]^^3;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import 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 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(){
long a = scan!int, b = scan!int, c = scan!int, d = scan!int;
long ans = max(a * c, a * d, b * c, b * d);
ans.print;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.array;
import std.string;
import std.math;
import std.functional;
import std.range;
import std.typecons;
import std.format;
void main(string[] args) {
readln;
readln.solve.writeln;
}
auto solve(string line1) {
auto hash = line1.chomp.split.map!(to!long).array.counter;
long res;
foreach(k, v; hash) {
if (k < v) {
res += v - k;
} else if (v < k) {
res += v;
}
}
return res;
} unittest {
assert(solve("3 3 3 3") == 1, "1");
assert(solve("2 4 1 4 2") == 2, "2");
assert(solve("1 2 2 3 3 3") == 0, "3");
assert(solve("1000000000") == 1, "4");
assert(solve("2 7 1 8 2 8 1 8") == 5, "4");
}
pure auto counter(N = int, E)(E[] arr) {
N[E] hash;
foreach(e; arr) {
hash[e]++;
}
return hash;
}
|
D
|
import std.stdio, std.conv, std.array,std.string,std.algorithm;
void main()
{
auto n=readln.chomp.to!int;
int ans=1;
for(int i=1;i<n;i++){
if(ans>n){
break;
}else{
ans*=2;
}
}
if(n==1){
ans=2;
}
writeln(ans/=2);
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, 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;
}
}
// }}}
long[] divisor(const long n) {
long[] ret;
for (long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret ~= i;
if (i != n / i) {
ret ~= n / i;
}
}
}
return ret;
}
void main() {
auto cin = new Scanner;
long n;
cin.scan(n);
auto primes = divisor(n);
auto prevPrimes = divisor(n - 1);
auto res = prevPrimes.length - 1;
foreach (k; primes) {
if (k == 1) continue;
long nn = n;
while (nn >= k) {
if (nn % k == 0) {
nn /= k;
} else {
nn %= k;
}
}
if (nn == 1) {
res++;
}
}
writeln(res);
}
|
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, "x", long, "y");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long h, a;
scan(h, a);
writeln((h + a - 1) / a);
}
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 solve(){
}
void main(){
int[] val = inary();
( (val[0]-1)*(val[1]-1) ).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, 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 = readln.split.map!(to!int).array;
if (N % 3 != 0) {
writeln(A.map!(a => a == 0).all ? "Yes" : "No");
} else {
int[int] cnt;
foreach (a; A) cnt[a] += 1;
int[] B;
foreach (k, v; cnt) {
if (v % (N / 3) != 0) {
writeln("No");
return;
}
while (v > 0) {
B ~= k;
v -= N / 3;
}
}
if (B.length != 3) {
writeln("No");
return;
}
if (B[0] != (B[1] ^ B[2])) {
writeln("No");
return;
}
writeln("Yes");
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1];
writeln(a % 3 == 0 || b % 3 == 0 || (a+b) % 3 == 0 ? "Possible" : "Impossible");
}
|
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()
{
bool b = (lread() % 500) <= lread();
writeln(b ? "Yes" : "No");
}
|
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 S = sread();
outer: foreach (state; 0 .. 2 ^^ 2)
{
auto ans = new char[](N);
ans[0] = "SW"[state & 1];
ans[1] = "SW"[(state >> 1) & 1];
foreach (i; 1 .. N - 1)
{
if (ans[i - 1 .. i + 1] == "SS")
{
ans[i + 1] = (S[i] == 'o') ? 'S' : 'W';
}
if (ans[i - 1 .. i + 1] == "SW")
{
ans[i + 1] = (S[i] != 'o') ? 'S' : 'W';
}
if (ans[i - 1 .. i + 1] == "WS")
{
ans[i + 1] = (S[i] == 'o') ? 'W' : 'S';
}
if (ans[i - 1 .. i + 1] == "WW")
{
ans[i + 1] = (S[i] != 'o') ? 'W' : 'S';
}
}
foreach (i; 0 .. N)
{
long l = (N + i - 1) % N;
long r = (i + 1) % N;
if (ans[i] == 'S' && S[i] == 'o' && ans[l] != ans[r])
continue outer;
if (ans[i] == 'S' && S[i] == 'x' && ans[l] == ans[r])
continue outer;
if (ans[i] == 'W' && S[i] == 'o' && ans[l] == ans[r])
continue outer;
if (ans[i] == 'W' && S[i] == 'x' && ans[l] != ans[r])
continue outer;
}
writeln(ans);
return;
}
writeln(-1);
}
|
D
|
void main()
{
string s = readln.chomp;
string t = readln.chomp;
bool[26][26] table;
foreach (i; 0 .. s.length)
{
table[s[i]-'a'][t[i]-'a'] = true;
}
bool ok = true;
foreach (i; 0 .. 26)
{
long row, col;
foreach (j; 0 .. 26)
{
if (table[i][j]) ++row;
if (table[j][i]) ++col;
}
if (row > 1 || col > 1) ok = false;
}
writeln(ok ? "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.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int[] HW = readln.chomp.split.to!(int[]);
int H = HW[0],
W = HW[1];
int[] hw = readln.chomp.split.to!(int[]);
int h = hw[0],
w = hw[1];
writeln((H - h) * (W - w));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto s = sread();
foreach_reverse (i; 0 .. s.length)
{
auto t = s[0 .. i];
if (t[0 .. $ / 2] == t[$ / 2 .. $])
{
writeln(t.length);
return;
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto xy = readln.split.to!(int[]);
auto X = xy[0];
auto Y = xy[1];
foreach (i; 0..X+1) {
auto j = X-i;
if (i*2 + j*4 == Y) {
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
void main(){
switch(_scan()%10){
case 0,1,6,8:
writeln("pon");
break;
case 3:
writeln("bon");
break;
default:
writeln("hon");
}
}
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 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;
}
}
// }}}
// 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 k;
string s;
cin.scan(k, s);
if (s.length <= k) {
writeln(s);
} else {
writeln(s[0 .. k], "...");
}
}
|
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; }
const long mod = 1_000_000_007;
void main(){
int n = read.to!int;
int m = read.to!int;
int[] as;
foreach(i; 0 .. n) as ~= read.to!int;
int[] bs;
foreach(j; 0 .. m) bs ~= read.to!int;
int[int] aPos, bPos;
foreach(i; 0 .. n) aPos[as[i]] = i;
foreach(j; 0 .. m) bPos[bs[j]] = j;
// xを減らしていくとき、その行・列・全体で入れるマスはいくつあるか
int[] aCounts; // その行にすでに入っている個数
int[] bCounts; // その列にすでに入っている個数
int aCount; // 解禁されている行数
int bCount; // 解禁されている列数
foreach(i; 0 .. n) aCounts ~= 0;
foreach(j; 0 .. m) bCounts ~= 0;
aCount = 0;
bCount = 0;
long ans = 1;
foreach_reverse(x; 1 .. n * m + 1){
if(x in aPos && x in bPos){
// そこの場所できまり
ans *= 1;
// その行および列は新たに解禁された
aCount += 1;
bCount += 1;
// その行および列はあきが減る
aCounts[aPos[x]] += 1;
bCounts[bPos[x]] += 1;
}
else if(x in aPos && x !in bPos){
// その行で、解禁済みの列のうち、あきをさがす
ans *= bCount - aCounts[aPos[x]];
ans %= mod;
// その行は新たに解禁された
aCount += 1;
// その行はあきが減る
aCounts[aPos[x]] += 1;
}
else if(x !in aPos && x in bPos){
// その列で、解禁済みの行のうち、あきをさがす
ans *= aCount - bCounts[bPos[x]];
ans %= mod;
// その列は新たに解禁された
bCount += 1;
// その列はあきが減る
bCounts[bPos[x]] += 1;
}
else{
// 全体で、解禁済みの行かつ解禁済みの列のうち、あきをさがす
ans *= aCount * bCount - (n * m - x);
ans %= mod;
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
string N = readln.chomp;
writeln(N[0] == N[2] ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
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, "p", long, "dist");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n, k;
scan(n, k);
long cnt;
foreach (b; iota(k + 1, n + 1))
{
auto p = n / b;
cnt += max(p * (b - k), 0);
cnt += max(0, n - p * b - k + 1);
}
if (k == 0)
cnt -= n;
cnt.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
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto s=new char[][](n);
foreach(i; 0..n) s[i]=readln.chomp.to!(char[]);
long[char] cnt;
cnt['M']=cnt['A']=cnt['R']=cnt['C']=cnt['H']=0L;
foreach(i; 0..n){
auto c=s[i][0];
if(c in cnt) cnt[c]++;
}
long tot=0;
tot+=cnt['M']*cnt['A']*cnt['R'];
tot+=cnt['M']*cnt['A']*cnt['C'];
tot+=cnt['M']*cnt['A']*cnt['H'];
tot+=cnt['M']*cnt['R']*cnt['C'];
tot+=cnt['M']*cnt['R']*cnt['H'];
tot+=cnt['M']*cnt['C']*cnt['H'];
tot+=cnt['A']*cnt['R']*cnt['C'];
tot+=cnt['A']*cnt['R']*cnt['H'];
tot+=cnt['A']*cnt['C']*cnt['H'];
tot+=cnt['R']*cnt['C']*cnt['H'];
writeln(tot);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import 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);
solve(A, B, C).writeln();
}
long solve(long A, long B, long C)
{
if (C <= B)
{
return C + B;
}
long result;
result += 2 * B;
C -= B;
result += min(C, A);
C -= A;
if (0 < C)
{
result++;
}
return result;
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
void main(){
string[] input = readln.split;
int A = input[0].to!int;
int B = input[1].to!int;
if(B % A == 0) write(A + B);
else write(B - A);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum mod = 10L^^9 + 7;
void main() {
auto k = readln.chomp;
int n = k.length.to!int;
int d;
scan(d);
auto dp = new long[][][](n + 1, 2, d);
dp[0][0][0] = 1;
foreach (i ; 0 .. n) {
foreach (m ; 0 .. 2) {
foreach (r ; 0 .. d) {
foreach (a ; 0 .. (m ? 10 : k[i] - '0' + 1)) {
(dp[i+1][m | (a < k[i] - '0')][(r + a % d) % d] += dp[i][m][r]) %= mod;
}
}
}
}
auto ans = dp[n][0][0] + dp[n][1][0] - 1 + mod;
ans %= mod;
writeln(ans);
}
int LIS(T)(T[] arr) {
import std.range, std.algorithm;
auto n = arr.length;
auto dp = repeat(T.max, n + 1).array.assumeSorted;
dp[0] = T.min;
int res;
foreach (i; 0 .. n) {
auto j = dp.lowerBound(arr[i]).length;
dp[j] = arr[i];
res = max(res, j);
}
return res;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
void main() {
int n = readln.chomp.to!int;
string s = readln.chomp;
int[] rcs = new int[n+1];
int[] gcs = new int[n+1];
int[] bcs = new int[n+1];
long sum = 0;
for(int i=0; i<s.length; i++) {
if(s[$-1-i]=='R') rcs[n-1-i]=rcs[n-i]+1;
else rcs[n-1-i]=rcs[n-i];
if(s[$-1-i]=='G') gcs[n-1-i]=gcs[n-i]+1;
else gcs[n-1-i]=gcs[n-i];
if(s[$-1-i]=='B') bcs[n-1-i]=bcs[n-i]+1;
else bcs[n-1-i]=bcs[n-i];
}
for(int i=0; i<s.length; i++) {
for(int j=i+1; j<s.length; j++) {
if(s[i]=='R') {
if(s[j]=='G') sum += bcs[j+1];
if(s[j]=='B') sum += gcs[j+1];
}
if(s[i]=='G') {
if(s[j]=='R') sum += bcs[j+1];
if(s[j]=='B') sum += rcs[j+1];
}
if(s[i]=='B') {
if(s[j]=='R') sum += gcs[j+1];
if(s[j]=='G') sum += rcs[j+1];
}
}
}
for(int span=1; span<n; span++) {
for(int i=0; i+span*2<n; i++) {
if(s[i]!=s[i+span] &&
s[i+span]!=s[i+span*2] &&
s[i]!=s[i+span*2]) sum--;
}
}
writeln(sum);
}
|
D
|
void main(){
int[] fares;
foreach(_;0..4){
fares ~= inelm();
}
min( fares[0]+fares[2], fares[0]+fares[3],
fares[1]+fares[2], fares[1]+fares[3])
.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio;
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()
{
bool f;
auto s = readln.chomp;
auto prev = 'a';
foreach (e; s) {
if (prev == e) {
f = true;
}
prev = e;
}
if (f) {
writeln("Bad");
} else {
writeln("Good");
}
}
|
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 = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(string, "s", long, "p");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto x = lread();
writeln(2 * (x / 11) + (x % 11 ? (x % 11 > 6 ? 2 : 1) : 0));
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nmc = readln.split.to!(int[]);
auto N = nmc[0];
auto M = nmc[1];
auto C = nmc[2];
auto bs = readln.split.to!(int[]);
int cnt;
foreach (_; 0..N) {
int d;
foreach (i, a; readln.split.to!(int[])) {
auto b = bs[i];
d += a * b;
}
if (d + C > 0) ++cnt;
}
writeln(cnt);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
void main() {
auto X = readln.split[0].to!int;
writeln(X == 7 || X == 5 || X == 3 ? "YES" : "NO");
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable inf = 10^^6;
void main() {
int k;
scan(k);
auto d = new int[](k);
d[] = inf;
d[1] = 1;
auto deq = Deque!(int)(3*k);
deq.pushBack(1);
while (!deq.empty) {
auto u = deq.front;
deq.popFront();
if (d[(u + 1) % k] > d[u] + 1) {
d[(u + 1) % k] = d[u] + 1;
deq.pushBack((u + 1) % k);
}
if (d[(u * 10) % k] > d[u]) {
d[(u * 10) % k] = d[u];
deq.pushFront((u * 10) % k);
}
}
writeln(d[0]);
}
struct Deque(T) {
private {
int N, head, tail;
T[] deq;
}
this(int n) {
N = n + 1;
deq = new T[](N + 1);
}
bool empty() {
return head == tail;
}
bool full() {
return head == ((tail + 1) % N);
}
int length() {
return (tail - head + N) % N;
}
T front() {
assert(!empty);
return deq[head];
}
T back() {
assert(!empty);
return deq[(tail - 1 + N) % N];
}
void pushFront(T x) {
assert(!full);
head = (head - 1 + N) % N;
deq[head] = x;
}
void pushBack(T x) {
assert(!full);
deq[tail++] = x;
tail %= N;
}
void popFront() {
assert(!empty);
head = (head + 1) % N;
}
void popBack() {
assert(!empty);
tail = (tail - 1 + N) % N;
}
}
unittest {
int n = 5;
auto deq = Deque!(int)(n);
deq.pushFront(3);
deq.pushBack(1);
deq.pushBack(4);
deq.pushFront(5);
assert(deq.front == 5);
assert(deq.back == 4);
assert(deq.length == 4);
deq.pushFront(0);
assert(deq.full);
assert(deq.front == 0);
assert(deq.length == 5);
deq.popFront();
deq.popFront();
deq.popBack();
assert(deq.front == 3);
assert(deq.back == 1);
assert(deq.length == 2);
deq.popBack();
deq.popFront();
assert(deq.empty);
assert(deq.length == 0);
writeln("ok");
}
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;
void main()
{
readln.chomp.replace(".", "").replace(",", "").split.filter!(a=> a.length >= 3 && a.length <= 6).join(" ").writeln;
}
|
D
|
void main() {
string[] tmp = readln.split;
string a = tmp[0], b = tmp[1];
writeln(a == b ? "H" : "D");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import 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;
int sum;
foreach (_; 0..n) {
auto l = readln.chomp.split.map!(to!int);
sum += l[1] - l[0] + 1;
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
int[string] a;
ubyte N = readln.chomp.to!ubyte;
foreach(_; 0..N) {
a[readln.chomp]++;
}
ubyte M = readln.chomp.to!ubyte;
foreach(_; 0..M) {
a[readln.chomp]--;
}
max(a.values.reduce!max, 0).writeln;
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
import std.numeric;
alias lread = () => readln.chomp.to!long;
alias aryread = () => readln.split.to!(long[]);
void main()
{
auto ab = aryread();
(((ab[0] * ab[1]) % 2 == 0) ? "No" : "Yes").writeln;
}
|
D
|
import std.stdio;
import std.string, std.conv;
import std.algorithm, std.array;
import std.typecons;
void main() {
for(;;) {
auto s = readln.split.map!(to!int).array;
int N = s[0], M = s[1];
if(N == 0 && M == 0) break;
if((N * M) % 2 == 0) writeln("yes");
else writeln("no");
}
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int read() { return readln.chomp.to!int; }
int[] reads() { return readln.split.to!(int[]); }
string solve(string s) {
int w = read();
string r;
foreach (i, c; s) if (i % w == 0) r ~= c;
return r;
}
void main() {
writeln(readln.chomp.solve);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto b = iota(n).map!(i => readln.chomp).array;
int checkLeft(string s) {
int h;
foreach (ch ; s) {
if (ch == '(') {
h++;
}
else {
h--;
if (h < 0) {
return -1;
}
}
}
return h;
}
int checkRight(string s) {
int h;
foreach_reverse (ch ; s) {
if (ch == ')') {
h++;
}
else {
h--;
if (h < 0) {
return -1;
}
}
}
return h;
}
auto cnt = new int[](5 * 10^^5 + 1);
int perfect;
foreach (s ; b) {
int h = checkLeft(s);
if (h > 0) {
cnt[h]++;
}
if (h == 0) {
perfect++;
}
}
int ans;
foreach (s ; b) {
int h = checkRight(s);
if (h > 0 && cnt[h] > 0) {
ans++;
cnt[h]--;
}
}
ans += perfect / 2;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 2*10^^9 + 7;
void main() {
int n, k; scan(n, k);
auto a = new int[](n);
auto b = new int[](n);
foreach (i ; 0 .. n) {
int ai, bi; scan(ai, bi);
a[i] = ai, b[i] = bi;
}
long ans;
foreach (j ; 0 .. n) {
if ((~k & a[j]) == 0) {
ans += b[j];
}
}
foreach (i ; 0 .. 32) {
if (k & (1 << i)) {
long tmp;
int ks = k ^ (1 << i) | ((1<<i) - 1);
debug {
writefln("%16b", ks);
}
foreach (j ; 0 .. n) {
if ((~ks & a[j]) == 0) {
tmp += b[j];
}
}
ans = max(ans, tmp);
}
}
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
readint;
auto ss = reads!string;
bool[string] d;
foreach (s; ss) d[s] = true;
writeln(d.keys.length == 3 ? "Three" : "Four");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
string s;
while ((s = readln.chomp) != null) {
auto ab = s.split.map!(to!int).array;
int a = ab[0], b = ab[1];
writeln(gcd(a, b));
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
void main() {
readln.chomp.count('1').writeln;
}
|
D
|
void main() {
writeln("ABC", readln.chomp);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n, m;
scan(n, m);
long left, right = n;
foreach (i; iota(m))
{
long l, r;
scan(l, r);
left = max(l, left);
right = min(r, right);
}
auto pass = right - left + 1;
writeln(pass > 0 ? pass : 0);
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], m = tmp[1];
int[] road = new int[n];
foreach (i; 0 .. m) {
tmp = readln.split.to!(int[]);
int a = tmp[0] - 1, b = tmp[1] - 1;
++road[a], ++road[b];
}
foreach (x; road) {
x.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;
import core.bitop;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "g");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
long a, b, c, d;
scan(a, b, c, d);
writeln((b - a + 1) - (b / c - (a - 1) / c) - (b / d - (a - 1) / d) + (b / (c / gcd(c,
d) * d) - (a - 1) / (c / gcd(c, d) * d)));
}
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.to!(int[]), w = rd[0], a = rd[1], b = rd[2];
if (a > b) swap(a, b);
writeln(b <= a+w ? 0 : b-(a+w));
}
|
D
|
import std.stdio, std.conv, std.string, std.math;
void main(){
auto ip = readln.chomp.to!int;
if(ip < 1200){
"ABC".writeln;
} else {
"ARC".writeln;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto nm = readints();
int n = nm[0], m = nm[1];
auto xs = new bool[](n + 1); // 1 から i へ定期便があるか
auto ys = new bool[](n + 1); // n から i へ定期便があるか
for (int i = 0; i < m; i++) {
auto ab = readints();
int a = ab[0], b = ab[1];
if (a > b)
swap(a, b);
if (a == 1)
xs[b] = true;
if (b == n)
ys[a] = true;
}
bool found = false;
for (int i = 2; i < n; i++) {
if (xs[i] && ys[i]) {
found = true;
break;
}
}
writeln(found ? "POSSIBLE" : "IMPOSSIBLE");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
writeln(180 * (readln.chomp.to!int-2));
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto ri = n.iota.map!(_ => readln.chomp.to!int).array;
auto maxR = ri.back;
auto maxP = int.min;
foreach_reverse (i; 0..n-1) {
maxP = max(maxP, maxR - ri[i]);
maxR = max(maxR, ri[i]);
}
writeln(maxP);
}
|
D
|
void main()
{
long[] tmp = readln.split.to!(long[]);
long k = tmp[0], a = tmp[1], b = tmp[2];
long op = k - a + 1;
long cal = a + (b - a) * (op / 2) + op % 2;
max(k + 1, cal).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, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int a, b, c, d;
scan(a, b, c, d);
writeln(max(a*b, c*d));
}
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();
string s;
string t;
scan(s, t);
// writeln(s, t);
foreach (i; 0 .. n)
{
write(s[i], t[i]);
}
}
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, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long INF = 1L << 59;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto A = readln.split.map!(to!int).array;
auto cnt1 = new int[](N+10);
auto cnt2 = new int[](M+10);
cnt2[0] = N;
auto ans = new dchar[](M);
int mv = 0;
foreach (i; 0..M) {
bool ok = false;
if (cnt1[A[i]] == mv && cnt2[cnt1[A[i]]] == 1) {
ok = true;
mv += 1;
}
cnt2[cnt1[A[i]]] -= 1;
cnt1[A[i]] += 1;
cnt2[cnt1[A[i]]] += 1;
if (ok) {
ans[i] = '1';
} else {
ans[i] = '0';
}
}
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long r, d, x;
scan(r, d, x);
auto tmp = x;
foreach (i; 0 .. 10)
{
tmp = calc_algae(r, tmp, d);
tmp.writeln();
}
}
auto calc_algae(long r, long x, long d)
{
return r * x - d;
}
|
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 cnt;
foreach (i; 0..N)
{
if (s[i] == 'R')
++cnt;
}
writeln(cnt > N / 2 ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main(){
auto Z=readln.split.to!(int[]),a=Z[0],b=Z[1],c=Z[2];
if(a+b>=c)writeln("Yes");
else writeln("No");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math, std.typecons;
void main() {
int n; scan(n);
writeln(euler_phi(n));
}
int euler_phi(int n) {
int ans = n;
for (int p = 2; p*p <= n; p++) {
if (n % p == 0) {
ans = ans / p * (p - 1);
while (n % p == 0) n /= p;
}
}
if (n > 1) ans = ans / n * (n - 1);
return 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() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], a = tmp[1], b = tmp[2];
min(a*n, b).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, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
string ans;
int i = 0, j = n - 1;
long x = -1;
while(i <= j){
if(as[i] < as[j]){
if(x < as[i]) x = as[i], ans ~= "L", i += 1;
else if(x < as[j]) x = as[j], ans ~= "R", j -= 1;
else break;
}
else{
if(x < as[j]) x = as[j], ans ~= "R", j -= 1;
else if(x < as[i]) x = as[i], ans ~= "L", i += 1;
else break;
}
}
ans.length.writeln;
ans.writeln;
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
3
1
---+/
void main(string[] args) {
const A = readln.chomp.to!long;
const B = readln.chomp.to!long;
[1, 2, 3].filter!(x => x != A && x != B).front.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()
{
auto a=(scanElem+11)%13;
auto b=(scanElem+11)%13;
if(a<b)end("Bob");
if(a>b)end("Alice");
end("Draw");
}
|
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() {
int N = 100;
int[] ary = new int[N+1];
while(true) {
auto input = readln.chomp;
if (stdin.eof) break;
ary[input.to!size_t]++;
}
int maximum = ary.reduce!(max);
foreach(size_t i; 0..ary.length) if (ary[i]==maximum) i.writeln;
}
|
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() {
string n;
scan(n);
bool yn = n[0 .. 3].to!int % 111 == 0 || n[1 .. 4].to!int % 111 == 0;
writeln(yn ? "Yes" : "No");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this(int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head = (head + 1) % N;
}
void clear() {
head = tail = 0;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
auto data = split(readln());
( to!int(data[0]) * to!int(data[1]) / 2 ).writeln();
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc177/tasks/abc177_c
// math
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
const long mod = 10^^9+7;
long n = readln.chomp.to!long;
long[] a = readln.split.map!(to!long).array;
long sum = 0;
for(long i = 0; i < n; i++) {
sum += a[i];
sum %= mod;
}
long ans = 0;
for(int i = 0; i < n; i++) {
sum -= a[i];
if(sum < 0) sum += mod;
ans += a[i] * sum;
ans %= mod;
}
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;
void times(alias pred)(int n) {
foreach(i; 0..n) pred();
}
auto rep(alias pred, T = typeof(pred()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = pred();
return res;
}
void main() {
string str = readln.chomp;
writeln(str[0..4], " ", str[4..$]);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
writeln(s.lastIndexOf("Z") - s.indexOf("A") + 1);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
int v_count = 0;
int h_count = 0;
foreach (s; S) {
if (s == '1') {
writeln(1, " ", h_count + 1);
h_count += 2;
if (h_count == 4) h_count = 0;
} else {
writeln(3, " ", v_count + 1);
v_count += 1;
if (v_count == 4) v_count = 0;
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable inf = 1<<30;
string ng = "UNRESTORABLE";
void main() {
string s, t;
scan(s);
scan(t);
int n = s.length.to!int, k = t.length.to!int;
foreach_reverse (i ; 0 .. n - k + 1) {
bool match = true;
foreach (j ; i .. i + k) {
if (s[j] == '?') continue;
if (s[j] != t[j - i]) {
match = false;
break;
}
}
if (match) {
foreach (j ; 0 .. n) {
if (i <= j && j < i + k) {
write(t[j - i]);
}
else {
write(s[j] == '?' ? 'a' : s[j]);
}
if (j == n - 1) {
writeln();
}
}
return;
}
}
writeln(ng);
}
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto s = readln.chomp;
writeln(s.startsWith("YAKI") ? "Yes" : "No");
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int a = tmp[0], b = tmp[1];
writeln(a > 2 * b ? a - 2 * b : 0);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nx = readln.split.to!(long[]);
auto N = nx[0];
auto X = nx[1];
long[51] as, ps;
as[0] = 1;
ps[0] = 1;
foreach (i; 1..51) {
as[i] = as[i-1] * 2 + 3;
ps[i] = ps[i-1] * 2 + 1;
}
long r;
while (X > 0 && N >= 0) {
if (N == 0) {
r += 1;
break;
}
if (X >= as[N]/2+1) {
r += ps[N-1] + 1;
X -= as[N-1] + 2;
} else {
X -= 1;
}
--N;
}
writeln(r);
}
|
D
|
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
ulong modComb(ulong n, ulong r, ulong m) {
if (n == r) return 1;
if (r == 0) return 1;
if (n < r) return 0;
ulong up = 1;
ulong down = 1;
for(ulong i = n-r+1; i <= n; i++) {
up *= i;
up %= m;
}
for(ulong i = 2; i <= r; i++) {
down *= i;
down %= m;
}
return up*modInv(down, m) % m;
}
ulong MOD = 1000000007;
void main() {
ulong n, k;
readInto(n, k);
for (ulong i = 1; i <= k; i++) {
writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD);
}
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner scn = new Scanner(stdin);
scope(exit) assert(!scn.hasNext);
int h, w, n, sr, sc;
string s, t;
scn.read(h, w, n, sr, sc, s, t); sr--; sc--;
int le = 0, ri = w, dw = 0, up = h;
foreach_reverse (ph; 0..n) {
{
char c = t[ph];
final switch(c) {
case 'U':
up = min(up + 1, h);
break;
case 'D':
dw = max(dw - 1, 0);
break;
case 'L':
ri = min(ri + 1, w);
break;
case 'R':
le = max(le - 1, 0);
break;
}
}
{
char c = s[ph];
final switch(c) {
case 'U':
dw++;
break;
case 'D':
up--;
break;
case 'L':
le++;
break;
case 'R':
ri--;
break;
}
}
if (le >= ri || dw >= up) {
writeln("NO");
return 0;
}
debug writefln("%d %d %d %d", le, ri, dw, up);
}
if (le <= sc && sc < ri && dw <= sr && sr < up) {
writeln("YES");
} else {
writeln("NO");
}
return 0;
}
/* 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);
}
}
}
}
/* 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/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;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.algorithm, std.range, std.conv;
void main(){
iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a*b)).join("\n")).join("\n").writeln;
}
|
D
|
import std.conv, std.stdio;
import std.algorithm, std.array, std.string, std.range;
void main()
{
auto
x = readln.chomp.to!int,
t = x / 500,
s = (x % 500) / 5;
(t * 1000 + s * 5).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;
int ans = 0;
readln.chomp.map!"a=='I'?1:-1".fold!((a, b) {
ans = max(ans, a+b);
return a+b;
})(0);
ans.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);
}
}
}
}
// 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
|
// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio, std.string, std.array, std.algorithm, std.ascii;
void main() {
readln();
auto xs = split(chomp(readln()));
auto ys = xs.map!( (x) => x.count!( isUpper ) );
writeln(ys.maxCount[0]);
}
|
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.datetime;
void main() {
auto N = readln.chomp.to!int;
auto G = new int[][](N);
foreach (i; 0..N-1) {
auto v = readln.chomp.to!int;
G[v-1] ~= i+1;
}
auto is_leaf = new bool[](N);
foreach (i; 0..N) is_leaf[i] = G[i].length == 0;
auto ok = new bool[](N);
foreach (i; 0..N) {
if (is_leaf[i]) {
ok[i] = true;
continue;
}
int cnt = 0;
foreach (m; G[i]) {
cnt += is_leaf[m];
}
ok[i] = cnt >= 3;
}
writeln(ok.all ? "Yes" : "No");
}
|
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;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
const n = r.next!uint ();
auto d = r.next!uint ();
auto a = r.nextA!uint (n);
foreach (i; 1 .. n) {
while (d >= i && a[i] > 0) {
d -= i;
a[0]++;
a[i]--;
}
}
writeln (a[0]);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
auto m = RD!int;
auto k = RD!int;
writeln(m >= n && k >= n ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
void main(string[] args)
{
inputFile = stdin;
debug inputFile = File(args[1]);
auto nt = next!int;
foreach(t; 0 .. nt)
{
auto n = next!int;
auto ss = next!string(n);
int[char] cnt;
foreach(s; ss)
foreach(c; s)
cnt.require(c, 0)++;
if (cnt.byValue.all!(c => c % n == 0))
writeln("YES");
else
writeln("NO");
}
}
// INPUT
enum InputStyle
{
byChunk, byLine
};
enum inputStyle = InputStyle.byChunk;
File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
{
const chunkSize = 4096;
const wordSize = 4096;
char[chunkSize] chunkBuff;
char[] currChunk;
void renewChunk()
{
if (inputFile.eof)
currChunk = null;
else
currChunk = inputFile.rawRead(chunkBuff);
}
char[wordSize] wordBuff;
char[] word;
string popWord()
{
if (currChunk.length == 0)
renewChunk;
assert(currChunk.length > 0);
while (currChunk[0].isWhite)
{
currChunk = currChunk[1 .. $];
if (currChunk.length == 0)
renewChunk;
}
word = wordBuff[0 .. 0];
while (!currChunk[0].isWhite)
{
word.length++;
word[$ - 1] = currChunk[0];
currChunk = currChunk[1 .. $];
if (currChunk.length == 0)
{
renewChunk;
if (currChunk.length == 0)
return cast(string) word;
}
}
return cast(string) word;
}
}
else
{
DList!string _words;
string popWord()
{
while (_words.empty)
{
foreach(w; inputFile.readln.split)
{
_words.insertBack(w);
}
}
auto word = _words.front;
_words.removeFront;
return word;
}
}
T next(T)()
{
return to!T(popWord);
}
auto next(T, S...)(S s)
{
static if (S.length == 0)
{
return to!T(popWord);
}
else
{
auto res = new typeof(next!T(s[1 .. $]))[](s[0]);
foreach(ref elem; res)
elem = next!T(s[1 .. $]);
return res;
}
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
const int MOD = 998244353;
string s = chomp(readln());
int n = to!int(s.length);
int[] a = new int[n];
foreach (i; 0..n) {
a[i] = s[i]-'a';
}
bool same() {
foreach (i; 1..n) {
if (a[i-1] != a[i]) return false;
}
return true;
}
if (same()) {
writeln(1);
return;
}
if (n == 2) {
writeln(2);
return;
}
if (n == 3) {
if (a[0] != a[1] && a[1] != a[2] && a[2] != a[0]) {
writeln(3);
return;
}
}
long ans = 1;
foreach (i; 0..n-1) ans = (ans*3)%MOD;
bool unstable() {
foreach (i; 1..n) {
if (a[i-1] == a[i]) return false;
}
return true;
}
if (unstable()) ans++;
long[][][] dp = new long[][][](n,3,3);
foreach (i; 0..3) dp[0][i][i] = 1;
foreach (i; 1..n) {
foreach (j; 0..3) foreach (k; 0..3) {
foreach (x; 0..3) {
if (k == x) continue;
(dp[i][(j+x)%3][x] += dp[i-1][j][k]) %= MOD;
}
}
}
ans -= dp[n-1][a.sum%3].sum;
ans = (ans%MOD+MOD)%MOD;
writeln(ans);
}
|
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 ab = aryread!string();
// writeln(ab);
string ab_str = ab.join();
// writeln(ab_str);
long tmp;
foreach (i; 0 .. ab_str.length)
{
// writeln(10 ^^ (((ab_str.length) - 1) - i));
tmp += (ab_str[i] - '0') * 10 ^^ (((ab_str.length) - 1) - i);
}
// writeln(tmp);
foreach (i; 1 .. 10 ^^ 4)
{
if (i * i == tmp)
{
writeln("Yes");
return;
}
}
writeln("No");
}
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, 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 A = RD;
auto B = RD;
writeln(max(A+B, max(A - B, A * B)));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto r = readln.chomp.to!int;
writeln(3 * r^^2);
}
|
D
|
void main(){
int[] x = _scanln!()();
foreach(i, elm; x){
if(elm == 0){
writeln(i+1);
return;
}
}
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string;
// 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.conv;
void main()
{
auto ip = readln.chomp, X = ip[0], Y = ip[2];
if(X > Y){
writeln(">");
} else if(X < Y){
writeln("<");
} else {
writeln("=");
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto N = readln.chomp.to!int;
string[] wordList;
bool result = true;
foreach (i; 0..N) {
auto W = readln.chomp;
scope(exit) wordList ~= W;
if (wordList is null) continue;
if (wordList.canFind(W)) result = false;
if (wordList.back.back != W.front) result = false;
}
writeln(result ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
readln.count('1').writeln;
}
|
D
|
import core.stdc.stdio;
import std.algorithm;
import std.stdio;
int lowerBound(int[] data,int t){
int l=0;
int r=cast(int)data.length+1;
while(r-l>1){
int m=(l+r)/2;
if(data[m-1]<t)
l=m;
else
r=m;
}
return l;
}
void main(){
int n,m;
scanf("%d%d",&n,&m);
int[] u = new int[n];
int[] k = new int[n];
foreach(i;0..m){
scanf("%d",&u[i]);
int j;
scanf("%d",&j);
k[--j]=u[i];
}
foreach(i;m..n)
scanf("%d",&u[i]);
sort(u);
int[] d=new int[n*2];
static immutable int inf = 114514;
bool Solve(int t){
int c=n-u.lowerBound(t);
foreach(i;0..n)
if(k[i])
if(k[i]>=t){
d[i]=0;
c--;
}else
d[i]=inf;
else
d[i]=1;
int b=0,e=n;
while(b<e-1){
d[e++]=min(inf,d[b]+d[b+1]+d[b+2]-max(d[b],d[b+1],d[b+2]));
b+=3;
}
return d[e-1]<=c;
}
int l=0,r=n;
while(r-l>1){
int s=(r+l)/2;
if(Solve(u[s]))
l=s;
else
r=s;
}
printf("%d\n",u[l]);
}
|
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[] 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 A, P;
scan(A, P);
writeln((3 * A + P) / 2);
}
|
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 = sread();
// writeln(n);
long ans;
foreach (i; 0 .. (n.length))
{
ans += n[i] - '0';
}
if (ans % 9 == 0)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
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() {
problem();
}
void problem() {
const MAX = 1_000_000_000_000_001;
auto N = scan!long - 1;
string solve() {
char[] ans;
foreach(i; 0..1000) {
auto mod = N % 26;
N /= 26;
N -= 1;
ans ~= 'a' + cast(char)mod;
if (N < 0) {
break;
}
}
return cast(string)ans.reverse();
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int a, b;
scan(a, b);
writeln(max(a + b, a - b, a * b));
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const T a, const T b) { return a > b ? a - b : b - a; }
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void main() {
const ip = readToArray!(long);
const auto n = ip[0], l = ip[1];
long sum = 0;
for(long i = 0; i < n; i++) {
sum += l + i;
}
if (l < 0 && l + n - 1 >= 0) {
// nothing
} else if (l + n - 1 < 0) {
sum -= l + n - 1;
} else {
sum -= l;
}
writeln(sum);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
auto p = iota(n).map!(i => readln.chomp.to!int).array;
auto dp = new int[](n + 1);
int ans = 1<<30;
foreach (i ; 0 .. n) {
dp[p[i]] = dp[p[i] - 1] + 1;
ans = min(ans, n - p[i] + p[i] - dp[p[i]]);
}
debug {
writeln(dp);
}
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.