code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio;
import std.conv;
import std.algorithm;
import std.string;
import std.file;
import std.math;
int main() {
string l;
while((l = readln()).length >= 2){
int a, b, aorg, borg, r;
a = aorg = to!int(l.split()[0]), b = borg = to!int(l.split()[1]);
while(b > 0){
a = a - (a / b) * b;
r = a, a = b, b = r;
}
printf("%d %d\n", a, aorg / a * borg);
}
return 0;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 998244353L;
void main()
{
auto ns = readln.split.to!(int[]);
auto N = ns[0];
auto S = ns[1];
auto AS = readln.split.to!(int[]);
auto DP = new long[][](N+1, S+1);
DP[0][0] = 1;
foreach (i; 0..N) {
foreach (j; 0..S+1) {
(DP[i+1][j] += DP[i][j] * 2) %= P;
if (j + AS[i] <= S) (DP[i+1][j+AS[i]] += DP[i][j]) %= P;
}
}
writeln(DP[N][S]);
}
|
D
|
import std.string;
import std.stdio;
import std.algorithm;
void main() {
auto s = readln.chomp.dup;
auto t = readln.chomp.dup;
if (s.length == 0 || t.length == 0) {
writeln();
return;
}
auto dp = new int[][t.length + 1];
foreach (ref e;dp) {
e = new int[s.length + 1];
}
foreach (size_t i, te;t) {
foreach (size_t j, se;s) {
if (te == se) {
dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1]);
} else {
dp[i + 1][j + 1] = max(dp[i][j], dp[i + 1][j], dp[i][j + 1]);
}
}
}
auto result = new char[dp[t.length][s.length]];
auto y = t.length, x = s.length;
for (;;) {
auto num = dp[y][x];
while (y >= 1 && dp[y - 1][x] == num) {
--y;
}
while (x >= 1 && dp[y][x - 1] == num) {
--x;
}
if (y == 0 || x == 0) break;
result[num - 1] = s[x - 1];
--x;
--y;
}
writeln(result);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
int N = readln.chomp.to!int;
foreach(int i; 0..N) {
long[] input = readln.split.to!(long[]);
long x = input[0];
long y = input[1];
long b = input[2];
long p = input[3];
long value1 = x*b + y*p;
long value2 = (x*max(b, 5) + y*max(p, 2))*8/10;
min(value1, value2).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 bool[](q);
foreach (i; 0..q)
{
auto n = RD!int;
auto a = RD!string;
auto b = RD!string;
int pos;
ans[i] = true;
foreach (j; 0..n)
{
if (pos == 0)
{
if ([a[j]].to!int >= 3)
{
if ([b[j]].to!int <= 2)
{
ans[i] = false;
break;
}
pos = 1;
}
}
else
{
if ([b[j]].to!int >= 3)
{
if ([a[j]].to!int <= 2)
{
ans[i] = false;
break;
}
pos = 0;
}
}
}
if (pos == 0)
ans[i] = false;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long K = lread();
long ans;
foreach (i; 1 .. K + 1)
foreach (j; 1 .. K + 1)
foreach (k; 1 .. K + 1)
{
ans += gcd(i, j).gcd(k);
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!string;
foreach (i, c; s)
{
if (i % 2 == 0)
write(c);
}
writeln();
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
import std.uni;
void main()
{
while (true)
{
auto a = readln;
if (a == "0\n") break;
writeln(a.strip.map!(x => x - '0').sum);
}
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main(){
string s, w = readln.chomp;
while(true){
string tmp = readln.chomp;
if (tmp == "END_OF_TEXT") break;
s ~= tmp.toLower ~ " ";
}
int c = 0;
foreach(x; s.split){
if (x == w) c++;
}
writeln(c);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto L = readln.chomp.to!int;
if (L <= 60) {
writeln(2, " ", L);
foreach (l; 0..L) {
writeln("1 2 ", l);
}
return;
}
int[] AS, BS, CS;
int k, l = 1;
while (l*2 <= L) {
++k;
l *= 2;
}
int x = 1;
foreach (_; 0..k) {
l /= 2;
AS ~= [x, x];
BS ~= [x+1, x+1];
CS ~= [l, 0];
x += 1;
}
auto d = 2^^k;
auto r = L - d;
while (r) {
if (r == 1) {
AS ~= 1;
BS ~= k+1;
CS ~= L-1;
break;
}
l = 2;
foreach_reverse (y; 1..k+1) {
if (l*2 > r) {
AS ~= 1;
BS ~= y;
CS ~= d;
r -= l;
d += l;
break;
}
l *= 2;
}
}
writeln(k+1, " ", AS.length);
foreach (i; 0..AS.length) writeln(AS[i], " ", BS[i], " ", CS[i]);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias Path = Tuple!(int, "to", long, "d");
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
Path[][] G;
G.length = N;
foreach (_; 0..M) {
auto lrd = readln.split.to!(int[]);
auto L = lrd[0]-1;
auto R = lrd[1]-1;
auto D = lrd[2];
G[L] ~= Path(R, D);
G[R] ~= Path(L, -D);
}
auto ds = new long[](N);
auto fs = new bool[](N);
bool ng;
void solve(int i, int p, long d) {
if (fs[i]) {
if (ds[i] != d) ng = true;
} else {
ds[i] = d;
fs[i] = true;
foreach (n; G[i]) if (n.to != p) solve(n.to, i, d + n.d);
}
}
foreach (i; 0..N) if (!fs[i]) solve(i, -1, 0);
writeln(ng ? "No" : "Yes");
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto input = readln.split.to!(int[]);
auto N = input[0];
auto i = input[1];
writeln(N-i+1);
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//}}}
void main() {
Scanner sc = new Scanner;
int A, B, C, D;
sc.scan(A, B, C, D);
while (1) {
C -= B;
if (C < 1) {
writeln("Yes");
return;
}
A -= D;
if (A < 1){
writeln("No");
return;
}
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.string;
void main()
{
int n, k;
n = readln.chomp.to!int;
k = readln.chomp.to!int;
int res = 1;
foreach (int i; 0..n) {
res = min(res * 2, res + k);
}
writeln(res);
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, k;
rd(n, k);
auto x = new long[](n), y = new long[](n);
foreach (i; 0 .. n)
rd(x[i], y[i]);
long mn = 9 * 10L ^^ 18;
foreach (a; 0 .. n) {
foreach (b; 0 .. n) {
foreach (c; 0 .. n) {
foreach (d; 0 .. n) {
auto left = x[a], right = x[b], top = y[c], bottom = y[d];
int count = 0;
foreach (i; 0 .. n) {
if (left <= x[i] && x[i] <= right && bottom <= y[i] && y[i] <= top) {
count++;
}
}
if (count >= k) {
mn = min(mn, (right - left) * (top - bottom));
}
}
}
}
}
writeln(mn);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
long[10^^5] XS;
long[10^^5] VS;
void main()
{
auto nc = readln.split.to!(long[]);
auto N = nc[0];
auto C = nc[1];
foreach (i; 0..N) {
auto xv = readln.split.to!(long[]);
XS[i] = xv[0];
VS[i] = xv[1];
}
long[10^^5] tb, tba;
long max_v = tb[N-1] = tba[N-1] = VS[N-1] - (C - XS[N-1]);
foreach_reverse (i; 0..N-1) {
tb[i] = tba[i] = tb[i+1] + VS[i] - XS[i+1] + XS[i];
if (tb[i] < max_v) {
tba[i] = max_v;
} else {
max_v = tb[i];
}
}
long[10^^5] tbb;
max_v = tbb[0] = VS[0] - XS[0];
foreach (i; 1..N) {
auto v = tbb[i-1] - XS[i-1] + tba[i];
if (v > max_v) max_v = v;
tbb[i] = tbb[i-1] + VS[i] - XS[i] + XS[i-1];
if (tbb[i] > max_v) max_v = tbb[i];
}
auto r1 = max_v;
max_v = tb[0] = tba[0] = VS[0] - XS[0];
foreach (i; 1..N) {
tb[i] = tba[i] = tb[i-1] + VS[i] - XS[i] + XS[i-1];
if (tb[i] < max_v) {
tba[i] = max_v;
} else {
max_v = tb[i];
}
}
max_v = tbb[N-1] = VS[N-1] - (C - XS[N-1]);
foreach_reverse (i; 0..N-1) {
auto v = tbb[i+1] - (C - XS[i+1]) + tba[i];
if (v > max_v) max_v = v;
tbb[i] = tbb[i+1] + VS[i] - XS[i+1] + XS[i];
if (tbb[i] > max_v) max_v = tbb[i];
}
auto r2 = max_v;
writeln(max(r1, r2, 0));
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
immutable int base = 2050;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(long);
if (n % base != 0)
{
writeln (-1);
}
else
{
n /= base;
writeln (n.text.map !(q{a - '0'}).sum);
}
}
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format;
// }}}
// 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, A, B;
cin.scan(N, A, B);
writeln(N / (A + B) * A + (N % (A + B) < A ? N % (A + B) : A));
}
|
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 s = readln.chomp;
(s.count('+').to!int - s.count('-').to!int).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
int[][] G;
G.length = N;
foreach (i; 0..M) {
auto ab = readln.split.to!(int[]);
auto A = ab[0]-1;
auto B = ab[1]-1;
G[A] ~= B;
G[B] ~= A;
}
auto res = new int[](N);
auto ss = [0];
while (!ss.empty) {
int[] nss;
foreach (s; ss) {
foreach (n; G[s]) if (res[n] == 0) {
res[n] = s+1;
nss ~= n;
}
}
ss = nss;
}
writeln("Yes");
foreach (r; res[1..$]) writeln(r);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.exception;
int n; rd(n);
void q(int i){
writeln(i); stdout.flush();
}
const M=0, F=1, V=-1;
int read(){
auto t=readln.chomp.to!(char[]);
if(t[0]=='M') return M;
else if(t[0]=='F') return F;
else return V;
}
int l=0, r=n-1;
q(l); auto ls=read();
if(ls==V) return;
q(r); auto rs=read();
if(rs==V) return;
void fun(int l, int ls, int r, int rs){
if((r-l+1)&1){ // odd
enforce(ls!=rs);
auto m=(r+l)/2;
q(m); auto ms=read();
if(ms==V) return;
if(ls==ms){
if((m-l+1)&1) fun(m, ms, r, rs);
else fun(l, ls, m, ms);
}else{
if((m-l+1)&1) fun(l, ls, m, ms);
else fun(m, ms, r, rs);
}
}else{
enforce(ls==rs);
auto m=(r+l)/2;
q(m); auto ms=read();
if(ms==V) return;
if(ls==ms){
if((m-l+1)&1) fun(m, ms, r, rs);
else fun(l, ls, m, ms);
}else{
if((m-l+1)&1) fun(l, ls, m, ms);
else fun(m, ms, r, rs);
}
}
}
fun(l, ls, r, rs);
}
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.conv,std.string;
void main(){
auto AB = readLine!int();
if ( AB[0] <= 8 && AB[1] <= 8 ){
writeln("Yay!");
}else{
writeln(":(");
}
}
T[] readLine(T)(){
T[] ret;
foreach( elm ; readln().split() ){
ret ~= elm.to!int();
}
return ret;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;
string[] C = ["dreamer", "eraser", "dream", "erase"];
bool judge(string sub_str) {
if (sub_str.length == 0)
return true;
bool ok = false;
foreach(c; C) {
if (sub_str.length < c.length)
continue;
if (sub_str[0..c.length] == c)
ok = ok || judge(sub_str[c.length..$]);
}
return ok;
}
void main() {
string S; readlnTo(S);
writeln(judge(S) ? "YES" : "NO");
}
// helpers
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..$];
}
}
|
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, a, b;
scan(n, a, b);
int mx = min(a, b);
int mn = max(0, a + b - n);
writeln(mx, " ", mn);
}
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.range;
import std.string;
import std.conv;
// 素因数分解
uint[] prime_factor(uint n) {
uint[] res = [];
for(uint i = 2; i * i <= n; i++) {
if (n % i == 0) {
do {
res ~= i;
n = n / i;
} while (n % i == 0);
}
}
if (n > 1) res ~= n;
return res;
}
void main() {
// 5 と 2 の約数を調べる
while(true) {
uint n = readln.chomp.to!(uint);
uint a = 0, b = 0;
if (n == 0) break;
foreach(numbers; iota(0, n + 1).map!(prime_factor)) {
foreach(number; numbers) {
if (number == 2) a++;
else if (number == 5) b++;
}
}
min(a, b).writeln;
}
}
|
D
|
import std.stdio,
std.string,
std.range,
std.algorithm;
void main(){
int n, x, cnt;
while (true){
scanf("%d %d", &n, &x);
if (n == 0 && x == 0) break;
cnt = 0;
n+=1;
foreach (i; 1..n){
foreach (j; i..n){
foreach (k; j..n){
if (i == j || i == k || j == k) continue;
if (i+j+k == x) cnt++;
}
}
}
writeln(cnt);
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
string s = readStr;
int l = s.length.to!int;
if(s.count("o") + 15 - l >= 8){
writeln("YES");
} else {
writeln("NO");
}
}
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long N;
scanln(N);
long[] as = readln.split.to!(long[]);
long s = as.sum;
long d = INF;
long i = -1;
foreach(j; 0..N) {
long x = abs(as[j]*N - s);
if (x < d) {
d = x;
i = j;
}
}
i.writeln;
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
Unqual!Element seed = r.front;
r.popFront();
return extremum!selector(r, seed);
}
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
Unqual!CommonElement extremeElement = seedElement;
alias selectorFun = binaryFun!selector;
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
return extremeElement;
}
auto minElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum(r);
}
auto minElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto minElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum(r, seed);
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!`a > b`(r);
}
auto maxElement(alias map, Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
auto maxElement(Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!`a > b`(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
void main(){
string n = readln().chomp();
char[] ans;
foreach(elm; n){
ans ~= (elm=='9')? '1': '9';
}
ans.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] input = new string[](2);
bool[] cup = new bool[](3);
cup.fill(false);
cup[0] = true;
while ((input = readln.chomp.split(",")).length != 0) {
swap(cup[(input[0][0] - 'A').to!int], cup[(input[1][0] - 'A').to!int]);
}
foreach (i; 0..cup.length) {
if (cup[i]) {
writeln((i + 'A').to!char);
break;
}
}
}
|
D
|
void main(){
char[] s = readln().chomp().dup();
char[] t = readln().chomp().dup();
sort!("a<b")( cast(ubyte[]) s);
sort!("a>b")( cast(ubyte[]) t);
writeln(s<t?"Yes":"No");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto s = sread();
long i = 1;
string previous; //前の文字
previous = s[0 .. 1];
// writeln(previous);
long cnt = 1;
// writeln(s);
while (i < (s.length))
{
// writeln(s[i .. i + 1]);
if (previous == s[i .. i + 1])
{
if (i + 2 <= s.length)
{
previous = s[i .. i + 2];
i += 2;
cnt += 1;
}
else
{
break;
}
}
else
{
if (i + 1 <= s.length)
{
previous = s[i .. i + 1];
i += 1;
cnt += 1;
}
else
{
break;
}
}
// writeln("i:", i);
// writeln("pre:", previous);
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto size = readln.chomp.to!int;
auto lucky = cast(byte[])readln.chomp;
lucky[] -= '0';
void solve() {
int count;
bool[byte] ava;
foreach(a; lucky) ava[a] = true;
foreach(x; ava.keys) foreach(y; ava.keys) foreach(z; ava.keys) {
if (auto after_x = lucky.findSplit([x])) {
if (auto after_y = after_x[2].findSplit([y])) {
if (after_y[2].canFind(z)) {
count++;
}
}
}
}
writeln(count);
}
solve();
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto D = aryread();
auto cnt = new long[](13);
cnt[0] = 1;
foreach (d; D)
{
cnt[d]++;
if (cnt[d] == 3 || ((d == 0 || d == 12) && cnt[d] == 2))
{
writeln(0);
return;
}
}
auto t = new bool[](24);
t[0] = true;
t[12] = (cnt[12] != 0);
bool b;
foreach (i; 1 .. 12)
{
if (cnt[i] == 1)
t[(b ? 24 - i : i)] = true;
if (cnt[i] == 2)
t[24 - i] = t[i] = true;
if (cnt[i] != 0)
b = (b == false);
}
// writeln(t.map!(to!long));
long ans = long.max;
foreach (i; 0 .. 24)
foreach (j; i + 1 .. 24)
if (t[i] && t[j])
{
long diff = j - i;
ans = min(ans, diff, 24 - diff);
}
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 inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, z, w;
scan(n, z, w);
auto a = readln.split.to!(int[]);
if (n == 1) {
auto ans = abs(a[0] - w);
writeln(ans);
return;
}
auto ans = max(abs(a[n - 1] - w), abs(a[n - 2] - a[n - 1]));
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD!string;
auto B = RD!string;
int ans;
if (A.length == B.length)
{
foreach (i; 0..A.length)
{
auto x = A[i].to!int;
auto y = B[i].to!int;
if (x == y) continue;
ans = x < y ? -1 : 1;
break;
}
}
else
ans = A.length == B.length ? 0 : A.length < B.length ? -1 : 1;
writeln(ans == -1 ? "LESS" : ans == 0 ? "EQUAL" : "GREATER");
stdout.flush();
debug readln();
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string S;
sc.scan(S);
auto y = S[0 .. 4];
foreach (i; 0 .. 5)
S.popFront;
auto m = S[0 .. 2];
foreach (i; 0 .. 3)
S.popFront;
auto d = S[0 .. 2];
if (to!int(y) > 2019) {
writeln("TBD");
return;
} else {
if (to!int(m) > 4) {
writeln("TBD");
return;
}
}
writeln("Heisei");
}
|
D
|
// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio : readln, writeln;
import std.string : chomp;
import std.array : split;
import std.conv : to;
import std.algorithm : min;
auto gets() { return readln.chomp; }
auto getV(T)() { return gets.to!T; }
auto getVals(T)() { return gets.split.to!(T[]); }
void main() {
auto nkt = getVals!int;
auto n = nkt[0], k = nkt[1], t = nkt[2];
if (t <= k) {
writeln(t);
return;
}
if (t <= n) {
writeln(k);
return;
}
writeln(k - (t - n));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
alias P = Tuple!(int, "a", int, "b");
void main()
{
auto hwn = readln.split.to!(int[]);
long H = hwn[0];
long W = hwn[1];
auto N = hwn[2];
int[P] ps;
foreach (_; 0..N) {
auto ab = readln.split.to!(int[]);
auto a = ab[0];
auto b = ab[1];
static foreach (ad; [-1, 0, 1])
static foreach (bd; [-1, 0, 1]) {{
auto h = ad+a;
auto w = bd+b;
if (1 < h && h < H && 1 < w && w < W) ++ps[P(h, w)];
}}
}
long[10] res;
res[0] = (H-2) * (W-2);
foreach (_, v; ps) {
--res[0];
++res[v];
}
static foreach (i; 0..10) writeln(res[i]);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
long prev = 0;
long ans = 1;
foreach (i; 1 .. N)
{
long now = A[i] - A[i - 1];
if (prev == 0)
{
prev = now;
continue;
}
if (prev * now < 0)
{
prev = 0;
ans++;
continue;
}
}
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;
alias Point = Tuple!(int, "x", int, "y", int, "c");
void main() {
int n;
scan(n);
int ans = 10^^9;
foreach (a ; 1 .. n) {
ans = min(ans, s(a) + s(n - a));
}
writeln(ans);
}
int s(int x) {
return x > 0 ? s(x/10) + (x % 10) : 0;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string;
void main(){
auto ip = readln.chomp.to!int;
int res;
for(int i = 0; i <= ip; i++){
res += i;
}
res.writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto N = my_readln().to!ulong();
auto tokens = split(my_readln());
auto A = tokens[0].to!ulong();
auto B = tokens[1].to!ulong();
ulong[3] cnt;
foreach (token; split(my_readln()))
{
auto p = token.to!ulong();
if (p <= A) ++cnt[0];
else if (p <= B) ++cnt[1];
else ++cnt[2];
}
writeln(min(min(cnt[0], cnt[1]), cnt[2]));
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto G = new int[][](N);
foreach (_; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= s[1]-1;
G[s[1]-1] ~= s[0]-1;
}
if (N == 1) {
writeln("First");
return;
} else if (N == 2) {
writeln("Second");
return;
}
Tuple!(int, int) dfs(int n, int p) {
auto ret = tuple(0, n);
foreach (m; G[n]) {
if (m == p) continue;
auto t = dfs(m, n);
t[0] += 1;
ret = max(ret, t);
}
return ret;
}
auto d = dfs(dfs(0, -1)[1], -1)[0];
int g = (d - 1) % 3;
writeln(g > 0 ? "First" : "Second");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b, c, d; readV(a, b, c, d);
auto n = 101, e = new int[](n);
e[a]++; e[b]--; e[c]++; e[d]--;
foreach (i; 1..n) e[i] += e[i-1];
writeln(e.filter!"a==2".count);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long a, b, c;
scan(a, b, c);
(a + b).min(b + c).min(a + c).writeln();
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
long R = scanElem;
if(R<1200){
writeln("ABC");
return;
}
if(R<2800){
writeln("ARC");
return;
}
writeln("AGC");
return;
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
dchar[] s; readV(s);
s.sort();
writeln(s == "abc" ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
import core.bitop;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
auto BS = readln.split.to!(int[]);
auto DP = new int[][](51, 2^^N);
foreach (ref dp; DP) dp[] = -1;
int solve(uint s, int p) {
auto i = popcnt(s);
if (i == N) return 0;
if (DP[p][s] == -1) {
int r = int.max/2;
foreach (j; 0..N) if (!(s & (1<<j))) {
auto x = (i+j)%2 == 0 ? AS[j] : BS[j];
if (x < p) continue;
int d;
foreach (k; j+1..N) if (s & (1<<k)) ++d;
r = min(r, solve(s | (1<<j), x) + d);
}
DP[p][s] = r;
}
return DP[p][s];
}
auto r = solve(0, 0);
writeln(r >= int.max/2 ? -1 : r);
}
|
D
|
import std.stdio,std.conv,std.string,std.algorithm;
void main(){
int m;
auto sm=readln().split();
m=to!int(sm[0]);
writeln(48-m);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n, t;
scan(n, t);
auto l = aryread();
long shower;
foreach (i; 0 .. n)
{
if (i == 0)
continue;
if (l[i - 1] + t > l[i])
{
shower += l[i] - l[i - 1];
}
else
{
shower += t;
}
}
shower += t;
shower.writeln();
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 n = RD!int;
auto dp = new long[](n+1);
dp[0] = 1;
dp[1] = 1;
foreach (i; 2..n)
{
dp[i] = dp[i-1];
dp[i].moda(dp[i-2]);
}
auto ans = dp[n-1];
long m = 2;
m.modpow(n);
ans.modd(m);
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 abcd = RDR.ARR;
bool ans;
if (abs(abcd[0] - abcd[2]) <= abcd[3])
{
ans = true;
}
else if (abs(abcd[0] - abcd[1]) <= abcd[3] && abs(abcd[1] - abcd[2]) <= abcd[3])
{
ans = true;
}
writeln(ans ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array;
void main()
{
foreach(unused; 0 .. readln.chomp.to!int)
{
int left = 0, right = -1;
bool flag = true;
foreach(n; readln.split.map!(to!int))
{
if(n > left && n > right)
{
if(left > right) left = n;
else right = n;
}
else if(n > left) left = n;
else if(n > right) right = n;
else
{
flag = false;
break;
}
}
(flag ? "YES" : "NO").writeln;
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!(char[]).array.count('9');
writeln(n?"Yes":"No");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
import std.bitmanip;
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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, ma, mb; readV(n, ma, mb);
int[] a, b, c; readC(n, a, b, c);
auto as = a.sum, bs = b.sum;
auto dp = new int[][][](n+1, as+1, bs+1), inf = 10^^9;
foreach (ref dpi; dp)
foreach (ref dpij; dpi)
dpij[] = inf;
dp[0][0][0] = 0;
foreach (i; 0..n)
foreach (j; 0..as+1)
foreach (k; 0..bs+1) {
dp[i+1][j][k] = dp[i][j][k];
if (j >= a[i] && k >= b[i])
dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j-a[i]][k-b[i]]+c[i]);
}
auto ans = inf;
foreach (j; 1..as+1)
foreach (k; 1..bs+1)
if (j*mb == k*ma)
ans = min(ans, dp[n][j][k]);
writeln(ans == inf ? -1 : ans);
}
|
D
|
import std.conv, std.stdio;
import std.algorithm, std.array, std.string, std.range;
void main()
{
auto s = readln.chomp.to!dstring;
if (s[0..$/2].isPalindrome && (s[0..$/2] == s[$/2+1..$]))
"Yes".writeln;
else
"No".writeln;
}
auto isPalindrome(T)(in T[] a)
{
return a.equal(a.retro);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int[] a) {
int p = 1;
int step = 0;
auto used = new int[a.length + 1];
while (p != 2) {
if (used[p]++) return -1;
p = a[p];
step++;
}
return step;
}
void main() {
int n = readint;
auto a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = readint;
}
writeln(calc(a));
}
|
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 readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=rdsp;foreach(ref j;v.tupleof)pick(r,j);}}
void main()
{
int n, t; readV(n, t);
struct R { int c, t; }
R[] r; readS(n, r);
auto cf = r.filter!(ri => ri.t <= t).map!(ri => ri.c);
if (cf.empty)
writeln("TLE");
else
writeln(cf.reduce!min);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto a = readln.chomp.to!int;
auto b = readln.chomp.to!int;
auto h = readln.chomp.to!int;
writeln((a + b) * h / 2);
}
|
D
|
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 dp = new long[][][][](n + 1, 4, 4, 4);
foreach (j1 ; 0 .. 4) {
foreach (j2 ; 0 .. 4) {
dp[2][0][j1][j2] = 1;
}
}
foreach (i ; 3 .. n + 1) {
foreach (j1 ; 0 .. 4) {
foreach (j2 ; 0 .. 4) {
foreach (j3 ; 0 .. 4) {
if (j1 == 1 && j2 == 2 && j3 == 3) continue; // ACG
if (j1 == 1 && j2 == 3 && j3 == 2) continue; // AGC
if (j1 == 3 && j2 == 1 && j3 == 2) continue; // GAC
foreach (k ; 0 .. 4) {
if (j1 == 1 && j3 == 3 && k == 2) continue; // A*GC
if (j1 == 1 && j2 == 3 && k == 2) continue; // AG*C
dp[i][j2][j3][k] += dp[i-1][j1][j2][j3];
dp[i][j2][j3][k] %= mod;
}
}
}
}
}
long ans;
foreach (j1 ; 0 .. 4) {
foreach (j2 ; 0 .. 4) {
foreach (j3 ; 0 .. 4) {
if (j1 == 1 && j2 == 2 && j3 == 3) continue; // ACG
if (j1 == 1 && j2 == 3 && j3 == 2) continue; // AGC
if (j1 == 3 && j2 == 1 && j3 == 2) continue; // GAC
ans += dp[n][j1][j2][j3];
ans %= mod;
}
}
}
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long[] t = new long[n];
foreach (i; 0 .. n)
{
t[i] = readln.chomp.to!long;
}
long time = t[0];
foreach (i; 1 .. n)
{
long g = gcd(time, t[i]);
time = time / g * t[i];
}
time.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
writeln(N*800 - (N/15)*200);
stdout.flush();
debug readln();
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
int maxi = int.min;
int n, m, q;
alias T = Tuple!(int, "a", int, "b", int, "c", int, "d");
T[] query;
int[] ar;
void rec(int idx) {
if (idx == n) {
int sum;
foreach (e; query) {
if (ar[e.b] - ar[e.a] == e.c) sum += e.d;
}
maxi = max(maxi, sum);
return;
}
if (idx == 0) {
foreach (i; 1 .. m + 1) {
ar ~= i;
rec(idx + 1);
ar.popBack;
}
} else {
foreach (i; ar.back .. m + 1) {
ar ~= i;
rec(idx + 1);
ar.popBack;
}
}
}
void main() {
auto cin = new Scanner;
cin.scan(n, m, q);
foreach (i; 0 .. q) {
int a, b, c, d;
cin.scan(a, b, c, d);
query ~= T(a - 1, b - 1, c, d);
}
rec(0);
writeln(maxi);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
int E = 0;
auto used = new bool[][](N, N);
auto black = new string[](N);
foreach (i; 0..N) black[i] = "0";
string[] ans = ["!"];
while (E < N - 1) {
int a = uniform(0, N);
int b = uniform(0, N);
if (a == b) continue;
if (used[a][b]) continue;
black[a] = black[b] = "1";
writeln("? " ~ black.join(""));
stdout.flush;
auto radius = readln.chomp.to!int;
if (radius) ans ~= ("(" ~ a.to!string ~ "," ~ b.to!string ~ ")");
E += radius;
black[a] = black[b] = "0";
used[a][b] = true;
used[b][a] = true;
}
ans.join(" ").writeln;
stdout.flush;
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int n, m;
void main() {
scan(n, m);
writeln((n - 1) * (m - 1));
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9+7;
long[10^^5+50] F, RF;
void init()
{
F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
int[2][] prime_division(int m)
{
int[2][] r;
for (int x = 2; x^^2 <= m; ++x) {
int cnt;
while (m % x == 0) {
++cnt;
m /= x;
}
if (cnt) r ~= [x, cnt];
}
if (m != 1) r ~= [m, 1];
return r;
}
void main()
{
init();
auto nm = readln.split.to!(int[]);
long N = nm[0];
auto M = nm[1];
long r = 1;
foreach (p; prime_division(M)) {
r = (r * comb(N+p[1]-1, p[1])) % P;
}
writeln(r);
}
|
D
|
void main() {
auto a = ri, b = ri, c = ri, d = ri, e = ri, k = ri;
if(e - a > k) writeln(":(");
else writeln("Yay!");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto s=readln.chomp.to!(char[]);
int f=0;
int k=0;
foreach(i; 0..n){
if(s[i]=='('){
k++;
}else{
if(k==0){
f++;
}else{
k--;
}
}
}
foreach(_; 0..f) write("(");
write(s);
foreach(_; 0..k) write(")");
writeln();
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
writeln("2018", s[4..$]);
}
|
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 K = RD;
auto seven = 7 % K;
auto ten = 10 % K;
auto used = new bool[](K);
long x = seven;
long ans = 1;
while (true)
{
if (x == 0) break;
used[x] = true;
x = (x * ten + seven) % K;
if (used[x])
{
ans = -1;
break;
}
++ans;
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string[] inputs = split(readln());
int A = to!int(inputs[0]);
int B = to!int(inputs[1]);
int C = to!int(inputs[2]);
if(A == B) writeln(C);
else if(A == C) writeln(B);
else writeln(A);
}
|
D
|
import std.stdio;
import std.string, std.conv, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons;
import core.bitop, std.datetime, std.range;
void main(){
auto rd = readln.split.to!(int[]);
auto n = rd[0], q = rd[1];
auto uf = new UnionFind(n);
foreach(lp ; 0 .. q){
rd = readln.split.to!(int[]);
auto c = rd[0], x = rd[1], y = rd[2];
if(c == 0){
uf.unite(x, y);
} else{
writeln(uf.is_same(x, y) ? 1 : 0);
}
}
}
class UnionFind{
private:
int[] ds;
int[] p;
int[] rank;
public:
this(int N){
ds = iota(N).array;
p = iota(N).array;
rank = new int[](N);
}
int find_root(int x){
if(x != p[x]){
p[x] = find_root(p[x]);
}
return p[x];
}
bool is_same(int x, int y){
return find_root(x) == find_root(y);
}
void unite(int x, int y){
auto u = find_root(x);
auto v = find_root(y);
if(rank[u] < rank[v]){
p[u] = v;
} else{
p[v] = u;
if(rank[u] == rank[v]) ++rank[u];
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto A = new long[](N);
foreach (i; 0..N)
{
A[i] = RD;
}
long ans;
foreach (i; 0..N)
{
if (i != 0)
{
if (A[i-1] != 0 && A[i] != 0)
{
++ans;
--A[i-1];
--A[i];
}
}
auto x = A[i] / 2;
ans += x;
A[i] -= x * 2;
debug writeln(ans);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.array, std.conv, std.string;
void main()
{
string[] input;
solve: while(!stdin.eof()) {
input = readln.split(",");
if(input.length != 8) return;
auto inx = new double[](4);
auto iny = new double[](4);
for(int i = 0; i < 4; i++)
{
inx[i] = input[2 * i].chomp.to!double;
iny[i] = input[2 * i + 1].chomp.to!double;
}
for(int xi = 0; xi < 4; xi++)
{
double[] x;
double[] y;
double xp, yp;
for(int i = 0; i < 4; i++)
{
if(i == xi)
{
xp = inx[i];
yp = iny[i];
continue;
}
x ~= inx[i];
y ~= iny[i];
}
bool l;
bool r;
l = r = false;
x ~= x[0];
y ~= y[0];
bool isConvex = false;
for(int i = 0; i < 3; i++) {
double ax = x[i+1] - x[i];
double ay = y[i+1] - y[i];
double px = xp - x[i];
double py = yp - y[i];
if(ax*py - ay*px < 0)
r = true;
else
l = true;
}
if(l != r)
{
"NO".writeln;
continue solve;
}
}
"YES".writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable inf = 10^^9 + 7;
alias Edge = Tuple!(int, "to", int, "cost", int, "rev");
void main() {
int h, w;
scan(h, w);
auto adj = new Edge[][](h + w + 2, 0);
int si, sj, ti, tj;
void addEdge(int u, int v, int cap) {
adj[u] ~= Edge(v, cap, adj[v].length.to!int);
adj[v] ~= Edge(u, 0, adj[u].length.to!int - 1);
}
foreach (i ; 0 .. h) {
auto line = readln.chomp;
foreach (j ; 0 .. w) {
int y = i + 1, x = h + j + 1;
if (line[j] == 'S') {
si = i, sj = j;
addEdge(0, y, inf);
addEdge(0, x, inf);
}
else if (line[j] == 'T') {
ti = i, tj = j;
addEdge(y, h + w + 1, inf);
addEdge(x, h + w + 1, inf);
}
else if (line[j] == 'o') {
addEdge(y, x, 1);
addEdge(x, y, 1);
}
}
}
if (si == ti || sj == tj) {
writeln(-1);
return;
}
else {
int ans = FordFulkerson(adj, 0, h + w + 1);
writeln(ans);
}
}
int FordFulkerson(Edge[][] adj, int s, int t) {
int n = adj.length.to!int;
bool[] visited = new bool[](n);
int dfs (int u, int f) {
if (u == t) {
debug {
writef("%d ", t);
}
return f;
}
visited[u] = 1;
foreach (ref e ; adj[u]) {
if (e.cost > 0 && !visited[e.to]) {
int d = dfs(e.to, min(f, e.cost));
if (d > 0) {
e.cost -= d;
adj[e.to][e.rev].cost += d;
debug {
writef("%d ", u);
}
return d;
}
}
}
return 0;
}
int F;
while (true) {
visited[] = 0;
int f = dfs(s, inf);
debug {
writeln("flow: ", f);
}
if (f == 0) break;
F += f;
}
return F;
}
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.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto K = RD;
long ans;
if (K % 2 == 0)
{
ans = A - B;
}
else
{
ans = B - A;
}
writeln(abs(ans) > 10LU^^18 ? "Unfair" : ans.to!string);
stdout.flush();
debug readln();
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto a = aryread();
auto b = aryread();
// writeln(a);
// writeln(b);
long cnt;
long tmp;
long tmptmp;
foreach (i; 0 .. n)
{
tmp = min(a[i], b[i]);
cnt += tmp;
a[i] -= tmp;
b[i] -= tmp;
// writeln("a:", a);
// writeln("b:", b);
// writeln(cnt);
tmptmp = min(a[i + 1], b[i]);
cnt += tmptmp;
a[i + 1] -= tmptmp;
b[i] -= tmptmp;
// writeln("a:", a);
// writeln("b:", b);
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, a, b, c; readV(n, a, b, c);
int[] l; readC(n, l);
auto d = [a, b, c], r = 10^^9;
foreach (i; 0..1<<(n*2)) {
auto sl = new int[](4), sc = new int[](4);
foreach (j; 0..n) {
auto k = ((i>>(j*2))&3);
sl[k] += l[j];
++sc[k];
}
if (sc[0..3].all!"a>0")
r = min(r, 3.iota.map!(j => (d[j]-sl[j]).abs+(sc[j]-1)*10).sum);
}
writeln(r);
}
|
D
|
void main()
{
long n = rdElem;
n %= 10;
if (n == 3)
{
"bon".writeln;
}
else if (n == 0 || n == 1 || n == 6 || n == 8)
{
"pon".writeln;
}
else
{
"hon".writeln;
}
}
enum long mod = 10L^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.array, std.conv;
void main() {
while (true) {
string[] input = split(readln());
int H = to!int(input[0]);
int W = to!int(input[1]);
if (H == 0 && W == 0) break;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
write(is_dots(i, j) ? "." : "#");
}
writeln("");
}
writeln("");
}
}
bool is_dots(int y, int x) {
return (((y % 2) + x) % 2 == 1);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto memo = new long[][](10, 10);
foreach (n; 1..N+1) {
auto y = n%10;
int x;
while (n) {
x = n;
n /= 10;
}
++memo[x][y];
}
long r;
foreach (i; 1..10) {
foreach (j; 1..10) {
r += memo[i][j] * memo[j][i];
}
}
writeln(r);
}
|
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;
int[] Z_algorithm(string s) {
auto A = new int[](s.length);
A[0] = s.length.to!int;
for (int i = 1, j = 0; i < s.length; ) {
while (i + j < s.length && s[j] == s[i+j]) ++j;
A[i] = j;
if (j == 0) { ++i; continue; }
int k = 1;
while (i + k < s.length && k + A[k] < j) A[i+k] = A[k], ++k;
i += k; j -= k;
}
return A;
}
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
if (all(S.map!(s => s == S[0]))) {
writeln(N);
writeln(1);
return;
}
string T;
foreach (i; 0..N) T ~= S[N - i - 1];
auto z1 = Z_algorithm(S);
auto z2 = Z_algorithm(T);
auto ng1 = new bool[](N + 1);
auto ng2 = new bool[](N + 1);
for (int i = 0; i < N; i++) {
if (i > 0 && i - z1[i] <= 0) {
int len = min(i, z1[i]);
int end = min(i + z1[i], N) + 1;
for (int j = i + len; j < end && !ng1[j]; j += len)
ng1[j] = true;
}
}
for (int i = 0; i < N; i++) {
if (i > 0 && i - z2[i] <= 0) {
int len = min(i, z2[i]);
int end = min(i + z2[i], N) + 1;
for (int j = i + len; j < end && !ng2[j]; j += len)
ng2[j] = true;
}
}
ng2.reverse;
auto ng = (N + 1).iota.map!(i => ng1[i] || ng2[i]);
if (!ng[0]) {
writeln(1);
writeln(1);
} else {
writeln(2);
writeln(N + 1 - ng.sum);
}
debug {
z1.writeln;
z2.writeln;
ng1.map!(x => x ? 1: 0).writeln;
ng2.map!(x => x ? 1: 0).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 s = readln.split.map!(to!int);
auto N = s[0].to!int;
auto M = s[1];
auto edges = new int[][](N);
foreach (_; 0..M) {
s = readln.split.map!(to!int);
edges[s[0]-1] ~= s[1]-1;
edges[s[1]-1] ~= s[0]-1;
}
int[][] groups;
bool[] used = new bool[](N);
int[] color = new int[](N);
color.fill(-1);
void dfs1(int n, int g) {
if (used[n]) return;
used[n] = true;
groups[g] ~= n;
foreach (m; edges[n]) if (used[n]) dfs1(m, g);
}
bool dfs2(int n, int c) {
if (color[n] != -1) return color[n] == c;
color[n] = c;
foreach (m; edges[n]) {
if (!dfs2(m, c^1)) return false;
}
return true;
}
int gn = 0;
bool[] is_bi;
foreach (i; 0..N) if (!used[i]) {groups ~= (new int[](0)); dfs1(i, gn++);}
foreach (g; 0..gn) is_bi ~= dfs2(groups[g][0], g);
long ans = 0;
long G = groups.length;
long one = groups.map!(g => g.length == 1).sum;
long bi = is_bi.sum - one;
long other = G - one - bi;
ans += one * 2 * N - one * one;
ans += bi * 2 + other;
ans += bi * (bi - 1) * 2;
ans += other * (other - 1);
ans += other * bi * 2;
ans.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto A = new long[][](N);
foreach (i; 0..N)
{
A[i] = RDA(-1L);
}
long[] open;
foreach (i; 0..N) open ~= i;
//open ~= -1;
auto used = new long[](N);
while (!open.empty)
{
auto lhs = open.front; open.popFront;
debug writeln(lhs);
/*if (lhs == -1)
{
++ans;
if (open.empty) break;
continue;
}*/
if (A[lhs].empty) continue;
auto rhs = A[lhs].front;
if (A[rhs].empty)
break;
if (A[rhs].front == lhs)
{
auto d = max(used[lhs], used[rhs]) + 1;
used[lhs] = d;
used[rhs] = d;
A[lhs].popFront;
A[rhs].popFront;
if (!A[lhs].empty)
open ~= lhs;
if (!A[rhs].empty)
open ~= rhs;
}
}
bool ok = true;
foreach (i; 0..N)
{
if (!A[i].empty)
ok = false;
}
if (ok)
{
long ans;
foreach (i; 0..N)
ans = max(ans, used[i]);
writeln(ans);
}
else
writeln(-1);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.regex;
void main(){
auto ip = readln.split.to!(int[]), a = ip[0], b = ip[1], c = ip[2];
if(a == b + c || b == a + c || c == a + b){
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
void main()
{
long n = rdElem;
string march = "MARCH";
long[] list = new long[5];
foreach (i; 0 .. n)
{
string s = rdStr;
foreach (j, m; march)
{
if (m == s[0]) ++list[j];
}
}
long result;
foreach (i; 0 .. 3)
{
foreach (j; i+1 .. 4)
{
foreach (k; j+1 .. 5)
{
result += list[i] * list[j] * list[k];
}
}
}
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.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.stdio, std.string, std.algorithm, std.conv, std.range;
void main() {
foreach (string line; lines(stdin)) {
if (line == "0 0\n")
break;
auto inputArray = line.chomp.split.map!(to!int);
auto h = inputArray[0];
auto w = inputArray[1];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if ((i + j) % 2 == 0) {
write("#");
} else {
write(".");
}
}
writeln;
}
writeln;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
// writeln(s.substitute(',', ' '));
writeln(s.replace(",", " "));
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int N, H, W;
sc.scan(N, H, W);
writeln(((N - H) + 1) * ((N - W) + 1));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long A, B, C, D;
scan(A, B, C, D);
long a = ((A + B) - (C + D));
long b = (a == 0) ? 0 : (a / abs(a));
auto ary = ["Right", "Balanced", "Left"];
writeln(ary[b + 1]);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a;
string s;
scan(a);
scan(s);
writeln(a >= 3200 ? s : "red");
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main()
{
auto sieve = Sieve!long(10^^5);
long[] dp = new long[10^^5+1];
foreach (i; 1 .. 10^^5+1)
{
dp[i] = dp[i-1];
if (sieve.isPrime(i) && sieve.isPrime((i+1)/2))
{
++dp[i];
}
}
long q = readln.chomp.to!long;
foreach (i; 0 .. q)
{
long[] tmp = readln.split.to!(long[]);
long l = tmp[0] - 1, r = tmp[1];
writeln(dp[r] - dp[l]);
}
}
struct Sieve(T)
{
bool[] flags;
this(T limit)
{
flags = sieve(limit);
}
bool[] sieve(T limit)
{
long len = (limit - 1) / 2;
bool[] result = new bool[len];
result[] = true;
foreach (i; 0 .. len)
{
if (!result[i]) continue;
long prime = 3 + 2 * i;
foreach (j; iota(prime+i, len, prime))
{
result[j] = false;
}
}
return result;
}
size_t length() @property
{
return flags.length;
}
bool isPrime(T x)
{
if (x < 2)
{
return false;
}
else if (x == 2)
{
return true;
}
else if (x % 2 == 0)
{
return false;
}
else
{
return flags[(x-3)/2];
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a, b;
scan(a, b);
int ans;
if (b % a == 0) {
ans = a + b;
}
else {
ans = b - a;
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
auto expr=readln.chomp.to!(char[]);
int res; rd(res);
auto n=expr.length;
int[] a; a~=(expr[0]-'0');
for(auto i=1; i<n; i+=2){
if(expr[i]=='+') a~=(expr[i+1]-'0');
else{
auto bk=a.back; a.popBack;
a~=(bk*(expr[i+1]-'0'));
}
}
auto res1=sum(a);
int res2=(expr[0]-'0');
for(auto i=1; i<n; i+=2){
if(expr[i]=='+') res2+=(expr[i+1]-'0');
else res2*=(expr[i+1]-'0');
}
if(res1==res2 && res1==res) writeln("U");
else if(res1==res) writeln("M");
else if(res2==res) writeln("L");
else writeln("I");
}
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));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
void main()
{
long l, r;
rdVals(l, r);
long m = 2019;
if (l / m != r / m)
{
0.writeln;
}
else
{
long x = l % m, y = r % m;
long result = m;
foreach (i; x .. y)
{
foreach (j; i+1 .. y+1)
{
result = min(result, i*j%m);
}
}
result.writeln;
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import 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 N = RD;
auto S = RD!string;
auto cnt = new long[][](N+1, 3);
auto str = ['R':0, 'G':1, 'B':2];
foreach_reverse (i; 0..N)
{
cnt[i][] += cnt[i+1][];
auto j = str[S[i]];
++cnt[i][j];
}
long ans;
foreach (i; 0..N)
{
auto c1 = str[S[i]];
foreach (j; i+1..N)
{
auto c2 = str[S[j]];
if (c1 == c2) continue;
auto c3 = 3 - (c1+c2);
ans += cnt[j][c3];
auto d = j - i;
if (j+d >= N) continue;
if (str[S[j+d]] == c3)
--ans;
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.ascii;
import std.array;
import std.container;
import std.conv;
import std.numeric;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
const int INF = 10000;
int op(char c, int a, int b) {
if (c == '+') return a + b;
if (c == '-') return a - b;
assert(false);
}
int next_op_index(in string s, int i) {
for (int j = i; j < s.size; j++) {
if (s[j] == '+' || s[j] == '-') return j;
}
throw new Exception("");
}
int prev_op_index(in string s, int i) {
for (int j = i; j >= 0; j--) {
if (s[j] == '+' || s[j] == '-') return j;
}
throw new Exception("");
}
void main() {
string s = readln.chomp;
auto t_index = new int[s.size]; t_index[] = -1;
int cur = 0;
foreach (i, c; s) {
if (c == '(' || c == ')') continue;
t_index[i] = cur++;
}
auto buf = new char[cur];
foreach (i, c; s) {
if (c == '(' || c == ')') continue;
buf[t_index[i]] = c;
}
string t = buf.idup;
int N = t.size;
auto open_banned = new bool[N];
auto close_banned = new bool[N];
foreach (int i, c; s) {
try {
if (c == '(') close_banned[t_index[next_op_index(s, i)]] = true;
else if (c == ')') open_banned[t_index[prev_op_index(s, i)]] = true;
} catch (Exception e) {}
}
auto dp_min = new int[][](N + 1, N + 1);
auto dp_max = new int[][](N + 1, N + 1);
foreach (ref a; dp_max) a[] = -INF;
foreach (ref a; dp_min) a[] = INF;
foreach (i, c; t) {
if (c.isDigit) dp_max[i][i + 1] = dp_min[i][i + 1] = cast(int)(c - '0');
}
for (int k = 2; k <= N; k++) {
for (int i = 0; i + k <= N; i++) {
for (int j = i + 1; j < i + k; j++) {
if (! (t[j] == '+' || t[j] == '-')) continue;
int lcount = cast(int)t[i .. j].count!isDigit;
int rcount = cast(int)t[j .. i + k].count!isDigit;
if (lcount >= 2 && close_banned[j]) continue;
if (rcount >= 2 && open_banned[j]) continue;
switch (t[j]) {
case '+':
dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] + dp_max[j + 1][i + k]);
dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] + dp_min[j+ 1][i + k]);
break;
case '-':
dp_max[i][i + k] = max(dp_max[i][i + k], dp_max[i][j] - dp_min[j + 1][i + k]);
dp_min[i][i + k] = min(dp_min[i][i + k], dp_min[i][j] - dp_max[j + 1][i + k]);
break;
default:
break;
}
}
}
}
/*
foreach (L; dp_max) log(L);
log();
foreach (L; dp_min) log(L);
*/
writeln(dp_max[0][t.size]);
}
|
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!(char[]);
writeln(S[0] == S[1] || S[1] == S[2] || S[2] == S[3] ? "Bad" : "Good");
}
|
D
|
void main()
{
int[] path = new int[4];
foreach (i; 0 .. 3)
{
int[] tmp = readln.split.to!(int[]);
int a = tmp[0] - 1, b = tmp[1] - 1;
++path[a], ++path[b];
}
writeln(path.all!"a < 3" ? "YES" : "NO");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.