code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
int n = readint;
bool[int] m;
for (int i = 0; i < n; i++) {
int x = readint;
if (x in m) {
m[x] = !m[x];
}
else {
m[x] = true;
}
}
int ans = 0;
foreach (k, v; m) {
if (v)
ans++;
}
writeln(ans);
}
|
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[10 ^^ 5] dp;
dp[] = long.max;
dp[0] = 0;
long N, K;
scan(N, K);
auto h = aryread();
foreach (i; 0 .. N)
{
foreach (j; 1 .. K + 1)
{
if (i + j < N)
{
dp[i + j].minAssign(dp[i] + abs(h[i] - h[i + j]));
}
}
}
dp[N - 1].writeln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
const int DATA = 200;
int main() {
int[] digit = new int[DATA];
int i;
for(i=0; i<DATA; i++) {
string[] data = chomp(readln).split(" ");
if(stdin.eof()) break;
int val = to!(int)(data[0]) + to!(int)(data[1]);
int dig = 1;
while((val/=10) != 0) dig++;
digit[i] = dig;
}
for(int j=0; j<i; j++) writeln(digit[j]);
return 0;
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
int N = readln.chomp.to!int;
char[] S = readln.chomp.dup;
foreach(ref c; S) {
c = ((c - 'A' + N) % 26) + 'A';
}
S.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long H, N;
scan(H, N);
auto A = new long[](N);
auto B = new long[](N);
foreach (i; 0 .. N)
scan(A[i], B[i]);
auto dp = new long[](10 ^^ 4 * 2);
foreach (i; 1 .. dp.length)
dp[i] = long.max;
long ans = long.max;
foreach (i; 0 .. H)
foreach (j; 0 .. N)
if (dp[i] != long.max)
{
long damage = i + A[j];
dp[damage] = min(dp[i] + B[j], dp[damage]);
if (H <= damage)
{
ans = min(ans, dp[damage]);
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N;
scan(N);
auto a = new int[](N);
auto b = new int[](N);
auto c = new int[](N);
foreach (i ; 0 .. N) {
scan(a[i], b[i], c[i]);
}
auto dp = new int[][](N, 3);
dp[0][0] = a[0];
dp[0][1] = b[0];
dp[0][2] = c[0];
foreach (i ; 1 .. N) {
foreach (j ; 0 .. 3) {
if (j == 0) {
chmax(dp[i][j], max(dp[i - 1][1], dp[i - 1][2]) + a[i]);
}
else if (j == 1) {
chmax(dp[i][j], max(dp[i - 1][0], dp[i - 1][2]) + b[i]);
}
else {
chmax(dp[i][j], max(dp[i - 1][0], dp[i - 1][1]) + c[i]);
}
}
}
auto ans = max(dp[N - 1][0], dp[N - 1][1], dp[N - 1][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);
}
}
}
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
|
// Vicfred
// https://atcoder.jp/contests/abc162/tasks/abc162_f
// dynamic programming, cumulative sum
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
long n = readln.chomp.to!long;
long[] b = readln.split.map!(to!long).array;
long[] a;
a ~= 0;
a ~= b;
long[] cumulative = new long[n+1];
long[] dp = new long[n+1];
cumulative[1] = a[1];
for(long i = 3; i <= n; i++)
cumulative[i] = cumulative[i-2]+a[i];
for(long i = 2; i <= n; i++) {
if(i%2 == 1)
dp[i] = max(dp[i-1], dp[i-2]+a[i]);
else {
dp[i] = max(cumulative[i-1], dp[i-2]+a[i]);
}
}
dp[n].writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto abk = readints;
int a = abk[0], b = abk[1], k = abk[2];
for (int i = 0; i < k; i++) {
if (i % 2 == 0) {
if (a % 2 == 1) a--;
b += a / 2;
a /= 2;
}
else {
if (b % 2 == 1) b--;
a += b / 2;
b /= 2;
}
}
writeln(a, " ", b);
}
|
D
|
import std.stdio;
import std.algorithm;
import core.stdc.stdio;
import core.memory;
void main(){
while(true){
GC.collect;
int[][4][23] nextTown;
int[4] beTown;
beTown[] = -1;
int[10][10] mapData;
int[] dx = [0,-1,0,1];
int[] dy = [-1,0,1,0];
int w,h;
scanf("%d%d",&w,&h);
if(w==0&&h==0)
break;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
scanf("%d",&mapData[i][j]);
}
}
int tc=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(mapData[i][j] == 0){
mapData[i][j] = 255;
}else if(mapData[i][j] == 1){
mapData[i][j] = tc++;
}else{
mapData[i][j] = 114;
}
}
}
bool[] sl = new bool[tc];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(mapData[i][j] < 23){
for(int k=0;k<4;k++){
int ny=i+dy[k],nx=j+dx[k];
while(0<=nx&&nx<w&&0<=ny&&ny<h){
if(mapData[ny][nx] < 23)
nextTown[mapData[i][j]][k] ~= mapData[ny][nx];
ny += dy[k];
nx += dx[k];
}
}
}
if(mapData[i][j] == 114){
for(int k=0;k<4;k++){
int ny=i+dy[k],nx=j+dx[k];
while(0<=nx&&nx<w&&0<=ny&&ny<h){
if(mapData[ny][nx] < 23){
if(beTown[k] == -1)
beTown[k] = mapData[ny][nx];
sl[mapData[ny][nx]]=true;
}
ny += dy[k];
nx += dx[k];
}
}
}
}
}
int[int] now;
for(int k=0;k<4;k++){
if(beTown[k] >= 0){
now[(beTown[k]<<tc)|(1<<beTown[k])] = 1;
}
}
for(int _=1;_<tc;_++){
int[int] next;
foreach(int state,int num;now){
for(int k=0;k<4;k++){
foreach(s;nextTown[state>>tc][k]){
if((state&(1<<s))==0){
int idx = (s<<tc)|(1<<s)|(state&((1<<tc)-1));
int* np = idx in next;
if(np == null)
next[idx] = num;
else
(*np)+=num;
break;
}
}
}
}
swap(now,next);
}
int ans=0;
foreach(int state,int num;now){
if(sl[state>>tc]){
ans += num;
}
}
printf("%d\n",ans);
}
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;
import std.bigint;
bool is_prime(long x) {
if (x <= 1 || x % 2 == 0) return false;
long i = 3;
while (i*i <= x) {
if (x % i == 0)
return false;
i += 2;
}
return true;
}
void main() {
auto N = readln.chomp.to!long;
if (N == 2) writeln(1);
else if (N == 3) writeln(1);
else if (N == 5) writeln(1);
else if (N % 2 == 0) writeln(2);
else if (is_prime(N)) writeln(1);
else if (is_prime(N-2)) writeln(2);
else writeln(3);
}
|
D
|
void main() {
rs.count!(i => i == '2').writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
string s = readln.chomp;
int w;
scan(w);
foreach (i ; 0 .. (s.length.to!int + w - 1) / w ) {
write(s[i*w]);
}
writeln;
}
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, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, M;
scan(N, M);
auto a = readln.split.to!(int[]);
auto rem = N - a.sum();
writeln(rem >= 0 ? rem : -1);
}
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.ascii;
void main() {
auto d = readln.dup;
foreach (c; d) {
if (c.isLower) c.toUpper.write;
else c.toLower.write;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long N = lread();
long D, X;
scan(D, X);
long sum;
foreach (_; 0 .. N)
{
long A = lread();
sum += (D + (A - 1)) / A;
}
writeln(X + sum);
}
|
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()
{
char r, g, b;
scan(r, g, b);
string s = [r, g, b];
writeln((s.to!long % 4 == 0) ? "YES" : "NO");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N;
scan(N);
auto A = readln.split.to!(int[]);
auto B = readln.split.to!(int[]);
auto C = readln.split.to!(int[]);
auto ans = B.sum();
foreach (i ; 0 .. N - 1) {
if (A[i] == N) continue;
ans += (A[i] + 1 == A[i + 1]) * C[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);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main()
{
long x = rdElem;
long now = 100;
long year;
while (now < x)
{
now += now / 100;
++year;
}
year.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int[] a) {
int ans = 1;
int h = a[0];
for (int i = 1; i < a.length; i++) {
if (h <= a[i]) ans++;
h = max(h, a[i]);
}
return ans;
}
void main() {
readint;
auto a = readints;
writeln(calc(a));
}
|
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 main() {
writeln("ABC", readln.chomp);
}
// ----------------------------------------------
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
int hi = N;
int lo = 1;
foreach (i; 0..M) {
s = readln.split.map!(to!int);
lo = max(lo, s[0]);
hi = min(hi, s[1]);
}
if (lo > hi) {
writeln(0);
} else {
writeln(hi - lo + 1);
}
}
|
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 cntC;
int pos;
if (S.front != 'A') {
writeln("WA");
return;
}
foreach (i; 2 .. S.length - 1) {
if (S[i] == 'C') {
cntC++;
pos = to!int(i);
}
}
if (cntC != 1) {
writeln("WA");
return;
}
foreach (i; 0 .. S.length) {
if (i != 0 && i != pos) {
if ('A' <= S[i] && S[i] <= 'Z') {
writeln("WA");
return;
}
}
}
writeln("AC");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD-1;
auto B = RD;
auto C = RD;
auto D = RD;
auto cnt1 = B / C;
auto cnt2 = B / D;
auto cnt3 = B / lcm(C, D);
auto cnt4 = A / C;
auto cnt5 = A / D;
auto cnt6 = A / lcm(C, D);
auto ans = B - (cnt1 + cnt2) + cnt3;
ans -= A - (cnt4 + cnt5) + cnt6;
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
void main()
{
for(string S; (S=readln().chomp()).length; )
{
auto nm = S.split().map!(to!int)();
auto n=nm[0], m=nm[1];
auto d = new int[][](n,n);
foreach(i;0..m)
{
auto abc = readln().split().map!(to!int)();
d[abc[0]-1][abc[1]-1]=abc[2];
}
foreach(k;0..n)
foreach(i;0..n)
foreach(j;0..n)
{
auto c = min(d[i][k],d[k][j]);
d[i][j] += c;
d[i][k] -= c;
d[k][j] -= c;
}
auto t = 0;
foreach(i;0..n)
foreach(j;0..n)
t += d[i][j];
writeln(t);
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main(){
auto a=map!(to!int)(readln.chomp.split);
if(a[0]>a[1])
writeln("a > b");
else if(a[0]<a[1])
writeln("a < b");
else
writeln("a == b");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto a = readln[0];
switch (a) {
case 'A': .. case 'Z':
writeln("A");
return;
default:
writeln("a");
return;
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), n = rd[0], k = rd[1];
writeln(k * (k-1) ^^ (n-1));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
writeln(readln.chomp.count!"a == 'o'" * 100 + 700);
}
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;
void main()
{
bool flg = false;
bool[char] num;
foreach(char a;readln.chomp())
if(a in num) num[a] = !num[a];
else num[a] = true;
foreach(bool a;num)
flg |= a;
writeln(flg? "No" : "Yes");
}
|
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 ps = new int[](10^^6+1);
foreach (i; 2..10^^6+1) if (ps[i] == 0) {
auto x = i;
while (x <= 10^^6) {
ps[x] = i;
x += i;
}
}
auto N = readln.chomp.to!int;
auto ns = new int[](10^^6+1);
foreach (a; readln.split.to!(int[])) {
while (a != 1) {
auto p = ps[a];
ns[p] += 1;
while (a % p == 0) a /= p;
}
}
auto pc = true;
foreach (p; ns) {
if (p == N) {
writeln("not coprime");
return;
} else if (p > 1) {
pc = false;
}
}
writeln(pc ? "pairwise coprime" : "setwise coprime");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
string N = sread();
auto dp = new long[][](2, N.length + 1);
dp[0][] = long.min;
dp[1][] = long.min;
dp[0][0] = 0;
foreach (i; 0 .. N.length)
{
dp[0][i + 1] = dp[0][i + 1].max(dp[0][i] + N[i] - '0');
dp[1][i + 1] = dp[1][i + 1].max(dp[0][i] + N[i] - '0' - 1, dp[1][i] + 9);
}
writeln(max(dp[0][N.length], dp[1][N.length]));
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner, dcomp.array;
void main() {
auto sc = new Scanner(stdin);
int x, y, z;
sc.read(x, y, z);
string[] s;
foreach (i; 0..x) {
s ~= "a";
}
foreach (i; 0..y) {
s ~= "b";
}
foreach (i; 0..z) {
s ~= "c";
}
while (s.length >= 2) {
s.sort!"a<b";
string u = s.front ~ s.back;
s[0] = u;
s.popBack;
}
writeln(s[0]);
return;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.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);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
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 {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.bitmanip;
void main()
{
auto n = readln.chomp.to!size_t;
auto ai = readln.split.to!(int[]);
auto q = readln.chomp.to!size_t;
auto mi = readln.split.to!(int[]);
auto ri = new bool[](q);
foreach (i; 0..1 << n) {
auto s = ai.indexed(i.bitsSet).sum;
foreach (j, m; mi)
if (s == m) ri[j] = true;
}
foreach (r; ri)
writeln(r ? "yes" : "no");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp, n = s.length;
writeln((n % 2 == 0) ^ (s[0] == s[$-1]) ? "Second" : "First");
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], m = tmp[1];
int[][] s = new int[][](m);
foreach (i; 0 .. m) {
int[] ss = readln.split.to!(int[]);
ss = ss[1..$];
foreach (x; ss) {
s[i] ~= x - 1;
}
}
int[] p = readln.split.to!(int[]);
int ans;
for (int i = 0; i < (1 << n); ++i) {
bool ok;
foreach (j; 0 .. m) {
int cnt;
foreach (x; s[j]) {
if ((i >> x) & 1) ++cnt;
}
cnt %= 2;
if (cnt != p[j]) ok = true;
}
if (!ok) ++ans;
}
ans.writeln;
}
|
D
|
import std;
import core.bitop;
// dfmt off
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[]);
void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); }
alias Pair = Tuple!(long, "H", long, "W");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto n = lread();
auto a = aryread();
auto s = new long[](n + 1);
iota(n).each!(i => s[i + 1] = s[i] + a[i]);
long[long] counts;
s.each!(x => counts[x] = (x in counts ? counts[x] + 1 : 1));
counts.byValue.map!(x => x * (x - 1) / 2).sum.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.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
readln;
readln.chomp.split.array.reverse.join(" ").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;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
long M;
long query(long[] A, long lhs, long rhs, long clhs, long crhs, long current = 1)
{
if (lhs == rhs)
return long.min;
if (rhs <= clhs || crhs <= lhs)
return long.min;
if (lhs <= clhs && crhs <= rhs)
return A[current];
long m = (lhs + rhs) / 2;
long cm = (clhs + crhs) / 2;
long l = query(A, lhs, rhs, clhs, cm, current * 2);
long r = query(A, lhs, rhs, cm, crhs, current * 2 + 1);
return max(l, r);
}
void main()
{
long N = lread();
M = 2 ^^ (cast(long) ceil(log2(N)));
auto A = new long[](2 * M);
A[] = long.min;
foreach (i; 0 .. N)
{
A[A.length - M + i] = lread();
}
foreach_reverse (d; 0 .. cast(long) ceil(log2(N)))
{
foreach (i; 0 .. 2 ^^ d)
{
long p = 2 ^^ d + i;
A[p] = max(A[p * 2], A[p * 2 + 1]);
}
}
foreach (i; 0 .. N)
{
writeln(query(A, 0, i, 0, M).max(query(A, i + 1, M, 0, M)));
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), x = rd[0], a = rd[1], b = rd[2];
if (b-a <= 0)
writeln("delicious");
else if (b-a <= x)
writeln("safe");
else
writeln("dangerous");
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void theCode(){
int n = scan!int;
ll[] arr = scanArray;
ll minn = arr[0];
for(int i = 0; i < n; ++i){
minn = minn & arr[i];
}
writeln(minn);
}
void main(){
long tests = scan; // Toggle!
while(tests--) theCode();
}
/********* That's All Folks! *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
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;
auto S = RD!string;
long ans;
foreach (i; 1..N-1)
{
auto cnt1 = new bool[](26);
foreach (j; 0..i)
{
cnt1[S[j]-'a'] = true;
}
auto cnt2 = new bool[](26);
foreach (j; i..N)
{
cnt2[S[j]-'a'] = true;
}
long cnt;
foreach (j; 0..26)
{
if (cnt1[j] && cnt2[j])
++cnt;
}
ans = max(ans, cnt);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int d, int[][] a) {
int ans = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
int len = 0;
for (int k = 0; k < d; k++) {
len += abs(a[i][k] - a[j][k]) ^^ 2;
}
int p = 1;
while (p * p <= len) {
if (p * p == len) {
ans++;
break;
}
p++;
}
}
}
return ans;
}
void main() {
int n, d; scan(n, d);
int[][] a;
foreach (_; 0..n) a ~= readints;
writeln(calc(d, a));
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
if (n % 2 == 0)
{
writeln((n / 2) - 1);
}
else
{
writeln((n - 1) / 2);
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
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 A, B, C, K;
scan(A, B, C, K);
writeln(min(A, K) - min(C, max(0, K - A - B)));
}
|
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()
{
auto S = sread();
auto K = "keyence";
foreach (i; 0 .. S.length + 1)
foreach (j; i .. S.length + 1)
{
if (K == S[0 .. i] ~ S[j .. $])
{
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
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 N = nk[0];
auto K = nk[1];
auto bs = 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;
if (xyc[2] == "B") {
bs[y%(K*2)][x%(K*2)] += 1;
} else {
bs[y%(K*2)][(x+K)%(K*2)] += 1;
}
}
foreach (i; 0..K*2) {
foreach (j; 0..K*2) {
if (i > 0) bs[i][j] += bs[i-1][j];
if (j > 0) bs[i][j] += bs[i][j-1];
if (i > 0 && j > 0) bs[i][j] -= bs[i-1][j-1];
}
}
int r;
auto m = K*2-1;
foreach (i; 0..K) {
foreach (j; 0..K) {
r = max(r,
bs[m][m] - bs[K+i][m] - bs[m][K+j] + bs[K+i][K+j]
+ bs[K+i][K+j] - bs[i][K+j] - bs[K+i][j] + bs[i][j]
+ bs[i][m] - bs[i][K+j]
+ bs[m][j] - bs[K+i][j]
+ bs[i][j]
);
r = max(r,
bs[K+i][m] - bs[K+i][K+j] - bs[i][m] + bs[i][K+j]
+ bs[m][K+j] - bs[K+i][K+j] - bs[m][j] + bs[K+i][j]
+ bs[i][K+j] - bs[i][j]
+ bs[K+i][j] - bs[i][j]
);
}
}
writeln(r);
}
|
D
|
void main(){
int n = _scan();
string[] st = readln().chomp().split();
char[] ans;
foreach(i; 0..n){
ans ~= st[0][i];
ans ~= st[1][i];
}
ans.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
|
void main()
{
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], k = tmp[1];
writeln(n - k + 1);
}
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.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; }
/*
a[i]: i番目を踏むときの最善
漸化式は
a[i + 1] = min(a[i - 1] + abs(h[i + 1] - h[i - 1]), a[i] + abs(h[i + 1] - h[i]))
*/
void main(){
long n = read.to!long;
long[] hs = readln.chomp.split.map!(to!long).array;
long[] as = [0, abs(hs[1] - hs[0])];
foreach(i; 2 .. n){
as ~= min(as[i - 2] + abs(hs[i] - hs[i - 2]), as[i - 1] + abs(hs[i] - hs[i - 1]));
}
as[n - 1].writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto rdx = readln.split.to!(long[]);
auto r = rdx[0];
auto D = rdx[1];
auto x = rdx[2];
foreach (_; 0..10) {
x = r*x-D;
writeln(x);
}
}
|
D
|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
void main()
{
long N;
scan(N);
// writeln(N);
long 今の時間 = 0;
long 今のx座標 = 0;
long 今のy座標 = 0;
long dist = 0;
bool check = true;
long time = 0;
foreach (i; 0 .. N)
{
long T, X, Y;
scan(T, X, Y);
// writeln(T, X, Y);
time = T - 今の時間; //3=6-0
// writeln("TT:", T);
// writeln("tt", t);
// writeln("time", time);
dist = abs(X - 今のx座標) + abs(Y - 今のy座標);
//writeln("dist:", dist);
if (dist > time)
{
check = false;
}
if ((time - dist) % 2 != 0)
{
check = false;
}
今のx座標 = X;
今のy座標 = Y;
今の時間 = T;
}
if (check)
{
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;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
auto A = [0L] ~ aryread();
A ~= 0;
auto D = new long[](N + 1);
foreach (i; 0 .. N + 1)
{
D[i] = abs(A[i + 1] - A[i]);
}
long sum = D.sum();
// writeln(A);
// writeln(D);
// writeln(sum);
foreach (i; 0 .. N)
{
writeln(sum - (D[i] + D[i + 1]) + abs(A[i + 2] - A[i]));
}
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!int;
const X = scan!int;
const Y = scan!int;
void solve() {
int[int] counts;
foreach(i; 1..N) {
foreach(j; i+1..N+1) {
auto liner = j - i;
auto linerIToX = X - i;
if (linerIToX < 0) linerIToX *= -1;
auto winded = j - Y;
if (winded < 0) winded *= -1;
winded += linerIToX + 1;
deb([i, j, liner, winded]);
auto distance = min(liner, winded);
counts[distance]++;
}
}
foreach(i; 1..N) {
writeln(i in counts ? counts[i] : 0);
}
}
solve();
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
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.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 edges = new int[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
edges[s[0]-1] ~= s[1]-1;
edges[s[1]-1] ~= s[0]-1;
}
if (N == 2) {
writeln("Second");
return;
} else if (N == 3) {
writeln("First");
return;
}
int root;
foreach (i; 0..N) {
if (edges[i].length > 1) {
root = i;
break;
}
}
bool first = false;
int dfs(int n, int p) {
int[] tmp;
foreach (m; edges[n]) if (m != p)
tmp ~= dfs(m, n);
if (tmp.sum > 1)
first = true;
//writeln(n, tmp);
return tmp.sum ? 0 : 1;
}
if (dfs(root, -1) == 1)
first = true;
writeln( first ? "First" : "Second" );
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a, b;
scan(a, b);
if (a >= 13) {
writeln(b);
}
else if (a >= 6) {
writeln(b / 2);
}
else {
writeln(0);
}
}
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 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, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = aryread();
auto ary = new long[](MAX);
foreach (e; a)
{
ary[e]++, ary[e + 1]++, ary[e + 2]++;
}
ary.reduce!(max).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
|
/* 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;
/*}}}*/
/* libnum {{{*/
void amax(N, Args...)(ref N x, Args y) {
x = x.max(y);
}
void amin(N, Args...)(ref N x, Args y) {
x = x.min(y);
}
auto maxE(R)(R r) if (isInputRange!R) {
ElementType!R x = r.front;
r.popFront();
foreach (y; r) x.amax(y);
return x;
}
auto minE(R)(R r) if (isInputRange!R) {
ElementType!R x = r.front;
r.popFront();
foreach (y; r) x.amin(y);
return x;
}
/*}}}*/
/+---test
5
10 4 8 7 3
---+/
/+---test
7
4 4 5 6 6 5 5
---+/
/+---test
4
1 2 3 4
---+/
void main(string[] args) {
readln;
const H = readln.split.map!(to!long).array;
auto dp = new long[H.length];
dp[$-1] = 0;
foreach_reverse (i; 0..H.length-1) {
if (H[i] >= H[i+1]) dp[i] = dp[i+1] + 1;
}
dp.maxE.writeln;
}
|
D
|
void main() {
auto S = rs;
("A" ~ S[8] ~ "C").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, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n, a, b;
scan(n, a, b);
writeln(min(a*n, 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
|
void main() {
problem();
}
void problem() {
auto D = scan!int;
auto T = scan!int;
auto S = scan!int;
bool solve() {
return T * S >= D;
}
writeln(solve() ? "Yes" : "No");
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto nab = readints;
int n = nab[0], a = nab[1], b = nab[2];
int ans = 0;
for (int i = 1; i <= n; i++) {
int s = 0;
int x = i;
while (x > 0) {
s += x % 10;
x /= 10;
}
if (a <= s && s <= b) ans += i;
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD!uint;
auto a = RDR.ARR;
auto cnt_l = new long[](N+1);
{
long last;
foreach_reverse (i; 0..N)
{
if (a[i] < last)
cnt_l[i] = cnt_l[i+1] + 1;
last = a[i];
}
}
auto cnt_r = new long[](N+1);
{
long last;
foreach (i; 0..N)
{
if (a[i] < last)
cnt_r[i+1] = cnt_r[i] + 1;
last = a[i];
}
}
debug writeln(cnt_l);
debug writeln(cnt_r);
string ans;
long last;
uint l, r = cast(uint)(a.length);
void popL()
{
last = a.front;
a.popFront;
ans ~= "L";
++l;
}
void popR()
{
last = a.back;
a.popBack;
ans ~= "R";
--r;
}
while (!a.empty)
{
if (a.front > last && a.front == a.back)
{
debug writeln(cnt_l[l], " ", cnt_r[r]);
if (cnt_l[l] >= cnt_r[r])
{
popL();
}
else
{
popR;
}
}
else if (a.front > last && a.front < a.back)
{
popL();
}
else if (a.back > last && a.back < a.front)
{
popR();
}
else if (a.front > last)
{
popL();
}
else if (a.back > last)
{
popR();
}
else
{
break;
}
}
writeln(ans.length);
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b; readV(a, b);
if (a == 1) a = 14;
if (b == 1) b = 14;
writeln(a > b ? "Alice" : a < b ? "Bob" : "Draw");
}
|
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 s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto A = readln.split.map!(to!int).array;
A[] -= 1;
auto cnt = new int[][](M);
foreach (i; 1..N) cnt[A[i]] ~= i;
auto imos = new long[](M * 2);
foreach(i; 0..N-1) {
if (A[i] <= A[i+1]) {
imos[A[i] + 1] += 1;
imos[A[i + 1] + 1] -= 1;
} else {
imos[A[i] + 1] += 1;
imos[A[i + 1] + M + 1] -= 1;
}
}
foreach (i; 0..2*M-1) imos[i+1] += imos[i];
debug imos.writeln;
long ans = 0;
foreach (i; 0..N-1) {
ans += min((A[i+1] + M - A[i]) % M, A[i+1] + 1);
}
long tmp = ans;
debug tmp.writeln;
foreach (x; 0..M-1) {
foreach (i; cnt[x]) tmp += (A[i] + M - A[i-1]) % M;
tmp -= imos[x] + imos[x+M];
debug tmp.writeln;
ans = min(ans, tmp);
}
ans.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;
auto b=scanElem;
writeln((a+b)%24);
}
|
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;
auto M = RD;
auto X = RD;
auto A = RDR.ARR;
long ans1, ans2;
foreach (i; 0..M)
{
if (A[i] < X)
{
++ans1;
}
else
{
++ans2;
}
}
writeln(min(ans1, ans2));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
int n,s;
int saiki(int n1,int u,int m){
if(n1 > s) return 0;
else if(m==1){
int d = s-n1;
if(d <= 9 && (u & (1<<d))==0) return 1;
else return 0;
}else if(m==2){
if(s-n1>26) return 0;
}else if(m==3){
if(s-n1>50) return 0;
}else if(m==4){
if(s-n1>80) return 0;
}else if(m==5){
if(s-n1>115) return 0;
}else if(m==6){
if(s-n1>154) return 0;
}
int ans;
for(int i=9;i>=0;i--){
if((u & (1<<i))==0){
ans += saiki(n1+m*i,u|(1<<i),m-1);
}
}
return ans;
}
void main(){
while(true){
auto ss = readln();
if(stdin.eof()) break;
int[] s1 = ss.split().to!(int[]);
n = s1[0];
s = s1[1];
if(n <= 330)
writeln(saiki(0,0,n));
else writeln(0);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n, x; scan(n, x);
auto a = readints;
int ans = 1;
int d = 0;
for (int i = 0; i < n; i++) {
d += a[i];
if (d <= x) ans++;
}
writeln(ans);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
for (;;) {
auto rd = readln.split.map!(to!int), n = rd[0], x = rd[1];
if (n == 0 && x == 0) break;
auto r = 0;
foreach (i; 1..n+1)
foreach (j; i+1..n+1) {
auto k = x - i - j;
if (k > j && k <= n) ++r;
}
writeln(r);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto x = readln.split[1].to!long;
auto as = readln.split.to!(long[]);
auto last = as[0];
long let = 0;
foreach (a; as[1..$]) {
long d;
if (a+last > x) {
d = (a+last) - x;
let += d;
}
last = a-d < 0 ? 0 : a-d;
}
writeln(let);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n, m;
scan(n, m);
if(n == m)
writeln("Yes");
else
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.math, std.random, std.range;
void main()
{
const MOD = 1000000007;
int[] input = readln().split.to!(int[]);
int H = input[0];
int W = input[1];
int K = input[2];
int[][] dp;
dp.length = W;
dp.each!((ref a)=>a.length=H+1);
dp[0][0] = 1;
foreach(h; 0..H)
{
foreach(w; 0..W)
{
for(int i = 0;i < 1 << (W-1);i++)
{
bool ok = true;
for(int k = 0;k < W-2;k++)
{
if((i >> k & 1) && (i >> k+1 & 1))
{
ok = false;
}
}
if(ok == false)
{
continue;
}
//左側
if(w >= 1 && i >> w-1 & 1)
{
dp[w-1][h+1] += dp[w][h];
dp[w-1][h+1] %= MOD;
} //右側
else if(w <= W-2 && i >> w & 1)
{
dp[w+1][h+1] += dp[w][h];
dp[w+1][h+1] %= MOD;
} //移動しない
else
{
dp[w][h+1] += dp[w][h];
dp[w][h+1] %= MOD;
}
}
}
}
writeln(dp[K-1][$-1]);
}
|
D
|
import std.conv;
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
void main() {
auto vars = readln.split.map!(to!int).array;
auto idxTask = iota(1, vars[0] + 1).map!(a => a * 5).array;
auto sumFoldTask = cumulativeFold!((a, b) => a + b)(idxTask).array;
int count;
foreach (idx, el; sumFoldTask) {
if (240 - el < vars[1]) {
break;
}
++count;
}
writeln(count);
}
|
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];
if (K > N) {
writeln(0);
return;
}
long r;
foreach (e; 0..K) {
(r += comb(K, e) * pow(K-e, N) % P * (-1)^^e + P) %= P;
}
writeln(r);
}
|
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) {
auto a = r.nextA!ulong(3);
sort (a);
auto s = sum (a);
long m = (s + 1) / 2;
if (a[2] > m) {
writeln ("No");
} else {
writeln ("Yes");
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto A = new long[][](3);
foreach (i; 0 .. 3)
A[i] = aryread();
long N = lread();
auto B = new bool[][](3, 3);
foreach (_; 0 .. N)
{
long b = lread();
foreach (i; 0 .. 3)
foreach (j; 0 .. 3)
if (A[i][j] == b)
{
B[i][j] = true;
}
}
bool a = B[0][0] && B[0][1] && B[0][2];
bool b = B[1][0] && B[1][1] && B[1][2];
bool c = B[2][0] && B[2][1] && B[2][2];
bool d = B[0][0] && B[1][0] && B[2][0];
bool e = B[0][1] && B[1][1] && B[2][1];
bool f = B[0][2] && B[1][2] && B[2][2];
bool g = B[0][0] && B[1][1] && B[2][2];
bool h = B[0][2] && B[1][1] && B[2][0];
if (a || b || c || d || e || f || g || h)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional;
void main()
{
auto x = readln.chomp.to!long;
if (x < 7) {
writeln(1);
} else if (x < 12) {
writeln(2);
} else {
auto ret = (x / 11) * 2;
writeln(ret + (x%11 == 0 ? 0 : x%11 > 6 ? 2 : 1));
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(string s) {
int n = cast(int)s.length;
// dp[i][j] = i 番目と j 番目から始まる文字列の共通の長さ
auto dp = new int[][](n, n);
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (s[i] == s[j]) {
if (i + 1 < n && j + 1 < n)
dp[i][j] = dp[i + 1][j + 1] + 1;
else
dp[i][j] = 1;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans = max(ans, min(j - i, dp[i][j]));
}
}
return ans;
}
void main() {
readint;
string s = read!string;
writeln(calc(s));
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;
int ri () {
return readln.stripRight.to!int;
}
string slow (string z, int k) {
auto s = z.dup;
for (int i = 0; i + k <= s.length; ++i) {
reverse (s[i .. i + k]);
}
return s.idup;
}
void research (int k) {
string p = "1234567890";
foreach (i; 1 .. k + 1) {
stderr.writeln (slow (p[0 .. k], i));
}
}
void reseach0 () {
research (5);
stderr.writeln;
research (6);
stderr.writeln;
research (7);
stderr.writeln;
research (9);
}
void main() {
debug reseach0 ();
const nt = ri ();
foreach (tid; 0 .. nt) {
const n = ri ();
const s = readln.stripRight;
auto z = new byte[2*n];
foreach (i; 0 .. 2*n) z[i] = s[i%n].to!byte;
int best = n - 1;
byte[] ans = z[0 .. n].dup;
reverse (ans);
auto u = new byte[n];
foreach_reverse (k; 0 .. n - 1) {
u[] = z[k .. k + n][];
/*
123456 0
234561 1
345612 2
456321 3
561234 4
654321 5
123456789 0
234567891 1
345678921 2
456789123 3
567894321 4
678912345 5
789654321 6
891234567 7
987654321 8
*/
if ((k & 1) != (n & 1)) {
reverse (u[n - k .. n]);
}
if (cmp (u, ans) <= 0) {
ans = u.dup;
best = k;
}
}
foreach (i; ans) {
write(i.to!char);
}
writeln;
writeln (best+1);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto R = readln.chomp.to!int;
auto G = readln.chomp.to!int;
writeln(2 * G - R);
}
|
D
|
import std.stdio, std.conv, std.string, std.array;
void main(){
int check,cnt=0;
auto n = readln.chomp.to!int;
for(uint i;i<n;++i){
check = 0;
auto s = readln.chomp.to!int;
for(uint x=1;x*x<s;++x){
if((s+x+1)%(x*2+1)==0){
check=1;
break;
}
}
if(check==0) cnt++;
}
cnt.writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto mns=new int[](8);
fill(mns, 1000000000);
foreach(_; 0..n){
auto args=readln.split.to!(char[][]);
int kind=0;
foreach(c; args[1]){
kind^=(1<<(c-'A'));
}
mns[kind]=min(mns[kind], args[0].to!(int));
}
struct T{int kind, price;}
T[] as, bs, cs;
foreach(bit; 1..(1<<3)){
if(mns[bit]==1000000000) continue;
if(bit&(1<<0)) as~=T(bit, mns[bit]);
if(bit&(1<<1)) bs~=T(bit, mns[bit]);
if(bit&(1<<2)) cs~=T(bit, mns[bit]);
}
as~=T(0, 0); bs~=T(0, 0); cs~=T(0, 0);
int mincost=1000000000;
foreach(a; as){
foreach(b; bs){
foreach(c; cs){
int bit=a.kind|b.kind|c.kind;
if(bit==((1<<3)-1)){
mincost=min(mincost, a.price+b.price+c.price);
}
}
}
}
if(mincost<1000000000){
writeln(mincost);
}else{
writeln(-1);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import 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, M;
scan(N, M);
auto a = new int[](M);
auto open = new int[](M);
foreach (i ; 0 .. M) {
int ai, bi;
scan(ai, bi);
a[i] = ai;
auto t = readln.split.to!(int[]);
t[] -= 1;
foreach (ti ; t) {
open[i] |= (1 << ti);
}
}
auto dp = new int[](1 << N);
dp[] = inf;
dp[0] = 0;
foreach (s ; 0 .. 1 << N) {
foreach (i ; 0 .. M) {
chmin(dp[s | open[i]], dp[s] + a[i]);
}
}
debug {
writeln(dp);
}
auto ans = dp[(1 << N) - 1];
writeln(ans < inf ? ans : -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);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.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 main() {
readln.chomp.startsWith("YAKI").pipe!(a=>a?"Yes":"No").writeln;
}
// ----------------------------------------------
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
int N = readln.chomp.to!int;
string S = readln.chomp;
int ans = 0;
char prev = '0';
foreach(c; S) {
if(c != prev) ans++;
prev = c;
}
ans.writeln;
}
|
D
|
void main() {
auto s = ri;
int[] a;
a ~= s;
while(true) {
auto v = a.back;
auto p = f(v);
if(a.canFind(p)) {
writeln(a.length+1);
return;
}
else a ~= p;
}
}
int f(int n) {
if(n % 2 == 0) return n / 2;
else return 3*n + 1;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
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 x = readint;
int a = readint;
int b = readint;
/*
int donut = (x - a) / b;
writeln(x - a - b * donut);
*/
writeln((x - a) % b);
}
|
D
|
module simple;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, core.stdc.string;
immutable long MOD = 998244353;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
if (A[0] != 0 || A.map!(a => a == 0).sum >= 2) {
writeln(0);
return;
}
auto D = new long[](N+10);
foreach (a; A) D[a] += 1;
foreach (i; 1..N) {
if (D[i-1] > 0 && D[i+1] > 0 && D[i] == 0) {
writeln(0);
return;
}
}
long ans = 1;
foreach (i; 1..N) {
if (D[i] == 0) break;
ans = ans * powmod(D[i-1], D[i], MOD) % MOD;
}
ans.writeln;
}
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
foreach (i; 0 .. s.length) {
if (s == t) {
writeln("Yes");
return;
}
s = s[$ - 1] ~ s[0 .. ($ - 1)];
}
writeln("No");
}
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
long f(long n)
{
long c = 0;
while (n != 0)
{
n /= 10;
c++;
}
return c;
}
void main()
{
long N = lread();
long cnt;
foreach (x; 1 .. N + 1)
{
if (f(x) & 1)
cnt++;
}
writeln(cnt);
}
|
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 = new long[][](2);
foreach (i; 0 .. N)
A[i & 1] ~= lread();
A[0].sort();
A[1].sort();
long[2] jj;
long ans;
// auto S = new long[](N);
// foreach (i; 0 .. N)
// {
// S[i] = A[i & 1][jj[i & 1]++];
// }
// jj[] = 0;
// writeln(S);
// S.sort();
// writeln(S);
foreach (i; 0 .. N)
{
if (A[1].length <= jj[1] || (jj[0] < A[0].length && A[0][jj[0]] < A[1][jj[1]]))
{
long j = jj[0] * 2;
ans += (i & 1) == 1;
// writeln(j - i);
jj[0]++;
}
else
{
long j = jj[1] * 2 + 1;
ans += (i & 1) == 0;
// writeln(j - i);
jj[1]++;
}
}
writeln(ans / 2);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main() {
auto data = readln().split();
auto A = data[0].to!int(), B = data[1].to!int(), K = data[2].to!int();
int[] list;
foreach_reverse(i; 1 .. min(A, B)+1) {
if (A % i == 0 && B % i == 0) list ~= i;
}
writeln(list[K-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;
long f(long n) {
if (n <= 0) return 0;
// 0, 1, 2, ..., n の排他的論理和を求める
if (n % 2 == 1) {
if ((n + 1) / 2 % 2 == 0) return 0; // 偶数奇数のペアが偶数個
return 1;
}
else {
// 0, 1, 2, ..., (n - 1) までの偶数奇数のペアが偶数個
if (n / 2 % 2 == 0) return n;
return n ^ 1;
}
}
long calc(long a, long b) {
return f(a - 1) ^ f(b);
}
void main() {
auto ab = reads!long;
long a = ab[0], b = ab[1];
auto ans = calc(a, b);
writeln(ans);
}
|
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() {
foreach(int i;1..10) foreach(int j; 1..10) {
writeln(i, "x", j, "=", i*j);
}
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0] - 1, b = tmp[1] - 1;
writeln(a * b);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n; scan(n);
auto b = readints;
int ans = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
ans += b[i];
}
else if (i == n - 1) {
ans += b[i - 1];
}
else {
ans += min(b[i - 1], b[i]);
}
}
writeln(ans);
}
|
D
|
void main() {
writeln(readln.chomp.to!double.sqrt.to!int ^^ 2);
}
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.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable i = readln.split.to!(ulong[]);
immutable R = i[0], G = i[1], B = i[2], N = i[3];
ulong cnt = 0;
for(ulong r = 0; r <= N / R; r++) {
const ulong r_val = r * R;
if (r_val > N) break;
else if (r_val == N) cnt++;
else {
for(ulong g = 0; g <= N / G; g++) {
const ulong g_val = r*R + g*G;
if (g_val > N) break;
else if (g_val == N) cnt++;
else {
const ulong rem = (N - g_val) % B;
if (rem == 0) {
cnt++;
}
}
}
}
}
writeln(cnt);
}
|
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!(x => x.to!int-1).array;
auto st = new SegmentTree!(int, (a,b)=>a+b, 0)(N);
int ans = 0;
foreach (i, a; A) {
if (st.query(a, N-1) == i.to!int) ans += 1;
st.add(a, 1);
}
ans.writeln;
}
class SegmentTree(T, alias op, T e) {
T[] table;
int size;
int offset;
this(int n) {
size = 1;
while (size <= n) size <<= 1;
size <<= 1;
table = new T[](size);
fill(table, e);
offset = size / 2;
}
void assign(int pos, T val) {
pos += offset;
table[pos] = val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
void add(int pos, T val) {
pos += offset;
table[pos] += val;
while (pos > 1) {
pos /= 2;
table[pos] = op(table[pos*2], table[pos*2+1]);
}
}
T query(int l, int r) {
return query(l, r, 1, 0, offset-1);
}
T query(int l, int r, int i, int a, int b) {
if (b < l || r < a) {
return e;
} else if (l <= a && b <= r) {
return table[i];
} else {
return op(query(l, r, i*2, a, (a+b)/2), query(l, r, i*2+1, (a+b)/2+1, b));
}
}
}
|
D
|
import std.stdio; void main () {writeln ("NO");}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.