code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.algorithm,
std.string,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
string s = readln.chomp;
writeln(s[0], s[1..$-1].length, s[$-1]);
}
| D |
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n, k;
scanf("%d%d", &n, &k);
long ans = 0;
long f(long x) {
return x*x*x;
}
ans += f(n/k);
if (k%2 == 0) ans += f((n+k/2)/k);
writeln(ans);
} | D |
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
auto d = readln.chomp.to!(char[]);
d.reverse;
d.to!string.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;
bool solve() {
auto s = readln.split.map!(to!int);
auto P = s[0];
auto Q = s[1];
auto A = s[2];
auto N = s[3];
if (P == 0) return false;
long dfs(long p, long q, int n, long prod, long prev) {
if (p == 0) return 1;
if (prod * prev > A) return 0;
if (n == N) return 0;
if (p * prev > q * (N - n)) return 0;
long ret = 0;
for (long i = prev; prod * i <= A; ++i) {
long x = i * p - q;
long y = i * q;
if (x < 0) continue;
if (x != 0 && prod * i * i > A) continue;
long g = gcd(x, y);
ret += dfs(x / g, y / g, n + 1, prod * i, i);
}
return ret;
}
dfs(P, Q, 0, 1, 1).writeln;
return true;
}
void main() {
while (solve) {}
}
| 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;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long X = lread();
long l = 1;
long r = 50000;
while (1 < r - l)
{
long m = (l + r) / 2;
if (m * (m + 1) / 2 < X)
{
l = m;
}
else
{
r = m;
}
}
writeln((l * (l + 1) / 2 == X) ? l : r);
}
| 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()
{
long N, K;
scan(N, K);
long R, S, P;
scan(R, S, P);
string T = sread();
auto dp = new long[][](N + 1 + K, 3);
foreach (i; 0 .. N)
{
dp[i + K][0] = max(dp[i][1], dp[i][2]) + ((T[i] == 's') ? R : 0);
dp[i + K][1] = max(dp[i][0], dp[i][2]) + ((T[i] == 'p') ? S : 0);
dp[i + K][2] = max(dp[i][0], dp[i][1]) + ((T[i] == 'r') ? P : 0);
}
long ans;
foreach (i; 0 .. K)
{
ans += max(dp[N + K - 1 - i][0], dp[N + K - 1 - i][1], dp[N + K - 1 - i][2]);
}
writeln(ans);
}
| D |
import std.stdio, std.range, std.conv, std.string, std.array, std.functional, std.math;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
import std.container.binaryheap;
import std.typecons;
void main()
{
const input = readln().strip.to!(long);
writeln(input/2 * (input/2 + input%2));
} | 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[] NK = readln.chomp.split.to!(int[]);
int N = NK[0],
K = NK[1];
int[] A = readln.chomp.split.to!(int[]);
int ans = 1;
ans += (N - K) / (K - 1);
if ((N - K) % (K - 1) != 0) {
ans++;
}
writeln(ans);
}
| D |
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
import std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long n = read.to!long;
long a = read.to!long;
long b = read.to!long;
long ans;
if(n == 1 && a < b) ans = 0;
else if(a > b) ans = 0;
else ans = (a + b * (n -1)) - (a * (n - 1) + b) + 1;
ans.writeln;
}
| D |
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto N = readln.chomp;
if(N[0]=='9' || N[1]=='9'){
writeln("Yes");
}else{
writeln("No");
}
}
| D |
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array;
bool p(string s) {
for(int i=0; i<s.length/2; i++) {
if(s[i] != s[$-1-i]) return false;
}
return true;
}
bool q(string s) {
return p(s) && p(s[0..$/2]);
}
void main() {
string s = readln[0..$-1];
if(q(s)) writeln("Yes");
else writeln("No");
}
| D |
import std.stdio, std.string, std.array, std.conv, std.algorithm;
long P = 10^^9+7;
void main()
{
auto n = readln.chomp.to!long;
long ret = 1;
foreach (i; 0..n) {
ret *= i + 1;
ret %= P;
}
writeln(ret);
} | D |
import std.stdio,std.ascii,std.conv,std.string,std.algorithm,std.range,std.functional,std.math,core.bitop;
void main()
{
auto a=readln.chomp.to!int;
auto b=readln.chomp.to!int;
auto h=readln.chomp.to!int;
writeln((a+b)*(h/2));
} | D |
void main(){
import std.stdio, std.conv, std.string, std.algorithm;
long n, a, b, k; rd(n, a, b, k);
const long mod=998244353;
const int M=1_000_00*4;
auto fact=new long[](M);
fact[0]=fact[1]=1;
foreach(i; 2..M) fact[i]=i*fact[i-1]%mod;
auto inv_fact=new long[](M);
long powmod(long a, long x){
if(x==0) return 1;
else if(x==1) return a;
else if(x&1) return a*powmod(a, x-1)%mod;
else return powmod(a*a%mod, x/2);
}
foreach(i; 0..M) inv_fact[i]=powmod(fact[i], mod-2);
long comb(long nn, long rr){
long ret=fact[nn]%mod;
(ret*=inv_fact[rr])%=mod;
(ret*=inv_fact[nn-rr])%=mod;
return ret;
}
long tot=0;
for(long x=0; x<=n; x++)if(k-a*x>=0){
if((k-a*x)%b==0){
long y=(k-a*x)/b;
if(y>n) continue;
(tot+=comb(n, x)*comb(n, y)%mod)%=mod;
}
}
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 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() {
int n = readint();
int ans = 1;
while (ans * 2 <= n) {
ans *= 2;
}
writeln(ans);
}
| D |
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.3"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
string s;
sc.read(s);
writeln(s[0..$-8]);
return 0;
}
/* 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) {
import std.algorithm : max;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private size_t 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;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(4, 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 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 d0 = divisor(N - 1);
auto d1 = divisor(N);
// writeln(d0);
// writeln(d1);s
bool[long] dict;
foreach (d; d0)
dict[d] = true;
long ans = d0.length - 1;
foreach (d; d1)
{
if (d == 1 || (d in dict) || N < d)
continue;
long n = N;
while (0 < n && n % d == 0)
n /= d;
if (n % d == 1)
{
// writeln(d);
ans++;
}
}
writeln(ans);
}
long[] divisor(long N)
{
long[] ret;
foreach (i; 1 .. N + 1)
{
if (N < i * i)
break;
if (N % i == 0)
{
ret ~= i;
if (i * i != N)
ret ~= N / i;
}
}
return ret;
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, m;
scan(n, m);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. m) {
int ai, bi;
scan(ai, bi);
adj[ai - 1] ~= bi - 1;
adj[bi - 1] ~= ai - 1;
}
bool nibu = true;
int[2] cnt;
auto c = new int[](n);
c[] = -1;
void dfs(int v, int col) {
c[v] = col;
cnt[col]++;
foreach (u ; adj[v]) {
if (c[u] == -1) {
dfs(u, col ^ 1);
continue;
}
if (c[u] != (col ^ 1)) {
nibu = false;
return;
}
}
}
dfs(0, 0);
long ans = nibu ? 1L * cnt[0] * cnt[1] - m : 1L * n * (n - 1) / 2 - m;
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,
std.string,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int[] nm = readln.chomp.split.to!(int[]);
int n = nm[0],
m = nm[1];
writeln((n - 1) * (m - 1));
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
int[] AS;
foreach (_; 0..N) AS ~= readln.chomp.to!int;
int[] cs;
foreach (a; AS) {
if (cs.empty || cs[$-1] >= a) {
cs ~= a;
continue;
} else if (cs[0] < a) {
cs[0] = a;
continue;
}
size_t l, r = cs.length-1;
while (l+1 < r) {
auto m = (l+r)/2;
if (cs[m] < a) {
r = m;
} else {
l = m;
}
}
cs[r] = a;
}
writeln(cs.length);
} | D |
import std.stdio, std.string, std.array, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] point = new int[2];
foreach (i; 0 .. n) {
string[] tmp = readln.chomp.split;
if (tmp[0] == tmp[1]) {
point[] += 1;
} else if (tmp[0] < tmp[1]) {
point[1] += 3;
} else {
point[0] += 3;
}
}
writeln(point[0], " ", point[1]);
}
| D |
import core.stdc.stdio;
import std.stdio;
import std.algorithm;
static immutable int unSet=-114514;
struct Data{
int a,l,r,m;
}
Data Comb(const ref Data a,const ref Data b){
Data r;
r.a=a.a+b.a;
r.l=max(a.l,a.a+b.l);
r.r=max(a.r+b.a,b.r);
r.m=max(a.m,b.m,a.r+b.l);
return r;
}
struct Node{
Node* l,r,p;
int delay=unSet,size,v;
Data d;
bool rev;
int State(){
if(p !is null){
if(p.l==&this) return -1;
if(p.r==&this) return 1;
}
return 0;
}
void Propagate(){
if(rev){
if(l !is null) l.rev=!l.rev;
if(r !is null) r.rev=!r.rev;
swap(l,r);
swap(d.l,d.r);
rev=false;
}
if(delay!=unSet){
if(l !is null) l.delay=delay;
if(r !is null) r.delay=delay;
int s=delay*(delay<0?1:size);
d=Data(delay*size,s,s,s);
v=delay;
delay=unSet;
}
}
void Prepare(){
if(State)
p.Prepare;
Propagate;
}
void Update(){
Propagate;
d=Data(v,v,v,v);
size=1;
if(l !is null){
l.Propagate;
d=Comb(l.d,d);
size+=l.size;
}
if(r !is null){
r.Propagate;
d=Comb(d,r.d);
size+=r.size;
}
}
void Rotate(){
Node* par=p,ch;
if(p.l==&this){
ch=r;
r=p;
r.l=ch;
}else{
ch=l;
l=p;
l.r=ch;
}
if(ch) ch.p=p;
p=par.p;
par.p=&this;
if(p !is null){
if(p.l==par) p.l=&this;
if(p.r==par) p.r=&this;
}
par.Update;
Update;
}
void Splay(){
Prepare;
while(State){
int s=State*p.State;
if(!s)
Rotate;
else if(s==1){
p.Rotate;
Rotate;
}else{
Rotate;
Rotate;
}
}
}
void Expose(){
Node* x=&this;
while(x){
x.Splay;
x=x.p;
}
x=&this;
while(x.p){
x.p.r=x;
x=x.p;
x.Update;
}
Splay;
}
void Evert(){
Expose;
r=null;
rev=!rev;
Update;
}
void Link(Node* t){
Evert;
t.Expose;
p=t;
t.r=&this;
t.Update;
}
void Cut(){
Expose;
if(l !is null) l.p=null;
l=null;
Update;
}
void Set(int x){
Expose;
r=null;
delay=x;
Update;
}
int Get(){
Expose;
r=null;
Update;
return d.m;
}
}
void main(){
int n,q;
scanf("%d%d",&n,&q);
Node[] ns=new Node[n];
foreach(ref nd;ns){
int w;
scanf("%d",&w);
nd.v=w;
nd.d=Data(w,w,w,w);
}
foreach(i;0..n-1){
int s,e;
scanf("%d%d",&s,&e);
ns[--s].Link(&ns[--e]);
}
/* void Output(){
foreach(i,nd;ns){
writeln("Node ",i);
writeln("Rev ",nd.rev);
write("Parent ");
if(nd.p is null)
writeln("NULL");
else
writeln(nd.p-ns.ptr);
write("Left ");
if(nd.l is null)
writeln("NULL");
else
writeln(nd.l-ns.ptr);
write("Right ");
if(nd.r is null)
writeln("NULL");
else
writeln(nd.r-ns.ptr);
writeln("Value ",nd.v);
writeln("Data ",nd.d);
writeln;
}
}*/
// writeln("Begin");
// Output;
foreach(_;0..q){
int t,a,b,c;
// writeln("Query ",_);
scanf("%d%d%d%d",&t,&a,&b,&c);
if(t==1){
ns[--a].Evert;
// writeln("Evert ",a);
// Output;
ns[--b].Set(c);
// writeln("Set ",b);
// Output;
}else if(t==2){
ns[--a].Evert;
// writeln("Evert ",a);
// Output;
printf("%d\n",ns[--b].Get);
// writeln("Get ",b);
// Output;
}else
assert(false);
}
} | 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 Cake = Tuple!(long, "beauty", long, "tasty", long, "popular");
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, d;
scan(n, d);
long monitor = 2 * d + 1;
((n + monitor - 1)/ monitor).writeln();
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string;
int[int][10^^5 + 1] MEMO;
void main()
{
auto s = readln.chomp;
int v, p;
foreach (gp; s) {
switch (gp) {
case 'g':
if (p == 0) {
++p;
} else {
--p;
++v;
}
break;
case 'p':
if (p == 0) {
--v;
++p;
} else {
--p;
}
break;
default:
}
}
writeln(v);
} | D |
void main() {
problem();
}
void problem() {
auto K = scan!int;
long solve() {
int unitMod = 7 % K;
if (unitMod == 0) return 1;
int mod = unitMod;
foreach(i; 0..K) {
mod = (mod * 10) + unitMod;
mod %= K;
if (mod == 0) return 2 + i;
}
return -1;
}
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.algorithm, std.conv, std.array, std.string;
void main()
{
auto abck = readln.split.to!(long[]);
auto A = abck[0];
auto B = abck[1];
auto K = abck[3];
writeln(K&1 ? B - A : A - B);
} | 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
2
3 5 2
4 5
---+/
/+---test
3
5 6 3 8
5 100 8
---+/
/+---test
2
100 1 1
1 100
---+/
void main(string[] args) {
readln;
auto A = readln.split.map!(to!long).array;
auto B = readln.split.map!(to!long).array;
size_t ans;
foreach (i, b; B) {
auto n1 = min(b, A[i]);
b -= n1;
auto n2 = min(b, A[i+1]);
A[i+1] -= n2;
ans += n1 + n2;
}
ans.writeln;
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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!int;
auto b = RD!int;
auto c = RD!int;
auto d = RD!int;
auto e = RD!int;
auto f = RD!int;
long ans1, ans2;
{
auto aa = a;
auto bb = b;
auto cc = c;
auto dd = d;
auto ad = min(aa, dd);
ans1 += ad * e;
aa -= ad;
dd -= ad;
auto bcd = min(bb, cc, dd);
ans1 += bcd * f;
}
{
auto aa = a;
auto bb = b;
auto cc = c;
auto dd = d;
auto bcd = min(bb, cc, dd);
ans2 += bcd * f;
bb -= bcd;
cc -= bcd;
dd -= bcd;
auto ad = min(aa, dd);
ans2 += ad * e;
}
writeln(max(ans1, ans2));
stdout.flush;
debug readln;
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto T = readln.chomp.to!int;
while (T--) {
auto X = readln.chomp.to!long;
auto Y = 180L;
auto G = gcd(X, Y);
X /= G;
Y /= G;
if (Y - X == 1) {
Y *= 2;
}
writeln(Y);
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp;
switch (N[$-1]) {
case '3': writeln("bon"); return;
case '0':
case '1':
case '6':
case '8': writeln("pon"); return;
default: writeln("hon"); return;
}
} | D |
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
int n = readln.chomp.to!int;
int[] ts, as;
foreach(i; 0 .. n){
int[] tmp = readln.chomp.split.map!(to!int).array;
ts ~= tmp[0], as ~= tmp[1];
}
long t, a;
t = 1, a = 1;
long ad, td;
long d;
foreach(i; 0 .. n){
if(t % ts[i] == 0) td = t / ts[i];
else td = t / ts[i] + 1;
if(a % as[i] == 0) ad = a / as[i];
else ad = a / as[i] + 1;
if(td < ad) d = ad; else d = td;
t = ts[i] * d, a = as[i] * d;
debug writeln(ts[i], ",", as[i], "; ", t, ",", a);
}
(t + a).writeln;
} | D |
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
void main()
{
int n, k;
readln.chomp.split.tie(n, k);
if (k == 0) {
writeln = cast(long) n * n;
return;
}
long cnt = 0;
for (int b = 1; b <= n; ++b) {
int p = n / b;
int r = n % b;
cnt += p * max(0, b - k);
cnt += max(0, r - k + 1);
}
cnt.writeln;
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
| D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
writeln(tmp[0] < tmp[2] && tmp[2] < tmp[1] || tmp[0] > tmp[2] && tmp[2] > tmp[1] ? "Yes" : "No");
}
| D |
import std.stdio, std.conv, std.string;
void main() {
int N = readln().chomp().to!(int);
long [256] cnt;
foreach(i; 0..256) cnt[i] = 0;
foreach(i; 0..N) {
string S;
S = readln();
cnt[S[0]]++;
}
long res = 0;
string m = "MARCH";
foreach(i; 0..5) {
foreach(j; (i+1)..5) {
foreach(k; (j+1)..5) {
res += cnt[m[i]] * cnt[m[j]] * cnt[m[k]];
}
}
}
writeln(res);
}
| 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 A, B;
scan(A, B);
writeln(A * B % 2 == 0 ? "Even" : "Odd");
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto T = readln.chomp.to!int;
foreach (_; 0..T) {
auto S = readln.chomp;
int max_l, l;
foreach (c; S) {
if (c == 'L') {
l += 1;
max_l = max(l, max_l);
} else {
l = 0;
}
}
writeln(max_l + 1);
}
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
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;
long ans = long.max;
long i;
while (i^^2 <= N)
{
++i;
if (N % i != 0) continue;
auto x = max(N / i, i);
//stderr.writeln(i, " ", x);
long cnt;
while (x != 0)
{
++cnt;
x /= 10;
}
ans = min(ans, cnt);
}
writeln(ans);
stdout.flush();
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
char[] str = readln.chomp.map!(to!char).array;
char[] s = new char[str.length];
int[] cnt = new int[str.length];
int index = 0;
foreach(c; str) {
if (c=='x') {
cnt[index]++;
} else {
s[index] = c;
index++;
}
}
s.length = index;
cnt.length = index+1;
int N = s.length.to!int;
foreach(i; 0..N/2) {
if (s[i] != s[N-i-1]) {
(-1).writeln;
return;
}
}
long ans = 0;
foreach(i; 0..(N+1)/2) {
ans += abs(cnt[i] - cnt[N-i]);
}
ans.writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
| D |
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int n = readln.chomp.to!int;
char[][] field = new char[][](4, n + 2);
foreach (ref line; field)
{
line[] = '*';
}
int[char] cnt;
foreach (i, c; readln.chomp)
{
field[1][i + 1] = c;
cnt[c]++;
}
foreach (i, c; readln.chomp)
{
field[2][i + 1] = c;
cnt[c]++;
}
bool[char] done;
long sum = 1;
enum MOD = 1_000_000_007L;
bool x;
int ofs;
if (field[1][1] == field[2][1]) {
sum *= 3;
x = true;
ofs = 2;
} else {
sum *= 6;
x = false;
ofs = 3;
}
for (int c = ofs; c <= n;)
{
long t;
bool nx = field[1][c] == field[2][c];
final switch (x << 1 | nx) {
case 0:
sum *= 3;
break;
case 1:
sum *= 1;
break;
case 2:
sum *= 2;
break;
case 3:
sum *= 2;
break;
}
if (nx) {
++c;
} else {
c += 2;
}
x = nx;
sum %= MOD;
}
sum.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;
/*}}}*/
/* 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
TSTTSS
---+/
/+---test
SSTTST
---+/
/+---test
TSSTTTSS
---+/
void main(string[] args) {
const S = readln.chomp;
long rem, dep;
foreach (c; S) {
if (c == 'S') {
++dep;
}
if (c == 'T' && dep > 0) {
--dep;
++rem;
}
}
(S.length - rem*2).writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
auto as = DList!char("AKIHABARA".map!(to!char));
auto bs = DList!char(readln.chomp.map!(to!char));
while(true) {
if (bs.empty) {
as[].all!"a=='A'".pipe!(a => a?"YES":"NO").writeln;
return;
} else if (as.empty) {
"NO".writeln;
return;
}
char a = as.front;
char b = bs.front;
if (a == b) {
as.removeFront;
bs.removeFront;
} else if (a=='A') {
as.removeFront;
} else {
"NO".writeln;
return;
}
}
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
auto s = readln.chomp.to!int;
writeln(s/3600, ":", s%3600/60, ":", s%60);
} | D |
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
int n = readln.chomp.to!int;
int k = readln.chomp.to!int;
int sum = 0;
foreach (x; readln.chomp.split.map!(to!int)) {
sum += min(abs(x - k), x) * 2;
}
sum.writeln;
}
| D |
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
const inf = 10^^9;
void main()
{
int n; readV(n);
int[] p; readA(n-1, p); p[] -= 1;
int[] x; readA(n, x);
auto c = new int[][](n);
foreach (i; 1..n) c[p[i-1]] ~= i;
auto nc = new int[](n);
foreach (i; 0..n) nc[i] = c[i].length.to!int;
auto y = new int[](n);
foreach_reverse (i; 0..n) {
auto dp = new int[][](nc[i]+1, x[i]+1);
foreach (ref dpi; dp) dpi[] = inf;
dp[0][0] = 0;
foreach (j; 0..nc[i]) {
auto xj = x[c[i][j]], yj = y[c[i][j]];
foreach (k; 0..x[i]+1) {
if (k >= xj) dp[j+1][k] = min(dp[j+1][k], dp[j][k-xj]+yj);
if (k >= yj) dp[j+1][k] = min(dp[j+1][k], dp[j][k-yj]+xj);
}
}
y[i] = dp[$-1].reduce!min;
if (y[i] >= inf) {
writeln("IMPOSSIBLE");
return;
}
}
writeln("POSSIBLE");
}
| 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;
int color (int[] c1, int[] c2) {
return (c1[0] - c2[0])^^2 + (c1[1] - c2[1])^^2 + (c1[2] - c2[2])^^2;
}
void main()
{
while (1) {
if (!readln.chomp.to!int) break;
auto x = readln.chomp.split.map!(to!int).array;
auto num = 32;
int turn;
bool ply;
while (num > 0) {
if (ply) {
num -= x[turn++%x.length];
} else {
num -= (num - 1) % 5;
}
if (num < 0) num = 0;
num.writeln;
ply = !ply;
}
}
} | D |
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
string s = readln.chomp;
bool[char] flag;
foreach(c; s) flag[c] = 1;
string ans = "Yes";
if('N' in flag && !('S' in flag)) ans = "No";
if('S' in flag && !('N' in flag)) ans = "No";
if('E' in flag && !('W' in flag)) ans = "No";
if('W' in flag && !('E' in flag)) ans = "No";
ans.writeln;
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
void main() {
auto S = readln;
int[char] count;
foreach (c; S) {
if (c != '\n')
count[c]++;
}
int[] data;
int oddCount = 0;
foreach(int co; count) {
if (co % 2 == 1) oddCount++;
else data ~= co / 2;
}
if (oddCount > 1) {
writeln("0");
return;
}
ulong evenCount = S.length - oddCount;
if (evenCount == 0) {
writeln("1");
return;
}
//evenCount / 2???????????????????????????????????????)
auto a = evenCount / 2;
//data??????????????????????????§?????§?¨???????????????????
auto combi(ulong a, ulong b) {
auto u = 1, d = 1;
foreach (i; 0..b) {
u *= a;
a--;
}
foreach (j; 0..b) {
d *= b - j;
}
return u / d;
}
ulong result = 1;
foreach (int co; data) {
result *= combi(a, co);
a -= co;
}
writeln(result);
} | D |
void main() {
int[] tmp = readln.split.to!(int[]);
int k = tmp[0], s = tmp[1];
int cnt;
foreach (x; 0 .. k+1) {
foreach (y; 0 .. k+1) {
int z = s - x - y;
if (0 <= z && z <= k) ++cnt;
}
}
cnt.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 |
void main(){
string[] val = inln!string();
bool a = (val[0]=="H")?true:false;
bool b = (val[1]=="H")?true:false;
writeln(( (a^b)==false)?'H':'D');
}
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.string;
import std.conv;
import std.algorithm;
void main()
{
auto N = readln.chomp.to!int;
int ans = 0;
foreach(_; 0..N){
auto l = readln.chomp.split.map!(to!int);
ans += l[1] - l[0] + 1;
}
writeln(ans);
} | D |
import std;
int calc(string s) {
int[2019] a;
a[0] = 1;
int ans = 0;
int p = 1;
int now = 0;
foreach_reverse (c; s) {
now = (now + p * (c - '0')) % 2019;
p = p * 10 % 2019;
ans += a[now];
a[now]++;
}
return ans;
}
void main() {
string s = read!string;
writeln(calc(s));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
| D |
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
bool[string] S;
foreach (i; 0 .. N) {
S[readln.split[0]] = true;
}
writeln(S.keys.length);
}
| D |
import std.stdio,std.algorithm;void main(){readln;writeln(readln.canFind("Y")?"Four":"Three");} | 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 w, a, b; readV(w, a, b);
if (a > b) swap(a, b);
writeln(max(0, b-(a+w)));
}
| D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
void main()
{
auto n = readln.chomp.to!int;
long sum;
foreach (i; 0..n) {
sum += readln.chomp.to!long;
}
writeln(sum / n);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto x=readln.chomp.to!int;
if(x<1200)writeln("ABC");
else writeln("ARC");
} | D |
// Vicfred
// https://atcoder.jp/contests/abc043/tasks/arc059_a
// brute force
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.map!(to!int).array;
long minima = long.max;
for(int i = -100; i <= 100; i++) {
long cost = 0;
foreach(item; a)
cost += (item - i)^^2;
minima = min(minima, cost);
}
minima.writeln;
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
int res1 = 0;
int res2 = 0;
foreach (i, c; s)
{
res1 += (c != ('0' ^ (i & 1)));
res2 += (c != ('1' ^ (i & 1)));
}
writeln (min (res1, res2));
}
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto K = RD;
auto Q = RD;
auto cnt = new long[](N);
foreach (i; 0..Q)
{
auto A = RD-1;
++cnt[A];
}
foreach (i; 0..N)
{
if (K - Q + cnt[i] > 0)
writeln("Yes");
else
writeln("No");
}
stdout.flush();
debug readln();
} | D |
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int[long] mp;
int n;
sc.read(n);
long[] a;
sc.read(a);
long sm = 0;
mp[sm] = 1;
long ans = 0;
foreach (i; 0..n) {
sm += a[i];
if (sm in mp) ans += mp[sm];
if (sm in mp) {
mp[sm] = mp[sm] + 1;
} else {
mp[sm] = 1;
}
}
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/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/Program/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/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
writeln(180 * (n - 2));
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N, M;
scan(N, M);
auto cost = new int[][](N, N);
fillAll(cost, inf);
foreach (i ; 0 .. M) {
int u, v, d;
scan(u, v, d);
cost[u][v] = d;
}
auto dp = new int[][](1 << N, N);
fillAll(dp, inf);
foreach (i ; 1 .. N) {
dp[1 << i][i] = cost[0][i];
}
foreach (mask ; 0 .. 1 << N) {
foreach (u ; 0 .. N) if (mask & 1 << u) {
auto sub = mask ^ 1 << u;
foreach (v ; 0 .. N) if (sub & 1 << v) {
chmin(dp[mask][u], dp[sub][v] + cost[v][u]);
}
}
}
auto ans = dp[(1 << N) - 1][0];
if (ans == inf) ans = -1;
writeln(ans);
}
| D |
import std.stdio, std.string, std.conv, std.array, std.algorithm;
void main() {
auto a = readln.split.map!(to!int).array;
writeln(a[0]*a[1], " ",(a[0]+a[1])*2);
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto ans = (A == B && A != C) || (A == C && A != B) || (B == C && A != B);
writeln(ans ? "Yes" : "No");
stdout.flush;
debug readln;
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto S = RD!string;
writeln(S[0..3]);
stdout.flush;
debug readln;
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
writeln(a <= b ? a : a - 1);
}
| D |
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] fib = new int[n+1];
fib[0] = fib[1] = 1;
foreach (i; 2 .. n+1) {
fib[i] = fib[i-1] + fib[i-2];
}
fib[n].writeln;
}
| D |
import std.stdio;
import std.string;
import std.format;
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.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
string str = readln.chomp;
string a = "keyence";
foreach(i; 0..8) {
if (str[0..i] == a[0..i] && str[$-7+i..$] == a[$-7+i..$]) {
"YES".writeln;
return;
}
}
"NO".writeln;
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// 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) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
| D |
import std.stdio;
import std.conv;
import std.array;
void main()
{
long N = readln.split[0].to!long;
long[] T = new long[5];
foreach (i; 0..5){
T[i] = readln.split[0].to!long;
}
long minT = T[0];
foreach (t; T){
if (t < minT) minT = t;
}
long ans = N / minT;
if (N % minT) ans++;
ans += 4;
writeln(ans);
}
| 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!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
long check = true;
long c_t, c_x, c_y;
foreach (_; iota(n))
{
long t, x, y;
scan(t, x, y);
auto manh = abs(c_x - x) + abs(c_y - y);
long t_dif = t - c_t;
check = check && (t_dif >= manh) && ((t_dif - manh) % 2 == 0);
c_t = t;
c_x = x;
c_y = y;
}
if (check)
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.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() {
ulong N = readln.chomp.to!ulong;
ulong[] A = readln.chomp.split.to!(ulong[]);
ulong ans;
foreach (a; A) {
while (a % 2 == 0) {
a /= 2;
ans++;
}
}
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; }
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!string;
auto B = RD!string;
auto C = RD!string;
writeln(A[0], B[0], C[0]);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = int.max;
void main() {
int n, q;
scan(n, q);
auto st = SegmentTree!(int, min, inf)(n);
foreach (_ ; 0 .. q) {
int com, x, y;
scan(com, x, y);
if (com == 0) {
st.assign(x, y);
}
else {
st.query(x, y + 1).writeln;
}
}
}
struct SegmentTree(T, alias op, T initValue = 0) {
import std.functional : binaryFun;
alias f = binaryFun!(op);
T[] _data;
int _treeSize, _lineSize = 1;
// 配列の要素数 n だけ与えて初期化
this(int n) {
while (_lineSize < n) {
_lineSize *= 2;
}
_treeSize = 2*_lineSize - 1;
_data = new T[](_treeSize);
_data[] = initValue;
}
// 配列 a を与えて初期化
this(T[] a) {
auto n = a.length;
while (_lineSize < n) {
_lineSize *= 2;
}
_treeSize = 2*_lineSize - 1;
_data = new T[](_treeSize);
_data[] = initValue;
_data[_lineSize - 1 .. _lineSize - 1 + n] = a[];
foreach_reverse (i ; 0 .. _lineSize - 1) {
_data[i] = f(_data[2*i + 1], _data[2*i + 2]);
}
}
// a[i] = x
void assign(int i, int x) {
i += _lineSize - 1;
_data[i] = x;
while (i > 0) {
i = (i - 1) / 2;
_data[i] = f(_data[2*i + 1], _data[2*i + 2]);
}
}
// a[i] = f(a[i], x)
void act(int i, int x) {
i += _lineSize - 1;
_data[i] = f(_data[i], x);
while (i > 0) {
i = (i - 1) / 2;
_data[i] = f(_data[2*i + 1], _data[2*i + 2]);
}
}
alias add = act;
// 要求区間 [a, b) に対して、対象区間 [l, r) の値を返す
T query(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r == -1) {
r = _lineSize;
}
if (a >= b) { // 要求区間が空である場合
return initValue;
}
if (b <= l || r <= a) { // 要求区間が対象区間外の場合
return initValue;
}
if (a <= l && r <= b) { // 要求区間が対象区間に含まれている場合
return _data[k];
}
// どちらでもない場合は対象区間を2つに分けて再帰する
auto leftValue = query(a, b, 2*k + 1, l, l + (r - l) / 2);
auto rightValue = query(a, b, 2*k + 2, l + (r - l) / 2, r);
return f(leftValue, rightValue);
}
}
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.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
(6 - readln.chomp.to!int - readln.chomp.to!int).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, std.bitmanip, std.regex;
void main() {
int[] dr = [1, -1, 0, 0];
int[] dc = [0, 0, 1, -1];
auto s = readln.split.map!(to!int);
auto H = s[0];
auto W = s[1];
auto A = H.iota.map!(_ => readln.chomp).array;
foreach (i; 0..H) {
foreach (j; 0..W) {
if (A[i][j] != '#') continue;
bool ok = false;
foreach (k; 0..4) {
int di = i + dr[k];
int dj = j + dc[k];
if (di < 0 || di >= H || dj < 0 || dj >= W) continue;
if (A[di][dj] == '#') ok = true;
}
if (!ok) {
writeln("No");
return;
}
}
}
writeln("Yes");
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main(){
string[] sqlen = readln().chomp().split();
int a = sqlen[0].to!int(); int b = sqlen[1].to!int();
writeln(a*b, " ", (a+b)*2);
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
writeln(N == 1 && M == 1 ? 1 : N == 1 ? max(0, M-2) : M == 1 ? max(0, N-2) : (N-2) * (M-2));
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
auto a = readln.chomp.split.map!(to!int);
int w = a[0], h = a[1], x = a[2], y = a[3], r = a[4];
if (x - r >= 0 && x + r <= w && y - r >= 0 && y + r <= h) {
writeln("Yes");
} else {
writeln("No");
}
} | D |
import std.stdio;
import core.stdc.stdio;
import std.algorithm;
struct state{
int x;
int y;
int z;
int r;
}
void main(){
while(1){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==0&&b==0&&c==0)
break;
int m = a+b+c;
int n;
scanf("%d",&n);
int[] dec = new int[m];
dec[] = 2;
state[] sts = new state[n];
for(int i=0;i<n;i++){
state s;
scanf("%d%d%d%d",&s.x,&s.y,&s.z,&s.r);
s.x--;
s.y--;
s.z--;
if(s.r==1){
dec[s.x] = 1;
dec[s.y] = 1;
dec[s.z] = 1;
}
sts[i] = s;
}
for(int _=0;_<m;_++){
foreach(st;sts){
if(st.r==0){
if(dec[st.x]==1&&dec[st.y]==1)
dec[st.z]=0;
else if(dec[st.y]==1&&dec[st.z]==1)
dec[st.x]=0;
else if(dec[st.z]==1&&dec[st.x]==1)
dec[st.y]=0;
}
}
}
foreach(d;dec)
printf("%d\n",d);
}
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[30] CNT;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
foreach (_; 0..N) {
auto as = readln.split.to!(int[]);
foreach (n; as[1..$]) {
++CNT[n-1];
}
}
int cnt;
foreach (c; CNT) if (c == N) ++cnt;
writeln(cnt);
} | 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 = sread();
// writeln(n);
if ((n[0] == n[1]) && (n[0] == n[2]))
{
writeln("Yes");
return;
}
if ((n[1] == n[2]) && (n[1] == n[3]))
{
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;
}
}
| D |
import std;
void main() {
int a, b; scan(a, b);
writeln(a * b);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
| D |
import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main()
{
immutable n = readln.chomp.to!size_t;
ubyte[][] b;
foreach (i; 0..n)
b ~= cast(ubyte[])readln.chomp;
size_t ret;
foreach (i; 0..n)
{
ret += b.sym * n;
b = b[1..$] ~ b[0];
}
ret.writeln;
}
bool sym(T)(T[][] b)
{
foreach (i; 0..b.length)
foreach (j; 0..i)
if (b[i][j]!=b[j][i]) return false;
return true;
} | D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
if (N == 0) {
"a".writeln;
return;
}
string ans = "";
char c = 'a';
while (N > 0) {
ans ~= c;
int tmp = 1;
while (N - tmp >= 0) {
N -= tmp;
ans ~= c;
tmp += 1;
}
c += 1;
}
ans.writeln;
}
| D |
void main()
{
string s = rdStr;
writeln(s[0..4], " ", s[4..$]);
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop; | D |
void main() {
auto s = rs;
ulong tmp;
foreach(i, v; s) {
if(v == 'A') {
tmp = i;
break;
}
}
foreach_reverse(i, v; s) {
if(v == 'Z') {
(i - tmp + 1).writeln;
return;
}
}
}
// ===================================
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, std.algorithm;
int lcs(string x, string y) {
int m = x.length.to!int, n = y.length.to!int;
int[][] c = new int[][](m+1, n+1);
foreach (i; 1 .. m+1) {
foreach (j; 1 .. n+1) {
if (x[i-1] == y[j-1]) {
c[i][j] = c[i-1][j-1] + 1;
} else {
c[i][j] = max(c[i-1][j], c[i][j-1]);
}
}
}
return c[m][n];
}
void main() {
int q = readln.chomp.to!int;
foreach (i; 0 .. q) {
string x = readln.chomp, y = readln.chomp;
lcs(x, y).writeln;
}
}
| D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto s = readln.chomp.to!(char[]);
auto t = readln.chomp.to!(char[]);
const no = "UNRESTORABLE";
if (t.length > s.length) {
writeln(no);
return;
}
char[][] cand;
for (size_t i = 0; i + t.length <= s.length; i++) {
bool ok = true;
foreach (j; 0 .. t.length) {
if (s[i + j] != '?') {
ok &= s[i + j] == t[j];
}
}
if (ok) {
auto u = s[0 .. i] ~ t ~ s[i + t.length .. $];
foreach (ref c; u)
if (c == '?')
c = 'a';
cand ~= u;
}
}
if (cand.length > 0) {
sort(cand);
writeln(cand[0]);
} else {
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 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;
}
struct S {
int node;
long cost;
}
long[] bfs(int k, int n, S[][int] adj) {
auto cost = new long[n + 1];
auto used = new bool[n + 1];
S[] q = [S(k, 0)];
while (q.length > 0) {
auto d = q[0];
q = q[1 .. $];
if (used[d.node])
continue;
used[d.node] = true;
cost[d.node] = d.cost;
if (d.node !in adj)
continue;
foreach (s; adj[d.node]) {
q ~= S(s.node, d.cost + s.cost);
}
}
return cost;
}
void main() {
int n = readint();
S[][int] adj;
for (int i = 0; i < n - 1; i++) {
auto abc = readints();
int a = abc[0], b = abc[1], c = abc[2];
adj[a] ~= S(b, c);
adj[b] ~= S(a, c);
}
auto qk = readints();
int q = qk[0], k = qk[1];
auto cost = bfs(k, n, adj);
for (int i = 0; i < q; i++) {
auto xy = readints();
int x = xy[0], y = xy[1];
auto ans = cost[x] + cost[y];
writeln(ans);
}
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9+7;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0];
auto M = s[1];
auto X = readln.split.map!(to!long).array;
auto Y = readln.split.map!(to!long).array;
auto DX = new long[](N-1);
auto DY = new long[](M-1);
foreach (i; 0..N-1) DX[i] = (X[i+1] - X[i]) % MOD;
foreach (i; 0..M-1) DY[i] = (Y[i+1] - Y[i]) % MOD;
long width = 0;
long left = 1;
long right = N - 1;
foreach (i; 0..N-1) {
width += left * right % MOD * DX[i] % MOD;
width %= MOD;
left += 1;
right -= 1;
}
long ans = 0;
long up = 1;
long down = M - 1;
foreach (i; 0..M-1) {
ans += up * down % MOD * width % MOD * DY[i] % MOD;
ans %= MOD;
up += 1;
down -= 1;
}
ans.writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main()
{
ulong N = readln.split[0].to!ulong;
char[] S = readln.chomp.to!(char[]);
ulong K = readln.split[0].to!ulong;
foreach (i; 0..N) if (S[i] != S[K - 1]) S[i] = '*';
writeln(S.to!string);
}
| D |
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.range;
void main() {
long n = readln.chomp.to!long;
long ans = 1;
for (long i = 1; i <= 20; i++) {
ans *= i;
}
ans.writeln;
} | D |
import std.stdio, std.conv, std.string;
void main(){
int n=to!int(chomp(readln()));
string s, t;
s=chomp(readln());
t=chomp(readln());
if(n==1){
writeln(3);
return;
}
const mod=1_000_000_000+7;
bool y=s[0]==t[0];
long c=y? 3: 6;
int i=y? 1: 2;
while(i<n){
if(i+1<n){
if(s[i]==t[i]){
if(y){
(c*=2)%=mod;
}else{
// c*=1;
}
y=true;
i++;
}else{
if(y){
(c*=2)%=mod;
}else{
(c*=3)%=mod;
}
y=false;
i+=2;
}
}else{
if(y){
(c*=2)%=mod;
}else{
// c*=1;
}
break;
}
}
writeln(c);
}
/*
1 : 2 2 3
2 : 1 3 1
*/ | D |
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void main(){
auto x = readln.chomp.to!int;
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
((x - a) % b).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 n=scanElem;
auto k=scanElem;
auto x=scanElem;
auto y=scanElem;
writeln(min(n,k)*x+max(0,n-k)*y);
}
| D |
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
void main() {
int N = readln.chomp.to!int;
auto c = readln.chomp.split.map!(to!int).array;
int Q = readln.chomp.to!int;
bool flag = true;
int[] apple = new int[](N);
for (int i = 0; i < Q; i++) {
auto operation = readln.chomp.split.map!(to!int).array;
int x = operation[1] - 1, d = operation[2];
if (!flag) {
continue;
}
if (operation[0] == 1) {
if (apple[x] + d <= c[x]) {
apple[x] += d;
}
else {
writeln(x + 1);
flag = false;
}
}
else {
if (apple[x] - d >= 0) {
apple[x] -= d;
}
else {
writeln(x + 1);
flag = false;
}
}
}
if (flag) {
0.writeln;
}
return;
}
| D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
const A = tmp[0], B = tmp[1], K = tmp[2];
int cnt;
foreach_reverse (x; 1..min(A,B)+1) {
if (A % x == 0 && B % x == 0) {
if (++cnt == K) {
writeln(x);
}
}
}
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto s = readln.strip;
writeln (s.maxElement);
}
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
long A = scanElem;
long B = scanElem;
writeln((A-1)*(B-1));
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
} | D |
import std.stdio, std.conv, std.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;
long cnt;
foreach (i; 0..N)
{
if (S[i] == 'I')
++cnt;
else
--cnt;
ans = max(ans, cnt);
}
writeln(ans);
stdout.flush();
debug readln();
} | D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.