code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1];
auto pa = a == 1 ? 14 : a, pb = b == 1 ? 14 : b;
if (pa == pb) writeln("Draw");
else if (pa > pb) writeln("Alice");
else writeln("Bob");
}
|
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 cnt;
long N = lread();
long m = long.min;
foreach (_; 0 .. N)
{
long p = lread();
m.maxAssign(p);
cnt += p;
}
cnt -= m / 2;
writeln(cnt);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm: canFind;
void main() {
string[] inputs = split(readln());
int A = to!int(inputs[0]);
int B = to!int(inputs[1]);
((A + B) % 24).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 1000000007;
void main()
{
auto N = readln.chomp.to!int;
int a, b, c;
long r = 1;
foreach (x; readln.split.to!(int[])) {
if (x == a && a == b && a == c) {
r = (r * 3) % P;
++a;
} else if (x == a && a == b) {
r = (r * 2) % P;
++a;
} else if (x == b && b == c) {
r = (r * 2) % P;
++b;
} else if (x == a) {
++a;
} else if (x == b) {
++b;
} else if (x == c) {
++c;
} else {
writeln(0);
return;
}
}
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.array;
import std.math;
void main(){
double r = readln.split[0].to!double;
r = 2 * PI * r;
printf("%lf\n",r);
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto t = readln.split.to!(long[]);
if (t == [1,1]) {
writeln(1);
return;
}
if (t[0] == 1) {
writeln(t[1]-2);
return;
}
if (t[1] == 1) {
writeln(t[0]-2);
return;
}
writeln((t[0]-2)*(t[1]-2));
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
auto input = getStdin!(string[])[0].split(" ");
int w = input[0].to!(int);
int h = input[1].to!(int);
int x = input[2].to!(int);
int y = input[3].to!(int);
int r = input[4].to!(int);
if (x - r < 0 || x + r > w || y - r < 0 || y + r > h) {
"No".writeln;
}
else {
"Yes".writeln;
}
}
T getStdin(T)() {
string[] cmd;
string line;
while ((line = chomp(stdin.readln())) != "") cmd ~= line;
return to!(T)(cmd);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
int n, m, x;
scan(n, m, x);
auto a = readln.split.to!(int[]);
int l, r;
foreach (ai ; a) {
if (ai < x) l++;
else r++;
}
writeln(min(l, r));
}
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.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;
}
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.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
int x = read.to!int;
int[] ls;
foreach(i; 0 .. n) ls ~= read.to!int;
int sum;
int ans;
foreach(l; ls){
if(sum <= x) ans += 1;
sum += l;
}
if(sum <= x) ans += 1;
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
auto nhw = aryread();
auto n = nhw[0];
auto h = nhw[1];
auto w = nhw[2];
long cnt;
foreach(i;0..n)
{
auto ab = aryread();
if (h <= ab[0] && w <= ab[1])
{
cnt++;
}
}
cnt.writeln;
}
|
D
|
import std.stdio, std.string, std.algorithm, std.conv, std.array;
void main(){
auto nx=readln.split.map!(to!long).array;
auto n=nx[0], x=nx[1];
auto as=readln.split.map!(to!long).array;
auto c=0L;
long result=0;
foreach(a; as){
if(a+c>x){
result+=a+c-x;
c=-c+x;
}else{
c=a;
}
}
writeln(result);
}
|
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!int);
auto N = s[0];
auto M = s[1];
auto W = new int[](N);
auto V = new long[](N);
foreach (i; 0..N) {
s = readln.split.map!(to!int);
W[i] = s[0];
V[i] = s[1];
}
auto dp = new long[][](N+1, M+1);
foreach (i; 0..N) {
dp[i+1] = dp[i].dup;
foreach (j; 0..M+1) {
if (j + W[i] > M) break;
dp[i+1][j+W[i]] = max(dp[i+1][j+W[i]], dp[i][j] + V[i]);
}
}
dp[N].reduce!max.writeln;
}
|
D
|
import std.stdio;
import std.datetime;
import std.conv;
void main()
{
auto s = TimeOfDay.fromISOExtString(readln[0 .. $-1] ~ ":00");
auto t = readln;
auto d = dur!"hours"((t[0..2]).to!long)
+ dur!"minutes"(t[3..5].to!long);
writeln((s - d).toISOExtString[0..5]);
}
|
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;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
long N = lread();
auto A1 = aryread();
auto A2 = aryread();
foreach (i; 1 .. N)
{
A1[i] = A1[i] + A1[i - 1];
A2[i] = A2[i] + A2[i - 1];
}
long result = 0;
// writeln(A1, A2);
foreach (i; 0 .. N)
{
long tmp = A1[i] + A2[$ - 1] - ((i == 0) ? 0 : A2[i - 1]);
result = result.max(tmp);
}
result.writeln();
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container;
bool check(bool[3][3] t) {
for(int i=0; i<3; i++) {
if(t[i][0]&&t[i][1]&&t[i][2]) {
return true;
}
if(t[0][i]&&t[1][i]&&t[2][i]) {
return true;
}
}
return t[0][0]&&t[1][1]&&t[2][2] || t[0][2]&&t[1][1]&&t[2][0];
}
void main() {
int[][] a = [];
for(int i=0; i<3; i++) a ~= readln.chomp.split.to!(int[]);
int n = readln.chomp.to!int;
bool[3][3] t;
for(int k=0; k<n; k++) {
int b = readln.chomp.to!int;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(a[i][j] == b) t[i][j] = true;
}
}
}
if(t.check) writeln("Yes");
else writeln("No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto S = RD!string;
writeln(S[2] == S[3] && S[4] == S[5] ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
// unihernandez22
// https://atcoder.jp/contests/abc076/tasks/abc076_c
// string manipulation
import std.stdio;
import std.string;
void main() {
string s = readln.chomp;
string t = readln.chomp;
ulong idx = -1;
bool matched;
for(long i = (s.count-t.count); i >= 0; i--) {
if (s[i] == '?' || s[i] == t[0]) {
matched = true;
foreach(j; 0..t.count) {
if (s[i+j] != '?' && s[i+j] != t[j]) {
matched = false;
break;
}
}
if (matched) {
idx = i;
break;
}
}
}
if (idx == -1) {
writeln("UNRESTORABLE");
return;
}
foreach(i; 0..s.count) {
if (s[i] == '?' && (i < idx || i >= t.count+idx))
write("a");
else if (s[i] == '?')
write(t[i-idx]);
else
write(s[i]);
} writeln;
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
char[] s;
cin.scan(s);
long sum;
foreach (i; 0 .. (1 << s.length - 1))
{
long res = s[0] - '0';
foreach (j; 0 .. s.length - 1)
{
if ((i >> j) & 1) {
sum += res;
res = s[j + 1] - '0';
}
else
{
res *= 10;
res += s[j + 1] - '0';
}
}
sum += res;
}
writeln(sum);
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.array;
void main() {
int sum = 0;
while(true) {
string line = readln.chomp;
bool prev_digit = false;
string number = "";
if (stdin.eof) break;
foreach(x; line) {
if (isDigit(x)) {
number ~= x;
prev_digit = true;
}
else if (prev_digit) {
sum += number.to!(int);
number = "";
prev_digit = false;
}
}
if (number != "") sum += number.to!(int);
}
writeln(sum);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto a = readln.split.to!(int[]);
writeln(a.count(5) == 2 && a.count(7) == 1 ? "YES" : "NO");
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.chomp.split.to!(int[]);
a = a.uniq.array;
foreach (i, x; a) {
x.write;
if (i != a.length - 1) " ".write;
else writeln;
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.chomp;
auto B = readln.chomp;
bool[] tate;
for (int i = 0; i < N; i++) {
if (i == N - 1 || A[i] != A[i + 1]) tate ~= true;
else tate ~= false, i += 1;
}
long ans = 1;
long MOD = 10^^9 + 7;
long a, b;
for (int i = 0; i < tate.length; i++) {
if (i == 0 && tate[i]) {
a = 3;
b = 1;
} else if (i == 0 && !tate[i]) {
a = 3;
b = 2;
} else if (tate[i] && tate[i-1]) {
a = 2;
b = 1;
} else if (tate[i] && !tate[i-1]) {
a = 1;
b = 1;
} else if (!tate[i] && tate[i-1]) {
a = 2;
b = 1;
} else {
a = 3;
b = 1;
}
ans = ans * a * b % MOD;
}
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.range, std.array, std.conv, std.string, std.math, std.container, std.typecons;
void main() {
writeln((readln.chomp.to!int - readln.chomp.to!int) % readln.chomp.to!int);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto N=readln.chomp.to!int;
auto K=readln.chomp.to!int;
auto X=readln.chomp.to!int;
auto Y=readln.chomp.to!int;
if(N<=K)writeln(X*N);
else writeln(X*K+Y*(N-K));
}
|
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;
auto ss = readln.split.to!(long[]);
long r;
foreach (x; 1..N-1) {
long rr;
int i, j = N-1;
while (i < N-1 && j > 0) {
if (i == j || i == j+x || i > N-1-x || j < x) break;
rr += ss[i] + ss[j];
r = max(r, rr);
i += x;
j -= x;
}
}
writeln(r);
}
|
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,
std.format,
std.math;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.to!int;
writeln(n^^2-a);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(dchar[]);
auto T = readln.chomp.to!(dchar[]);
foreach (i; 0..S.length) {
S = S[1..$] ~ S[0];
if (S == T) {
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.stdio, std.string;
void main() {
string n = readln.chomp;
string s = "0";
int cnt;
while (s != n) {
s = succ(s);
if (s.length % 2) ++cnt;
}
cnt.writeln;
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long[] a = readln.split.to!(long[]);
long x, y;
long r;
long result;
foreach (l; 0 .. n)
{
while (r < n && (x ^ a[r]) == (y + a[r]))
{
x ^= a[r];
y += a[r];
++r;
}
result += r - l;
if (l == r)
{
++r;
}
else
{
x ^= a[l];
y -= a[l];
}
}
result.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
long x;
scan(n, x);
auto a = new long[](n + 1); // 長さ
auto b = new long[](n + 1); // Pの枚数
a[0] = b[0] = 1;
foreach (i ; 1 .. n + 1) {
a[i] = 2L*a[i-1] + 3;
b[i] = 2L*b[i-1] + 1;
}
long query(int L, long x) {
if (L == 0) {
return x == 1;
}
if (x <= 1) {
return 0;
}
else if (x < (a[L] + 1) / 2) {
return query(L-1, x - 1);
}
else if (x == (a[L] + 1) / 2) {
return b[L-1] + 1;
}
else if (x < a[L] - 1) {
return b[L-1] + 1 + query(L-1, x - (a[L] + 1) / 2);
}
else {
return 2*b[L-1] + 1;
}
}
long ans = query(n, x);
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
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!int();
deq.insertBack(1); deq.removeBack();
auto p = deq.p;
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (p.length && (*p)[0] == i-m) {
p.removeFront();
}
if (p.length) {
ndp[i] = dp[(*p)[0]] + 1L * ph * a[i];
}
while (p.length && dp[(*p)[p.length-1]] <= dp[i]) {
p.removeBack();
}
p.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
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/container/deque.d */
// module dcomp.container.deque;
struct Deque(T) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (!p) p = new Payload(); }
private void C() const {
version(assert) if (!p) throw new RangeError();
}
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
@property bool empty() const { return (!p || p.empty); }
@property size_t length() const { return (p ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dcomp" version=">=0.8.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.algorithm;
int main() {
Scanner sc = new Scanner(stdin);
int n;
sc.read(n);
int[] a;
sc.read(a);
auto di = a.enumerate.minimum!"a[1]<b[1]"[0];
auto ui = a.enumerate.maximum!"a[1]<b[1]"[0];
int d = a[di], u = a[ui];
if (-d < u) {
writeln(2*n - 2);
foreach (i; 0..n) {
if (i == ui) continue;
writeln(ui+1, " ", i+1);
}
foreach (i; 1..n) {
writeln(i, " ", i+1);
}
} else {
writeln(2*n - 2);
foreach (i; 0..n) {
if (i == di) continue;
writeln(di+1, " ", i+1);
}
foreach_reverse (i; 1..n) {
writeln(i+1, " ", i);
}
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */
// module dcomp.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/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);
}
}
}
}
T[N] fixed(T, size_t N)(T[N] a) {return a;}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.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 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/algorithm.d */
// module dcomp.algorithm;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = l + (r-l) / 2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
import std.range.primitives;
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "minimum: range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "maximum: range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() const {
return now.empty;
}
@property auto front() const {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
/*
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.stdio;
void main()
{
foreach (former; 1 .. 10)
{
foreach (latter; 1 .. 10)
{
writeln(former,"x",latter,"=",former*latter);
}
}
}
|
D
|
import std.stdio,std.string,std.conv;
int main()
{
string s;
while((s = readln.chomp).length != 0)
{
bool flag = true;
for(int i=0;i<7;i += 3)
{
if(s[i] != 's' && s[i] == s[i+1] && s[i+1] == s[i+2])
{
s[i].writeln;
flag = false;
break;
}
}
if(flag)
{
foreach(i;0..3)
{
if(s[i] != 's' && s[i] == s[i+3] && s[i+3] == s[i+6])
{
s[i].writeln;
flag = false;
break;
}
}
}
if(flag)
{
if(s[0] != 's' && s[0] == s[4] && s[4] == s[8])
{
s[0].writeln;
flag = false;
}
}
if(flag)
{
if(s[2] != 's' && s[2] == s[4] && s[4] == s[6])
{
s[2].writeln;
flag = false;
}
}
if(flag)
{
writeln("d");
}
}
return 0;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
void main(){
while(true) {
auto n = readInt();
if(n == 0) {
return;
}
int[] a;
foreach(i; iota(n)) {
a ~= readInt();
}
int ans;
foreach_reverse(i; iota(n)) {
foreach(j; iota(i)) {
if(a[j] > a[j+1]) {
swap(a[j], a[j+1]);
++ans;
}
}
}
writeln(ans);
}
}
|
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 B = readln.split.to!(long[]);
auto A = new long[](N);
A[0] = B[0];
foreach (i ; 1 .. N - 1) {
A[i] = min(B[i], B[i - 1]);
}
A[N - 1] = B[N - 2];
auto ans = A.sum(0L);
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto x = RD;
auto y = RD;
long ans;
auto xy = sgn(x) * sgn(y);
if (x <= y)
{
if (xy >= 0)
ans = abs(abs(y) - abs(x));
else
ans = abs(abs(y) - abs(x)) + 1;
}
else
{
if (xy == 0)
ans = abs(abs(y) - abs(x)) + 1;
else if (xy > 0)
ans = abs(abs(y) - abs(x)) + 2;
else
ans = abs(abs(y) - abs(x)) + 1;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=new long[](100);
a[0]=2; a[1]=1;
for(int i=2; i<=n; i++){
a[i]=a[i-1]+a[i-2];
}
writeln(a[n]);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto a = iota(n).map!(i => readln.chomp.to!int).array;
// 0: 無, 1: 偶(!= 0), 2: 奇, 3: 偶(!= 0), 4: 無
auto dp = new long[][](n + 1, 5);
foreach (i ; 1 .. n + 1) {
foreach (j ; 0 .. 5) {
dp[i][j] = inf6;
foreach (k ; 0 .. j + 1) {
dp[i][j] = min(dp[i][j], dp[i - 1][k]);
}
if (j == 0 || j == 4) {
dp[i][j] += a[i - 1];
}
else if (j == 1 || j == 3) {
if (a[i - 1] == 0) {
dp[i][j] += 2;
}
else {
dp[i][j] += (a[i - 1] % 2);
}
}
else {
dp[i][j] += (a[i - 1] % 2 == 0);
}
}
}
long ans = inf6;
foreach (i ; 0 .. 5) {
ans = min(ans, dp[n][i]);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.math;
import std.algorithm;
import std.range;
void main() {
long n = readln.chomp.to!long;
long[] answer;
for (long i = 2; i <= sqrt(cast(float)n); i++) {
if (n % i == 0) {
answer ~= i;
}
while (n % i == 0) {
n /= i;
}
}
if (answer.empty) {
writeln(n);
} else {
writeln(answer.reduce!("a * b") * n);
}
}
|
D
|
import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
void main(string[] args)
{
inputFile = stdin;
debug inputFile = File(args[1]);
auto t = next!int;
foreach(tc; 0 .. t)
{
auto n = next!int;
auto q = next!int;
auto a = next!int(n);
assert(q == 0);
long maxval = a[0], minval = a[0];
long globalmax = a[0];
foreach(ai; a[1 ..$])
{
long localmax = ai;
if (minval < 0) localmax -= minval;
long localmin = ai;
if (maxval > 0) localmin -= maxval;
maxval = max(localmax, maxval);
minval = min(localmin, minval);
}
maxval.writeln;
}
}
ulong bitCnt(long x)
{
ulong cnt = 0;
while(x) {cnt += x & 1; x>>=1;}
return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
T[] _baseArray;
T[] _slice;
size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
size_t[dimension] _indexer;
this(T[] baseArray, size_t[dimension] sizes)
{
_baseArray = baseArray;
_sizes = sizes;
_indexer[dimension - 1] = 1;
static foreach_reverse(i; 0 .. dimension - 1)
_indexer[i] = _indexer[i + 1] * _sizes[i + 1];
auto requiredSize = _indexer[0] * _sizes[0];
debug assert(_baseArray.length >= requiredSize);
_slice = _baseArray[0 .. requiredSize];
}
this(size_t[dimension] sizes)
{
size_t reqSize = 1;
static foreach(i; 0 .. dimension)
reqSize *= sizes[i];
this(new T[](reqSize), sizes);
}
ref auto opIndex()
{
pragma(inline, true);
return _slice[];
}
ref auto opIndex(I...)(I i)
{
pragma(inline, true);
debug assert(i.length <= dimension);
size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
static if (i.length == dimension)
{
return _slice[index];
}
else
{
enum sliceDimension = dimension - i.length;
size_t[sliceDimension] remSizes = _sizes[i.length .. $];
auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
return Mat!(T, sliceDimension)(basePortion, remSizes);
}
}
static if (dimension == 2)
{
auto identity()
{
debug assert(dim(0) == dim(1));
auto n = dim(0);
auto id = Mat!(T, 2)([n, n]);
foreach(k; 0 .. n)
id[k, k] = T(1);
return id;
}
bool canMultiply(Mat!(T, 2) b)
{
return this.dim(1) == b.dim(0);
}
auto opBinary(string op)(Mat!(T, 2) b)
if (op == "*")
{
debug assert(canMultiply(b));
auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
foreach(i; 0 .. res.dim(0))
foreach(k; 0 .. this.dim(1))
foreach(j; 0 .. res.dim(1))
res[i, j] = res[i, j] + this[i, k] * b[k, j];
return res;
}
auto opBinary(string op)(ulong exp)
if (op == "^^")
{
return this.pow(identity, exp);
}
mixin interiorBinPow;
}
}
struct Mod(T, ulong mod)
{
alias This = Mod!(T, mod);
T _rep; T rep() {return _rep;}
this(W)(W w)
{
_rep = ((cast(T) w)%mod + mod) % mod;
}
static auto plainConstruct(T t)
{
pragma(inline, true);
debug assert(t >= 0 && t < mod);
auto res = Mod!(T, mod).init;
res._rep = t;
return res;
}
string toString()
{
return to!string(rep);
}
debug invariant
{
assert(_rep >= 0 && _rep < mod);
}
auto opBinary(string op)(This b)
{
static if (op == "+")
{
T resrep = _rep + b._rep;
if (resrep >= mod) resrep -= mod;
return This.plainConstruct(resrep);
}
else static if (op == "-")
{
T resrep = _rep - b._rep;
if (resrep < 0) resrep += mod;
return This.plainConstruct(resrep);
}
else static if (op == "*")
{
return This(_rep * b._rep);
}
}
}
mixin template interiorBinPow()
{
alias ThisType = typeof(this);
auto pow(ThisType res, ulong exp)
{
if (exp < 0) debug assert(0);
auto pow = this;
while(exp)
{
if (exp & 1)
res = res * pow;
pow = pow * pow;
exp >>= 1;
}
return res;
}
}
// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
_wordsFill;
auto word = _words.front;
_words.removeFront;
return word;
}
string peekWord()
{
_wordsFill;
return _words.front;
}
T next(T)()
{
return to!T(popWord);
}
T peek(T)()
{
return to!T(peekWord);
}
auto next(T, S...)(S s)
{
static if (S.length == 0)
{
return to!T(popWord);
}
else
{
auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
foreach(ref elem; res)
elem = next!T(s[1 .. $]);
return res;
}
}
|
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;
}
}
immutable names = "RPS";
void main() {
auto r = new InputReader;
foreach (t; 0 .. r.next!uint) {
immutable n = r.next!uint;
auto cnt = r.nextA!uint(3);
auto a = new char[n];
auto b = new int[n];
foreach (i; 0 .. n) {
char c;
int k = -1;
do {
c = r.nextByte ().to!char;
foreach (o; 0 .. 3) {
if (names[o] == c) {
k = o;
}
}
} while (k < 0);
b[i] = (k + 1) % 3;
}
debug stderr.writeln ("b = ", b);
int wins;
foreach (k; 0 .. 3) {
foreach (j; 0 .. n) {
if (cnt[k] > 0 && b[j] == k) {
--cnt[k];
a[j] = names[k];
++wins;
}
}
}
debug stderr.writeln ("wins = ", wins);
foreach (i; 0 .. n) {
if (a[i] == char.init) {
foreach (k; 0 .. 3) {
if (cnt[k] > 0) {
a[i] = names[k];
--cnt[k];
break;
}
}
}
}
int req = n / 2;
if (n & 1) ++req;
if (wins >= req) {
writeln ("YES");
writeln (a);
} else {
writeln ("NO");
}
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
int n = read.to!int;
string s = readln.chomp;
int d = 0;
int dmax = 0;
foreach(c; s){
if(c == '(') d += 1;
else d -= 1;
if(d > dmax) dmax = d;
}
char[] ans;
foreach(c; s){
if(c == '('){
d += 1;
if(d > dmax / 2) ans ~= "1";
else ans ~= "0";
}
else{
if(d > dmax / 2) ans ~= "1";
else ans ~= "0";
d -= 1;
}
}
ans.writeln;
}
|
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;
immutable nt = r.next!int;
foreach (tid; 0 .. nt) {
immutable n = r.next!int;
immutable s = r.next!int;
immutable t = r.next!int;
auto y = n - s;
auto x = n - t;
auto z = s - x;
debug stderr.writefln ("x = %d, y = %d, z = %d", x, y, z);
writeln (max (x, y) + 1);
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto T = readln.chomp.to!int;
while (T--) {
auto N = readln.chomp.to!int;
if (N % 2) write(7), N -= 3;
foreach (i; 0..N/2) write(1);
writeln;
}
}
|
D
|
import std.math;
import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
long pmod(long a, long m)
{
return (a%m +m)%m;
}
void main(string[] args)
{
inputFile = stdin;
debug inputFile = File(args[1]);
auto t = next!int;
foreach(tc; 0 .. t)
{
auto a = next!long;
auto b = next!long;
auto diff = abs(a - b);
long ops = diff / 10;
if (diff % 10 != 0)
ops++;
writeln(ops);
}
}
// INPUT
enum InputStyle
{
byChunk, byLine
};
enum inputStyle = InputStyle.byLine;
File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
{
const chunkSize = 1024 * 1024 * 8;
const wordSize = 4096;
char[chunkSize] chunkBuff;
char[] currChunk;
void renewChunk()
{
if (inputFile.eof)
currChunk = null;
else
currChunk = inputFile.rawRead(chunkBuff);
}
char[wordSize] wordBuff;
char[] word;
string popWord()
{
if (currChunk.length == 0)
renewChunk;
assert(currChunk.length > 0);
while (currChunk[0].isWhite)
{
currChunk = currChunk[1 .. $];
if (currChunk.length == 0)
renewChunk;
}
word = wordBuff[0 .. 0];
while (!currChunk[0].isWhite)
{
word.length++;
word[$ - 1] = currChunk[0];
currChunk = currChunk[1 .. $];
if (currChunk.length == 0)
{
renewChunk;
if (currChunk.length == 0)
return cast(string) word;
}
}
return cast(string) word;
}
}
else
{
DList!string _words;
string popWord()
{
while (_words.empty)
{
foreach(w; inputFile.readln.split)
{
_words.insertBack(w);
}
}
auto word = _words.front;
_words.removeFront;
return word;
}
}
T next(T)()
{
return to!T(popWord);
}
auto next(T, S...)(S s)
{
static if (S.length == 0)
{
return to!T(popWord);
}
else
{
auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
foreach(ref elem; res)
elem = next!T(s[1 .. $]);
return res;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
bool solve (string s, string [] t)
{
if (t.canFind (s))
{
return true;
}
foreach (a; t)
{
foreach (b; t)
{
if ("" ~ a[1] ~ b[0] == s)
{
return true;
}
}
}
return false;
}
void main ()
{
string s;
while ((s = readln.strip) != "")
{
int n = readln.strip.to !(int);
string [] t;
foreach (i; 0..n)
{
t ~= readln.strip;
}
writeln (solve (s, t) ? "YES" : "NO");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nd = readln.split.to!(long[]);
auto N = nd[0];
auto A = nd[1];
auto B = nd[2];
auto C = nd[3];
auto D = nd[4];
foreach (m; 0..N-1) {
if (C * (N-1-m) - D*m <= B-A && B-A <= D * (N-1-m) - C*m) {
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.functional;
void main() {
writeln(readln.replace(" ", "").chomp.to!uint.pipe!(x => (cast(uint)sqrt(cast(real)x)) ^^ 2 == x) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;
void main() {
immutable int INF = 1 << 29;
auto N = readln.chomp.to!int + 2;
auto S = '0' ~ readln.chomp ~ '0';
auto dp = new int[](N);
dp.fill(-INF);
dp[0] = 0;
foreach (i; 1..N-1) {
dp[i] = max(dp[i], dp[i-1]);
if (S[i-1..i+2] != "101") continue;
for (int p = i - 1; p >= 0 && S[p] == '1'; --p) {
dp[i+1] = max(dp[i+1], dp[p-1] + i - p);
}
for (int p = i + 1; p < N && S[p] == '1'; ++p) {
dp[p] = max(dp[p], dp[i-2] + p - i);
}
}
dp[N-2].writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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 = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto q = RD!int;
auto ans = new int[](q);
foreach (i; 0..q)
{
auto n = RD!int;
int odd;
int cnt1, cnt2;
foreach (j; 0..n)
{
auto s = RD!string;
if (s.length % 2 == 1)
++odd;
foreach (c; s)
{
if (c == '1')
++cnt1;
else
++cnt2;
}
}
foreach (j; 0..odd)
{
if (cnt1 % 2 == 1)
--cnt1;
else if (cnt2 % 2 == 1)
--cnt2;
else if (cnt1 > cnt2)
--cnt1;
else
--cnt2;
}
if (cnt1 % 2 == 0 && cnt2 % 2 == 0)
ans[i] = n;
else
ans[i] = n-1;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
int[] a, b;
scan(n);
a = readln.split.to!(int[]);
b = readln.split.to!(int[]);
long s;
foreach (i ; 0 .. n) {
if (a[i] <= b[i]) {
s += (b[i] - a[i]) / 2;
}
else {
s -= (a[i] - b[i]);
}
}
writeln(s >= 0 ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD!int;
auto d = RD;
auto e = RD*5;
long ans = long.max;
while (n >= e)
{
ans = min(n % d, ans);
n -= e;
}
ans = min(n % d, ans);
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio,std.string,std.conv;
void main()
{
auto N = readln.strip.to!int;
(N>=1000?"ABD":"ABC").writeln;
}
|
D
|
import std.stdio,std.string;
int main(){
auto input=readln.chomp;
char[] s=input.dup;
foreach(ss;s[1..3]){
if(ss!=s[0]){
writeln("Yes");
return 0;
}
}
"No".writeln;
return 0;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.array;
import std.conv;
import std.math;
char[12][12] map;
int[] dx = [1,0,-1,0];
int[] dy = [0,1,0,-1];
void saiki(int x, int y){
if(map[y][x] == '1'){
map[y][x] = '0';
for(int i=0;i<4;i++){
if(x+dx[i]>=12||y+dy[i]>=12||x+dx[i]<0||y+dy[i]<0) continue;
saiki(x+dx[i],y+dy[i]);
}
}
}
void main(){
auto temp = map.dup;
while(true){
map = temp.dup;
int ans;
for(int i=0;i<12;i++){
string t = readln().chomp();
for(int j=0;j<12;j++) map[i][j] = t[j];
}
for(int i=0;i<12;i++){
for(int j=0;j<12;j++){
if(map[j][i]=='1'){
saiki(i,j);
ans++;
}
}
}
writeln(ans);
auto a = readln();
if(stdin.eof()) break;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.conv;
void main() {
string[char] d;
d['a'] = rs;
d['b'] = rs;
d['c'] = rs;
import std.uni;
solve(d).toUpper.writeln;
}
char solve(string[char] d) {
if(d['a'].length == 1) return 'a';
char turn = 'a';
while(true) {
if(d[turn].length == 0) return turn;
char tmp = turn;
turn = d[turn].front.to!char;
d[tmp].popFront;
}
}
// ===================================
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 (isBasicType!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
int ri() {
return readAs!int;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto P = RD;
writeln((A * 3 + P) / 2);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string my_readln() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto tokens = my_readln.split;
auto N = tokens[0].to!long;
ulong[] six, nine;
uint x = 6;
while (x <= N)
{
six ~= x;
x *= 6;
}
uint y = 9;
while (y <= N)
{
nine ~= y;
y *= 9;
}
auto dp = new ulong[](N+1);
fill(dp, ulong.max);
dp[0] = 0;
foreach (i; 0..N)
{
if (dp[i] == ulong.max) continue;
foreach (e; six)
{
auto j = i + e;
if (j <= N)
{
dp[j] = min(dp[j], dp[i] + 1);
}
}
foreach (e; nine)
{
auto j = i + e;
if (j <= N)
{
dp[j] = min(dp[j], dp[i] + 1);
}
}
dp[N] = min(dp[N], dp[i] + N - i);
}
writeln(dp[N]);
stdout.flush();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto h = readln.chomp.to!long;
long c = 1;
long num;
while (h != 0) {
num += c;
h /= 2;
c *= 2;
}
writeln(num);
}
|
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], C = tmp[2];
writeln(min(C, B/A));
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
string s;
scan(s);
int x, y;
scan(x, y);
auto xys = new int[][](2, 0);
auto tk = 0;
auto xb = 0;
while (xb < s.length.to!int && s[xb] == 'F') xb++;
s = s[xb .. $];
debug {
writeln(xb);
writeln(s);
}
foreach (si ; s) {
if (si == 'F') {
xys[tk].back++;
}
else {
tk ^= 1;
xys[tk] ~= 0;
}
}
xys[0] = xys[0].filter!"a > 0".array;
xys[1] = xys[1].filter!"a > 0".array;
debug {
writeln(xys);
}
auto ans = knapsack(xys[0], x - xb) && knapsack(xys[1], y);
writeln(ans ? "Yes" : "No");
}
bool knapsack(int[] x, int X) {
int n = x.length.to!int;
auto dp = new bool[][](n + 1, 40001);
dp[0][20000] = 1;
foreach (i ; 0 .. n) {
foreach (j ; 0 .. 40001) {
if (j-x[i]>=0) dp[i+1][j-x[i]] |= dp[i][j];
if (j+x[i]<40001) dp[i+1][j+x[i]] |= dp[i][j];
}
debug {
writefln("%(%b %)", dp[i+1][20000 - 10 .. 20000 + 10]);
}
}
return dp[n][X + 20000];
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str) { return str.split.to!(T[]); }
long mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
void main()
{
auto N = RD!long;
auto M = RD!long;
auto p = RDR.ARR!(long);
p[] -= 1;
UnionFind uf;
uf.init(N);
foreach (i; 0..M)
{
auto x = RD!long - 1;
auto y = RD!long - 1;
uf.unite(x, y);
}
long ans;
foreach (i; 0..N)
{
if (p[i] == i) ++ans;
else if (uf.same(p[i], i)) ++ans;
}
writeln(ans);
stdout.flush();
}
|
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 N, K;
scan(N, K);
long[10 ^^ 5 + 1] B;
foreach (_; 0 .. N)
{
long a, b;
scan(a, b);
B[a] += b;
}
long sum;
foreach (i; 0 .. 10 ^^ 5 + 1)
{
sum += B[i];
if (K <= sum)
{
writeln(i);
return;
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
int a = read.to!int;
int b = read.to!int;
int ans = min(n * a, b);
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[][10^^5] PS;
int[10^^5] MEMO;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
foreach (_; 0..M) {
auto xy = readln.split.to!(int[]);
PS[xy[0]-1] ~= xy[1]-1;
}
MEMO[0..N] = -1;
int solve(int from) {
if (MEMO[from] != -1) return MEMO[from];
if (PS[from].empty) return MEMO[from] = 0;
int max_p;
foreach (to; PS[from]) {
max_p = max(max_p, solve(to));
}
return MEMO[from] = max_p + 1;
}
foreach (i; 0..N) solve(i);
int ret;
foreach (mp; MEMO[0..N]) ret = max(ret, mp);
writeln(ret);
}
|
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;
int calc(int n, int m) {
int f(int k) {
return k * (k - 1) / 2;
}
return f(n) + f(m);
}
void main() {
int n, m; scan(n, m);
writeln(calc(n, m));
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(wchar[]);
int r, b;
foreach (c; S) {
switch (c) {
case '0':
++r;
break;
case '1':
++b;
break;
default:
}
}
writeln(min(r, b) * 2);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long D, G;
scan(D, G);
auto P_orig = new long[](D);
auto C = new long[](D);
foreach (i; 0 .. D)
scan(P_orig[i], C[i]);
long ans = long.max;
foreach (comp; 0 .. (1 << D))
{
auto P = P_orig.dup;
long score;
long cnt;
foreach (i; 0 .. D)
if (comp & (1 << i))
{
cnt += P[i];
score += P[i] * 100 * (i + 1);
score += C[i];
P[i] = 0;
}
if (G <= score)
{
ans = ans.min(cnt);
continue;
}
foreach_reverse (i; 0 .. D)
{
long need = (G - score + (100 * (i + 1) - 1)) / (100 * (i + 1));
long c = min(need, P[i]);
// writefln("%s %s %s %s %s", comp, G - score, i, need, P[i]);
score += c * (100 * (i + 1));
cnt += c;
if (G <= score)
break;
}
ans = ans.min(cnt);
}
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, tc; readV(n, tc);
int[] t; readA(n, t);
auto ans = 0;
foreach (i; 0..n-1)
ans += t[i+1]-t[i] > tc ? tc : t[i+1]-t[i];
ans += tc;
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
void main() {
auto D = readln.split[0].to!int;
auto c =
D == 25 ? "Christmas" :
D == 24 ? "Christmas Eve" :
D == 23 ? "Christmas Eve Eve" :
D == 22 ? "Christmas Eve Eve Eve" : "";
c.writeln;
}
|
D
|
void main()
{
long n = rdElem;
Edge[][] edge = new Edge[][](n);
foreach (i; 0 .. n-1)
{
long u, v, w;
rdVals(u, v, w);
--u, --v;
edge[u] ~= Edge(v, w), edge[v] ~= Edge(u, w);
}
long[] color = new long[n];
color[] = -1;
void dfs(long x, long col)
{
color[x] = col;
foreach (e; edge[x])
{
if (color[e.to] != -1) continue;
if (e.cost & 1) dfs(e.to, !col);
else dfs(e.to, col);
}
}
dfs(0, 0);
foreach (x; color)
{
x.writeln;
}
}
struct Edge
{
long to, cost;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import 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;
}
int calc(int[] xs) {
int ans = 0;
for (int i = 0; i + 1 < xs.length; i++) {
if (xs[i] == i + 1) {
ans++;
swap(xs[i], xs[i + 1]);
}
}
if (xs[$ - 1] == xs.length)
ans++;
return ans;
}
void main() {
readint;
auto xs = readints;
writeln(calc(xs));
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
foreach (i; 0 .. 10) {
string cards = readln.chomp.to!(string);
if (cards == "-") break;
int m = readln.chomp.to!int;
foreach (j; 0 .. m) {
int h = readln.chomp.to!int;
cards = cards[h..$] ~ cards[0..h];
}
cards.writeln;
}
}
|
D
|
void main()
{
dchar[] s = rdDchar;
bool rev;
dchar[] front, back;
long q = rdElem;
foreach (i; 0 .. q)
{
string[] p = rdRow!string;
if (p.length == 1)
{
rev = !rev;
}
else
{
long f = p[1].to!long - 1;
dchar c = p[2].to!dchar;
if (f)
{
if (rev) front ~= c;
else back ~= c;
}
else
{
if (rev) back ~= c;
else front ~= c;
}
}
}
if (rev)
{
s.reverse();
back.reverse();
writeln(back, s, front);
}
else
{
front.reverse();
writeln(front, s, back);
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv, std.array, std.regex, std.algorithm;
void main(){
auto ip = readln.chomp;
switch(ip){
case "a", "i", "u", "e", "o":
writeln("vowel");
break;
default:
writeln("consonant");
break;
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
auto a = new int[](n), b = new int[](n);
auto c = new int[](n), d = new int[](n);
foreach (i ; 0 .. n) {
scan(a[i], b[i]);
}
foreach (i ; 0 .. n) {
scan(c[i], d[i]);
}
auto adj = new Edge[][](2*n + 2, 0);
void addEdge(int u, int v) {
adj[u] ~= Edge(v, 1, adj[v].length.to!int);
adj[v] ~= Edge(u, 0, adj[u].length.to!int - 1);
}
foreach (i ; 0 .. n) {
addEdge(0, i + 1);
addEdge(n + i + 1, 2*n + 1);
}
foreach (i ; 0 .. n) {
foreach (j ; 0 .. n) {
if (a[i] < c[j] && b[i] < d[j]) {
addEdge(i + 1, n + j + 1);
}
}
}
int ans = FordFulkerson(adj, 0, 2*n + 1);
writeln(ans);
}
import std.typecons;
alias Edge = Tuple!(int, "to", int, "cap", int, "rev");
immutable inf = 10^^9 + 7;
int FordFulkerson(Edge[][] adj, int s, int t) {
int n = adj.length.to!int;
auto visited = new bool[](n);
int dfs(int u, int f) {
if (u == t) {return f;}
visited[u] = 1;
foreach (ref e ; adj[u]) {
if (!visited[e.to] && e.cap > 0) {
int d = dfs(e.to, min(f, e.cap));
if (d > 0) {
e.cap -= d;
adj[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int F;
while (true) {
visited[] = 0;
int f = dfs(0, inf);
if (!f) break;
F += f;
}
return F;
}
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.algorithm;
void main() {
string S = readln.chomp;
int[char] arr;
foreach (c; 'a'..'z') {
arr[c] = 0;
}
foreach (c; S) {
arr[c]++;
}
char add_char = 'z' + 1;
foreach (c; arr.byKey) {
if (arr[c] != 0) continue;
add_char = min(add_char, c);
}
if (add_char != 'z' + 1) {
writeln(S ~ add_char);
return;
}
char min_char = 'z' + 1;
bool f = false;
for (long i = S.length - 2; i != ulong.max; i--) {
for (long j = i + 1; j < S.length; j++) {
if (S[j] > S[i]) {
min_char = min(min_char, S[j]);
f = true;
}
}
if (f) {
writeln(S[0..i] ~ min_char);
return;
}
}
writeln(-1);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
const long mod = 998_244_353;
void solve(){
int n = read.to!int;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
long[][] ws;
foreach(a; as){
long [] w;
for(long x = a; x > 0; x /= 10) w ~= x % 10;
ws ~= w;
}
long ans;
foreach(w; ws){
int i = 0;
long m = 1;
for(; i < w.length; i += 1, m *= 100, m %= mod){
ans += w[i] * 11 * m * n % mod;
ans %= mod;
}
}
ans.writeln;
}
|
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()
{
writeln((lread() + 1) / 2);
}
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.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
auto mn=readln.split.map!(to!int).array;
auto m=mn[0];
auto n=mn[1];
string[] as;
string[] bs;
foreach(i;0..m)as~=readln.chomp;
foreach(i;0..n)bs~=readln.chomp;
foreach(x;0..(m-n+1))
outer:foreach(y;0..(m-n+1)){
foreach(dx;0..n)
foreach(dy;0..n){
if(as[x+dx][y+dy]!=bs[dx][dy])continue outer;
}
writeln("Yes");
return;
}
writeln("No");
return;
}
|
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 N, M;
scan(N, M);
auto A = aryread();
long s = A.sum();
long cnt;
foreach (a; A)
{
cnt += s <= 4 * a * M;
}
writeln(M <= cnt ? "Yes" : "No");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
DList!int queue;
foreach (i; 0..N) {
if (i % 2 == 0) queue.insertFront(A[i]);
else queue.insertBack(A[i]);
}
if (N % 2 == 0) foreach (i; 0..N) write(queue.back, " "), queue.removeBack;
else foreach (i; 0..N) write(queue.front, " "), queue.removeFront;
writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
auto week = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"];
string s;
scan(s);
auto ans = 6 - week.countUntil(s);
if (s == "SUN") ans = 7;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.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[] digits(int n) {
if (n == 0) return [0];
int[] d;
while (n > 0) {
d ~= n % 10;
n /= 10;
}
d.reverse();
return d;
}
void main() {
int n = readint;
writeln(n % digits(n).sum == 0 ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
long cnt, x = long.max;
foreach (i; 0..n)
{
auto a = RDA;
foreach (j; 0..m)
{
x.chmin(abs(a[j]));
if (a[j] < 0)
++cnt;
ans[ti] += abs(a[j]);
}
}
if (cnt % 2)
{
ans[ti] -= x*2;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
void main()
{
dchar[] s = rdDchar;
dchar[] t = rdDchar;
LCS!dchar(s, t).writeln;
}
T[] LCS(T)(T[] x, T[] y)
{
T[] result;
long xlen = x.length, ylen = y.length;
long[][] dp = new long[][](xlen+1, ylen+1);
foreach (i, u; x)
{
foreach (j, v; y)
{
if (u == v) dp[i+1][j+1] = dp[i][j] + 1;
else dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
}
}
for (long i = xlen, j = ylen; i && j; )
{
if (x[i-1] == y[j-1])
{
result ~= x[i-1];
--i, --j;
}
else
{
if (dp[i][j-1] < dp[i-1][j]) --i;
else --j;
}
}
reverse(result);
return result;
}
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 chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.numeric;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
void main() {
auto cin = new Scanner;
long n, p;
cin.scan(n, p);
if (p == 1) {
writeln(1);
return;
}
if (n == 1) {
writeln(p);
return;
}
auto prime = (long n) {
if (n == 1)
return 1;
if (n % 2 == 0)
return 2;
for (long i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
};
int[long] m;
while (p != 1) {
auto i = prime(p);
p /= i;
m[i]++;
}
long ret = 1;
foreach (key, value; m) {
ret *= key ^^ (value / n);
}
writeln(ret);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto q = RD!int;
auto ans = new long[](q);
foreach (i; 0..q)
{
auto n = RD!int;
auto a = RDA;
auto s = a.sum;
ans[i] = s / n + (s % n != 0 ? 1 : 0);
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
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()
{
auto s = sread();
bool flag = true;
foreach(i, e; s)
{
if(i % 2)
flag &= (e == 'i');
else
flag &= (e == 'h');
}
if(!(s.length % 2) && flag)
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.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.datetime;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto Y = s[1];
for (int i = 0; i * 10000 <= Y && i <= N; ++i) {
for (int j = 0; i * 10000 + j * 5000 <= Y && i + j <= N; ++j) {
int k = Y - i*10000 - j*5000;
if (i + j + k/1000 == N) {
writeln(i, " ", j, " ", k/1000);
return;
}
}
}
writeln(-1, " ", -1, " ", -1);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[][](t);
foreach (ti; 0..t)
{
auto n = RD;
for (long i = 2; i*i <= n; ++i)
{
if (n % i) continue;
ans[ti] = [n / i, n / i * (i-1)];
break;
}
if (ans[ti].empty)
{
ans[ti] = [1, n-1];
}
}
foreach (e; ans)
writeln(e[0], " ", e[1]);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional;
void main() {
readln.enumerate.filter!(a => a.index % 2 == 0).map!(a => a.value).array.writeln;
}
|
D
|
void main(){
int a = _scan();
(a+a^^2+a^^3).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto aa = new bool[long][](3);
foreach (n; [1, 3, 5, 7, 8, 10, 12])
aa[0][n] = true;
foreach (n; [4, 6, 9, 11])
aa[1][n] = true;
aa[2][2] = true;
long x, y;
scan(x, y);
foreach (a; aa)
{
if (x in a && y in a)
{
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
string row;
foreach (i; 0 .. 300) row ~= "#";
while (true) {
int[] tmp = readln.chomp.split.to!(int[]);
int h = tmp[0], w = tmp[1];
if (h == 0 && w == 0) break;
foreach (i; 0 .. h) row[0..w].writeln;
writeln;
}
}
|
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[] d = [-1,1];
int h,w;
int[][] hwall,wwall;
int saiki(){
Tuple!(int,int)[] queuex,queuey;
int x,y;
bool[31][31] used;
foreach(i;used)foreach(j;i) j = false;
int ans = 2;
int temp;
int c;
queuex ~= tuple(x,1);
queuey ~= tuple(y,1);
while(c < to!int(queuex.length)){
x = queuex[c][0];
y = queuey[c][0];
ans = queuex[c][1];
c++;
if(x == w-1 && y == h-1){
return ans;
}
foreach(i;d){
if(x+i>=w || x+i < 0 ) continue;
if(i == -1 && wwall[y][x]) continue;
if(i != -1 && wwall[y][x+i]) continue;
if(used[y][x+i]) continue;
used[y][x+i] = true;
queuex ~= tuple(x+i,ans+1);
queuey ~= tuple(y,ans+1);
}
foreach(i;d){
if(y+i>=h || y+i < 0 ) continue;
if(i == -1 && hwall[y][x]) continue;
if(i != -1 && hwall[y+i][x]) continue;
if(used[y+i][x]) continue;
used[y+i][x] = true;
queuex ~= tuple(x,ans+1);
queuey ~= tuple(y+i,ans+1);
}
}
return 0;
}
void main(){
while(true){
int[][] th,tw;
auto s = readln().split().to!(int[]);
if(s[0] == 0 && s[1] == 0) break;
w = s[0];
h = s[1];
th ~= new int[31];
tw ~= 0 ~ readln().split().to!(int[]);
for(int i=0;i<h-1;i++){
th ~= readln().split().to!(int[]);
tw ~= 0 ~ readln().split().to!(int[]);
}
hwall = th.dup();
wwall = tw.dup();
writeln(saiki());
}
}
|
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()
{
string A, B, C;
scan(A, B, C);
bool b = A[$ - 1] == B[0] && B[$ - 1] == C[0];
writeln(b ? "YES" : "NO");
}
|
D
|
import std;
bool calc(int a, int b, int c, int d) {
int x = (c + b - 1) / b;
int y = (a + d - 1) / d;
return x <= y;
}
void main() {
int a, b, c, d; scan(a, b, c, d);
writeln(calc(a, b, c, d) ? "Yes" : "No");
}
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.