code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s;
scan(s);
auto n = s.length.to!int;
if (s == "keyence") {
writeln("YES");
return;
}
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n + 1) {
string res;
foreach (k ; 0 .. n) {
if (i <= k && k < j) {
continue;
}
res ~= s[k];
}
if (res == "keyence") {
writeln("YES");
return;
}
}
}
writeln("NO");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
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;
}
long powmod(long x, long y, long mod = 1_000_000_007L) {
long res = 1L;
while (y > 0) {
if (y & 1) {
(res *= x) %= mod;
}
(x *= x) %= mod;
y >>= 1;
}
return res;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const N = readln.chomp.to!long;
const xs = 5.iota.map!(_ => readln.chomp.to!long).array;
writeln(4 + cast(long)ceil(cast(double)N / xs.reduce!min));
}
|
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);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
string s;
string input = sread();
foreach (c; input)
{
switch (c)
{
case '0':
s ~= '0';
break;
case '1':
s ~= '1';
break;
case 'B':
if (!s.empty)
s.popBack();
break;
default:
assert(0);
}
}
writeln(s);
}
|
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 st = readln.split.to!(char[][]);
char[] u;
foreach (i; 0..N) {
u ~= st[0][i];
u ~= st[1][i];
}
writeln(u);
}
|
D
|
import std.algorithm;
import std.array;
import std.stdio;
import std.conv;
import std.string;
import std.range;
void main(){
size_t w, h, sx, sy;
char[][] data = new char[][](20, 20);
while(true){
auto input = readln.split;
w = input[0].to!int;
h = input[1].to!int;
if(!(w | h)){
break;
}
foreach(i; 0..h){
foreach(j, c; readln.chomp){
data[i][j] = c;
if(c == '@'){
sx = j;
sy = i;
data[i][j] = '#';
}
}
}
data.dfs(sx, sy, w, h).writeln;
}
}
enum move = [[1, 0], [0, 1], [-1, 0], [0, -1]];
size_t dfs(char[][] data, size_t sx, size_t sy, size_t w, size_t h){
size_t count = 1;
foreach(i; 0..4){
size_t x = sx + move[i][0];
size_t y = sy + move[i][1];
if(0 <= x && x < w && 0 <= y && y < h && data[y][x] != '#'){
data[y][x] = '#';
count += dfs(data, x, y, w, h);
}
}
return 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;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
long right, left, ans;
while (1)
{
while (left < a.length && a[left] == 0)
left++;
if(left == a.length)
break;
right = left;
while (right < a.length && a[right] != 0)
right++;
auto tmp = a[left .. right];
auto mini = tmp.reduce!(min);
tmp[] -= mini;
ans += mini;
left = right = 0;
}
ans.writeln();
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long total;
for (long i = 1; i * i < n; ++i)
{
if (n % i == 0)
{
long d = n / i;
if (i == n % (d - 1)) total += d - 1;
}
}
total.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main(){
long x, t;
scanf("%ld %ld", &x, &t);
(x-t>0?x-t:0).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto s=readln().chomp().split().map!(to!int);
int a=s[0],b=s[1],c=s[2];
if(a+b>=c)
writeln("Yes");
else
writeln("No");
}
|
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()
{
readln;
auto a = readln.chomp.split.map!(to!int);
auto s = new int[](100003);
foreach (e; a) {
s[e]++;
s[e+3]--;
//s[e..e+3] +=1;
//writeln(s[0..10]);
}
int cnt, prevCnt;
foreach (i, e; s) {
cnt += e;
prevCnt = max(cnt, prevCnt);
}
prevCnt.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;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
readln;
auto xs = readints();
int n = cast(int) xs.length;
auto segTree = new SegmentTree(n + 1);
foreach (x; xs) {
auto max = segTree.query(1, x);
segTree.update(x, x + max);
}
auto max = segTree.query(1, n + 1);
writeln(cast(long) n * (n + 1) / 2 - max);
}
class SegmentTree {
private long[] _data;
this(int n) {
int len = 1;
while (len < n)
len *= 2;
_data = new long[len * 2];
}
/// k ?????????????´????(0-indexed)??? a ????????´
void update(int k, long a) {
// ????????\???
k += (_data.length / 2) - 1;
_data[k] = a;
// ?????????????????´??°
while (k > 0) {
k = (k - 1) / 2;
_data[k] = max(_data[k * 2 + 1], _data[k * 2 + 2]);
}
}
/// [a, b) ???????°????????±???????
long query(int a, int b, int k, int l, int r) {
// [a, b) ??¨ [l, r) ??????????????????
if (r <= a || b <= l)
return 0;
// [a, b) ??? [l, r) ????????¨???????????§????????°???????????\?????????
if (a <= l && r <= b) {
return _data[k];
}
// ????????§???????????°???2 ?????????????????§???
auto vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
auto vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return max(vl, vr);
}
long query(int a, int b) {
return query(a, b, 0, 0, cast(int)(_data.length / 2));
}
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int x = tmp[0], a = tmp[1], b = tmp[2];
writeln(abs(x-a) < abs(x-b) ? 'A' : 'B');
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.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.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto s = RD!string;
long cnt0, cnt1;
foreach (c; s)
{
if (c == '0')
++cnt0;
else
++cnt1;
}
ans[ti] = min(cnt0, cnt1) % 2 == 1;
}
foreach (e; ans)
{
writeln(e ? "DA" : "NET");
}
stdout.flush;
debug readln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto g=new int[][](n+1, 0);
auto can=new int[](n+1);
void f(int i){
if(g[i].length==0){can[i]=0; return;}
foreach(j; g[i]) f(j);
foreach(j; g[i])if(can[j]==0){
can[i]=can[j]=1; break;
}
can[i]=max(can[i], 0);
}
foreach(i; 1..n+1){
int p; rd(p);
g[p]~=i;
fill(can, -1);
f(0);
int sm=reduce!("a+max(0, b)")(0, can);
// assert(sm%2==0);
writeln(sm/2);
// writeln(can);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.algorithm;
import std.bigint;
import std.concurrency;
import std.container;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
immutable string YES = "Yes";
immutable string NO = "No";
auto solve(string S) {
foreach (i, c; S) {
if (i % 2 == 1 && !"LUD".canFind(c)) return NO;
if (i % 2 == 0 && !"RUD".canFind(c)) return NO;
}
return YES;
}
void main() {
auto input = stdin.byLine.map!split.joiner;
string S;
S = input.front.to!string;
input.popFront;
static if (is(ReturnType!solve == void))
solve(S);
else
solve(S).writeln;
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
import std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
const ulong MOD = 1_000_000_007;
void main(){
long n = read.to!long;
long m = read.to!long;
ulong ans;
if(n < m - 1 || n > m + 1) ans = 0;
else if(n == m) ans = 2 * f(n) * f(n) % MOD;
else ans = f(m) * f(n) % MOD;
ans.writeln;
}
ulong f(long n){
ulong res = 1;
for(long i = 1; i <= n; i ++){
res *= i;
res %= MOD;
}
return res;
}
|
D
|
void main(){
int ans = 6;
int[] ab;
ab ~= _scan();
ab ~= _scan();
foreach(elm; ab)ans -= elm;
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.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()
{
string s; readV(s);
auto u = s[0..2].to!int, v = s[2..4].to!int;
if (1 <= u && u <= 12 && 1 <= v && v <= 12)
writeln("AMBIGUOUS");
else if (1 <= u && u <= 12)
writeln("MMYY");
else if (1 <= v && v <= 12)
writeln("YYMM");
else
writeln("NA");
}
|
D
|
void main()
{
int[] tmp = readln.split.to!(int[]);
int h = tmp[0], w = tmp[1];
string[] s = new string[h];
coord[] black;
foreach (i; 0 .. h)
{
s[i] = readln.chomp;
foreach (j, c; s[i])
{
if (c == '#')
{
black ~= coord(i, j.to!int);
}
}
}
int[] dy = [-1, 0, 1, 0], dx = [0, 1, 0, -1];
bool ok = true;
foreach (b; black)
{
bool flag;
foreach (dir; 0 .. 4)
{
int y = b.y + dy[dir], x = b.x + dx[dir];
if (y < 0 || h <= y || x < 0 || w <= x) continue;
if (s[y][x] == '#') flag = true;
}
if (!flag)
{
ok = false;
break;
}
}
writeln(ok ? "Yes" : "No");
}
alias Tuple!(int, "y", int, "x") coord;
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.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), a = rd[0], b = rd[1];
writeln((a + b) % 24);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
void main() {
int h,w;
int[] hw = array(map!(a => to!int(a))(readln().strip().split()));
h = hw[0];
w = hw[1];
while((h|w) != 0) {
auto s = new string[](h);
foreach(i;0..h){
s[i] = readln().strip();
}
auto dp = new int[][](h+1,w+1);
foreach(i; 0..h) {
foreach(j; 0..w) {
if(s[i][j] == '.')
dp[i+1][j+1] = dp[i+1][j]+1;
else
dp[i+1][j+1] = 0;
}
}
int ans = 0;
foreach(i; 0..h) {
foreach(j; 0..w) {
int maxS = 0;
int minw = int.max;
foreach_reverse(k; 0..i+1) {
minw = min(minw, dp[k+1][j+1]);
if(minw == 0)
break;
maxS = max(maxS, (i-k+1)*minw);
}
ans = max(ans, maxS);
}
}
writeln(ans);
hw = array(map!(a => to!int(a))(readln().strip().split()));
h = hw[0];
w = hw[1];
}
}
|
D
|
import std.stdio, std.array, std.conv, std.string;
void main() {
string[] input = split(readln());
int W = to!int(input[0]), H = to!int(input[1]);
int x = to!int(input[2]), y = to!int(input[3]), r = to!int(input[4]);
if (r <= x && r <= (W - x) && r <= y && r <= (H - y)) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.numeric;
import std.math;
import std.string;
void main()
{
auto tokens = split(chomp(readln()));
int n = to!int(tokens[0]);
int m = to!int(tokens[1]);
int[][] nodes;
int[][] edges;
nodes.length = n;
foreach (i; 0..m)
{
auto tokens2 = split(chomp(readln()));
int a = to!int(tokens2[0]) - 1;
int b = to!int(tokens2[1]) - 1;
nodes[a] ~= b;
nodes[b] ~= a;
edges ~= [a, b];
}
bool search(int src, int dst, in int[] open, int startPos)
{
if (src == dst) return true;
if (src == startPos) return false;
foreach (next; nodes[src])
{
auto oi = countUntil(open, next);
if (oi == -1) continue;
auto r = search(next, dst, remove(open.dup, oi), startPos);
if (r) return true;
}
return false;
}
int result = m;
int[] open;
foreach (i; 0..n) open ~= i;
foreach (edge; edges)
{
foreach (next; nodes[edge[0]])
{
if (next == edge[1]) continue;
auto r = search(next, edge[1], open, edge[0]);
if (r)
{
--result;
break;
}
}
}
writeln(result);
stdout.flush();
}
|
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!long;
auto s = readln.chomp.to!long;
long b = 2;
for (; b^^2 <= n; ++b) {
auto m = n;
long t;
while (m) {
t += m % b;
m /= b;
}
if (t == s) {
writeln(b);
return;
}
}
auto x = n/(b-1);
for (; x > 0; --x) {
auto y = s - x;
if (y < 0) continue;
b = (n-y)/x;
if (n-y >= 0 && (n-y)%x == 0 && b > 1 && n%b + (n/b)%b == s) {
writeln(b);
return;
}
}
if (s == n) {
writeln(n+1);
return;
}
writeln(-1);
}
|
D
|
import std.stdio, std.string;
void main() {
string input = chomp(readln());
char lastChar = ' ';
byte count = 1;
foreach(char i; input) {
if(lastChar == ' ') {
lastChar = i;
continue;
}
if(lastChar == i) {
count++;
} else {
count = 1;
lastChar = i;
}
if(count >= 3) {
writeln("Yes");
return;
}
}
writeln("No");
return;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
long n = S.length;
auto T = sread();
{
auto cnt = new long[]('z' - 'a' + 1);
foreach (c; S)
cnt[c - 'a']++;
foreach (c; T)
if (cnt[c - 'a'] == 0)
{
writeln(-1);
return;
}
}
auto next = new long[][](n, 'z' - 'a' + 1);
auto SS = S ~ S;
next[$ - 1][] = 0;
next[$ - 1][S[0] - 'a'] = 1;
foreach_reverse (i; 0 .. n * 2)
{
next[i % n][] = next[(i + 1) % n][] + 1;
next[i % n][SS[i] - 'a'] = 0;
}
// foreach (i, r; next)
// writeln(i, " ", S[i], " ", r['t' - 'a']);
// writeln();
long cur = -1;
foreach (c; T)
{
cur++;
// writeln(cur, " ", c, " ", next[cur % n][c - 'a']);
cur += next[cur % n][c - 'a'];
}
writeln(cur + 1);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 t = RD!int;
auto ans = new long[](t);
foreach (i; 0..t)
{
auto n = RD!int;
auto a = RDA!uint;
bool[uint] set;
foreach (e; a)
{
if (e % 2 == 0)
set[e] = true;
}
foreach (key; set.keys)
{
while (key % 2 == 0)
{
++ans[i];
key >>= 1;
if (set.get(key, false))
break;
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }
void main() {
int n = readln.chomp.to!int;
auto as = readln.chomp.split(" ").map!(to!int).array;
int[int] count;
foreach (a; as) {
count[a] = count.get(a, 0) + 1;
}
auto cs = count.values();
int r = 0;
foreach (c; cs) {
r += max(0, c - 1);
}
int eat = (r + 1) / 2 * 2;
writeln(n - eat);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
const long mod = 1_000_000_007;
void solve(){
int n = scan!int;
long[] as = scan!long(n);
long sum;
foreach(a; as) sum += a, sum %= mod;
long sqsum;
foreach(a; as) sqsum += (a * a) % mod, sqsum %= mod;
long ans = (sum * sum) % mod;
ans += mod - sqsum, ans %= mod;
if(ans % 2) ans += mod;
ans /= 2;
ans.writeln;
}
|
D
|
void main() {
string n = readln.chomp;
while (n.count(n[0]) != 3) {
n = n.succ;
}
n.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import 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()
{
auto s = sread();
foreach (i, e; s)
{
if(i == 4)
write(" ");
e.write();
}
writeln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
string s = sread();
long a, z;
foreach (i; 0 .. s.length)
if (s[i] == 'A')
{
a = i;
break;
}
foreach_reverse (i; 0 .. s.length)
if (s[i] == 'Z')
{
z = i;
break;
}
writeln(z - a + 1);
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] p = readln.split.to!(int[]);
int cnt;
foreach (i, x; p) {
if (x != i + 1) ++cnt;
}
writeln(cnt < 3 ? "YES" : "NO");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
long r;
foreach (n; K+1..N+1) {
r += N/n * (n-K);
r += max(0, N - N/n * n - max(0, K-1));
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.string, std.range;
import std.container;
void main(string[] args) {
auto dlist = DList!int();
int n = readln.chomp.to!int;
foreach (i; 0..n) {
string op = readln.chomp;
if (op[0..7] == "insert ") {
dlist.insertFront(op[7..$].to!int);
} else if (op[0..7] == "delete ") {
auto range = dlist[];
for ( ; !range.empty; range.popFront()) {
if (range.front == op[7..$].to!int) {
dlist.stableLinearRemove(take(range, 1));
break;
}
}
} else if (op == "deleteFirst") {
dlist.removeFront;
} else if (op == "deleteLast") {
dlist.removeBack;
}
}
dlist[].map!(to!string).join(" ").writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container;
void main()
{
auto ai = new PriorityQueue!int(2_0000_000);
for (;;) {
auto rd = readln.chomp.splitter(' '), cmd = rd.front;
switch (cmd) {
case "insert":
rd.popFront;
ai.insert(rd.front.to!int);
break;
case "extract":
writeln(ai.remove);
break;
case "end":
return;
default:
assert(0);
}
}
}
class PriorityQueue(T)
{
T[] buf;
size_t t;
this(size_t capacity)
{
buf = new T[](capacity);
t = 1;
}
void insert(T val)
{
buf[t++] = val;
goUp(t-1);
}
T remove()
{
T val = buf[1];
buf[1] = buf[--t];
goDown(1);
return val;
}
void goUp(size_t i)
{
auto p = i / 2;
if (p > 0 && buf[p] < buf[i]) {
swap(buf[p], buf[i]);
goUp(p);
}
}
void goDown(size_t i)
{
auto l = i * 2, r = l + 1;
size_t largest;
if (l < t && buf[l] > buf[i])
largest = l;
else
largest = i;
if (r < t && buf[r] > buf[largest])
largest = r;
if (largest != i) {
swap(buf[i], buf[largest]);
goDown(largest);
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int r, g;
scan(r);
scan(g);
writeln(2*g - r);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto F=readln.split.to!(int[]),A=F[0],B=F[1],C=F[2];
if(A+B+C==17)writeln("YES");
else writeln("NO");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;
import std.datetime;
immutable long MOD = 10^^9 + 7;
int N, X, Y, Z, S, MASK, NG;
long[][] mem;
long dp(int n, int mask) {
if (n >= N) return 1;
if (mem[n][mask] >= 0) return mem[n][mask];
long ret = 0;
foreach (i; 1..11) {
//if (i == ng) continue;
int n_mask = (mask << i) | (1 << (i - 1));
if ((n_mask & NG) == NG) continue;
n_mask &= MASK;
ret = (ret + dp(n+1, n_mask)) % MOD;
}
return mem[n][mask] = ret;
}
void main() {
scanf("%d %d %d %d", &N, &X, &Y, &Z);
S = X + Y + Z;
mem = new long[][](N, 1<<S);
foreach (i; 0..N) fill(mem[i], -1);
MASK = (1 << S) - 1;
NG |= (1 << (Z-1));
NG |= (1 << (Y+Z-1));
NG |= (1 << (X+Y+Z-1));
long ans = 1;
foreach (i; 0..N) ans = (ans * 10) % MOD;
ans = ((ans - dp(0, 0)) % MOD + MOD) % MOD;
ans.writeln;
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc174/tasks/abc174_d
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
const int n = readln.chomp.to!int;
string s = readln.strip;
int reds = 0;
foreach(ch; s)
if(ch == 'R')
reds += 1;
int ans = 0;
for(int i = 0; i < reds; i++)
if(s[i] == 'W')
ans += 1;
ans.writeln;
}
|
D
|
void main() {
problem();
}
void problem() {
auto S = scan;
auto T = scan;
long solve() {
long ans;
foreach(i; 0..S.length) {
if (S[i] != T[i]) ans++;
}
return ans;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
string N, K;
scan(N, K);
auto D = aryread();
uint ng;
foreach (d; D)
ng = ng | (1 << d);
// writefln("%010b", ng);
// foreach (i; 0 .. 10)
// {
// writeln(!(ng & (1 << i)));
// }
auto dp = new long[][](2, N.length + 1);
dp[0][] = 10 ^^ 9;
dp[1][] = 10 ^^ 9;
dp[0][0] = 0;
foreach (i; 1 .. 10)
if (!(ng & (1 << i)))
{
dp[1][0] = i;
break;
}
foreach (i; 0 .. N.length)
{
if (!(ng & (1 << (N[i] - '0'))))
{
dp[0][i + 1] = dp[0][i + 1].min(dp[0][i] * 10 + (N[i] - '0'));
}
foreach (j; 0 .. 10)
if (!(ng & (1 << j)))
{
dp[1][i + 1] = dp[1][i + 1].min(dp[1][i] * 10 + j);
if (N[i] - '0' < j)
dp[1][i + 1] = dp[1][i + 1].min(dp[0][i] * 10 + j);
}
}
// writeln(dp[0]);
// writeln(dp[1]);
writeln(min(dp[0][$ - 1], dp[1][$ - 1]));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int x, a, b;
scan(x);
scan(a);
scan(b);
writeln((x-a)%b);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long N = lread();
long K = lread();
long n = N - K;
long X = lread();
long a = (n < 0) ? N * X : K * X;
long b = (n < 0) ? 0 : n * lread();
writeln(a + b);
}
|
D
|
void main()
{
import std.array, std.stdio, std.string, std.conv;
auto buf = readln.chomp.split.to!(int[]);
auto window = buf[0];
auto curtain = buf[1] << 1;
if (window > curtain)
(window - curtain).writeln;
else
0.writeln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long A, B, C, D, E, F; rd(A, B, C, D, E, F);
A*=100; B*=100;
long tot=A, su=0;
for(long a=0; a<=F; a+=A)for(long b=0; a+b<=F; b+=B){
for(long c=0; a+b+c<=F; c+=C){
if(c*100>(a+b)*E) continue;
for(long d=0; a+b+c+d<=F; d+=D){
if((c+d)*100>(a+b)*E) continue;
long wa_=a+b, su_=c+d, to_=wa_+su_;
if(su*to_<tot*su_){
tot=to_;
su=su_;
}
}
}
}
writeln(tot, " ", su);
}
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;
import std.algorithm;
import std.conv;
import std.numeric;
import std.math;
import std.string;
void main()
{
auto tokens = split(chomp(readln()));
auto N = to!ulong(tokens[0]);
auto M = to!ulong(tokens[1]);
ulong inc = 1;
if (M % 2 == 1)
{
if (N % 2 == 0) ++N;
inc = 2;
}
ulong blocks = M;
ulong i;
while (i < M/2 - N + 1)
{
if (M % (N + i) == 0)
{
blocks = N + i;
break;
}
i += inc;
}
writeln(M / blocks);
stdout.flush();
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto s = readln.strip;
auto t = readln.strip;
writeln( solve( s, t ) );
}
auto solve( in string s, in string t )
{
foreach( i; 0 .. t.length )
{
auto ts = t[ i .. $ ] ~ t[ 0 .. i ];
if( s == ts ) return "Yes";
}
return "No";
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "p", long, "dist");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
long[] paty;
long[][] bager;
void main()
{
long n, k;
scan(n, k);
paty ~= 1, bager ~= [1];
foreach (i; iota(1, n + 1))
{
paty ~= 2 * paty[i - 1] + 1;
bager ~= [1, bager[i - 1].sum(), 1, bager[i - 1].sum(), 1];
}
// paty.writeln();
// bager.writeln();
long ans;
solve(ans, n, k);
ans.writeln();
}
void solve(ref long ans, long depth, long res)
{
if (res == 0)
{
return;
}
else if (depth == 0)
{
ans += 1;
return;
}
foreach (i, e; bager[depth])
{
if (res >= e)
{
if(i != 0 && i != bager[depth].length - 1)
ans += min(e, paty[depth - 1]);
res -= e;
}
else
{
solve(ans, depth - 1, res);
return;
}
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto S = RD!string;
long ans;
if (S == "RSR")
ans = 1;
else
{
foreach (c; S)
{
if (c == 'R')
++ans;
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Pair = Tuple!(long, "number", long, "times");
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;
}
}
auto comb = new long[][](2019, 2019);
void calc_comb(long limit)
{
long times;
comb[times++][0] = 1;
comb[times][0] = comb[times][1] = 1;
while (++times < limit)
{
long row;
while (row <= times)
{
if (row == 0)
{
comb[times][row] = 1;
row++;
continue;
}
comb[times][row] = (comb[times - 1][row] + comb[times - 1][row - 1]) % bignum;
row++;
}
}
}
void main()
{
long n, blue;
scan(n, blue);
auto red = n - blue;
calc_comb(n + 1);
foreach (i; 1.. blue + 1)
{
// comb[red + 1][i].write(" ");
// comb[blue - 1][i - 1].writeln();
auto ans = comb[red + 1][i] * comb[blue - 1][i - 1];
ans %= bignum;
ans.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 main()
{
int a, b; readV(a, b);
writeln(a+b == 15 ? "+" : a*b == 15 ? "*" : "x");
}
|
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!long;
auto AS = readln.split.to!(long[]);
long sum_a;
foreach (a; AS) sum_a += a;
auto d = (N+1) * N / 2;
if (sum_a % d != 0) {
writeln("NO");
return;
}
auto n = sum_a / d;
d = 0;
foreach (i; 0..N) {
auto a = AS[(i+1)%N] - AS[i];
if ((n-a)%N != 0 || n-a < 0 || n < (n-a)/N) {
writeln("NO");
return;
}
d += a;
}
writeln(d == 0 ? "YES" : "NO");
}
|
D
|
void main() {
auto N = ri;
auto S = rs;
int res, tmp;
foreach(i; S) {
if(i == 'I') tmp++;
else tmp--;
res = max(res, tmp);
}
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
long n = readln.chomp.to!long;
long ans = 0;
foreach (i; 0 .. n + 1)
{
if (i * i > n)
{
break;
}
ans = i * i;
}
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; }
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+1);
a[0] = 2;
a[1] = 1;
foreach (i; 2..N+1)
{
a[i] = a[i-1] + a[i-2];
}
writeln(a[N]);
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
readln.splitter.map !(to !(int)).sum.writeln;
}
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
import core.bitop;
immutable int B = 26;
int main() {
Scanner sc = new Scanner(stdin);
int n;
sc.read(n);
int f = (1<<B) - 1;
int ans = 0;
foreach (i; 0..n-1) { //ignore last query
char ty; string s;
sc.read(ty, s);
bool uni = popcnt(f) == 1;
if (ty == '!') {
//shock
if (uni) ans++;
int nf = 0;
foreach (c; s) {
nf |= (1 << (c - 'a'));
}
f &= nf;
} else if (ty == '.') {
int nf = 0;
foreach (c; s) {
nf |= (1 << (c - 'a'));
}
f &= ~nf;
} else if (ty == '?') {
if (uni) ans++;
int nf = 0;
foreach (c; s) {
nf |= (1 << (c - 'a'));
}
f &= ~nf;
}
}
// writeln(f, " ", popcnt(f));
writeln(ans);
return 0;
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
int res = n - (n == 1);
foreach (i; 0..n)
{
foreach (j; i + 1..n)
{
auto num = a[j] - a[i];
auto den = j - i;
int cur = 0;
foreach (k; 0..n)
{
auto value = num * (k - i);
cur += (a[k] - a[i]) * den != value;
}
res = min (res, cur);
}
}
writeln (res);
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto A = 2.iota.map!(_ => readln.split.map!(to!long).array).array;
auto B = new long[][](2, N+1);
foreach (i; 0..2)
foreach (j; 0..N)
B[i][j+1] = B[i][j] + A[i][j];
long ans = 0;
long tmp = 0;
long a = 0;
long b = 0;
foreach (i; 0..N) a += i * A[0][i];
foreach (i; 0..N) a += (N+N-i-1) * A[1][i];
foreach (i; 1..N) b += (i + 1) * A[1][i];
foreach (i; 1..N) b += (N+N-i) * A[0][i];
foreach (i; 0..N) {
long t = i*2;
if (i % 2 == 0) {
ans = max(ans, tmp + a);
tmp += t * A[0][i];
tmp += (t + 1) * A[1][i];
if (i < N-2) {
a -= t * A[0][i];
a -= (t+1) * A[0][i+1];
a += (B[0][N] - B[0][i+2]) * 2;
a -= (2 * N - 1) * A[1][i];
a -= (2 * N - 2) * A[1][i+1];
a += (B[1][N] - B[1][i+2]) * 2;
}
} else {
ans = max(ans, tmp + b);
tmp += t * A[1][i];
tmp += (t + 1) * A[0][i];
if (i < N-2) {
b -= t * A[1][i];
b -= (t+1) * A[1][i+1];
b += (B[1][N] - B[1][i+2]) * 2;
b -= (2 * N - 1) * A[0][i];
b -= (2 * N - 2) * A[0][i+1];
b += (B[0][N] - B[0][i+2]) * 2;
}
}
if (i == N-1) ans = max(ans, tmp);
}
ans.writeln;
}
|
D
|
import std.stdio;
void main()
{
long n;
scanf("%lld", &n);
printf("%lld\n", n / (2 * 2 * 2* 3 * 3 * 5 * 7));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.bigint;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
auto k = int.max, x = int.max, n = N;
size_t i, j = N-1;
while (i <= j) {
--n;
x = min(x, AS[i]);
x = min(x, AS[j]);
k = min(k, x / n);
++i;
--j;
}
writeln(k);
}
|
D
|
import std.stdio : writeln, stdin;
import std.conv : to;
import std.range : dropOne;
import core.stdc.stdio;
void main()
{
int n;
scanf("%d", &n);
string ss;
int[100005] arr;
for(int i = 0 ;i < 100005; ++i){
arr[i] = 1;
}
foreach(word; stdin.byLine)
{
ss = to!string(word);
}
for (int i = 0; i < n; ++i) {
int curr = i;
if(!(ss[i] == 'a' || ss[i] =='e' || ss[i] =='i' || ss[i] =='o' || ss[i] =='u' || ss[i] =='y'))
{
continue;
}
while((i+1) < n && ss[i + 1] == ss[curr]){
++arr[curr];
arr[i+1] = 0;
++i;
}
}
for(int i = 0; i < n; ++i){
if(arr[i] == 0){
continue;
}
if(arr[i] ==2 && (ss[i] == 'e' || ss[i] == 'o'))
{
printf("%c%c", ss[i], ss[i]);
while((i + 1) < n && arr[i+1] == 0)
{
++i;
}
}
else if(arr[i] > 0)
{
printf("%c", ss[i]);
while((i + 1) < n && arr[i+1] == 0)
{
++i;
}
}
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1].to!long;
auto A = readln.split.map!(to!long).array;
auto B = new long[](N+1);
foreach (i; 0..N) B[i+1] = (B[i] + A[i]) % K;
long ans = 0;
long[long] cnt;
auto C = new long[](N+1);
foreach (i; 0..N+1) {
long z = ((B[i] - i) % K + K) % K;
if (i >= K) {
cnt[C[i - K.to!int]] -= 1;
}
if (z in cnt) ans += cnt[z];
cnt[z] += 1;
C[i] = z;
}
ans.writeln;
}
|
D
|
import std;
long calc(int n) {
long sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 5 == 0) continue;
sum += i;
}
return sum;
}
void main() {
int n; scan(n);
writeln(calc(n));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
const real eps = 1e-10;
const long p = 1_000_000_000 + 7;
void main(){
long n, m, k;
readlnTo(n, m, k);
foreach(i; iota(n+1)) {
foreach(j; iota(m+1)) {
if(i * m + j * n - 2 * i * j == k) {
writeln("Yes");
return;
}
}
}
writeln("No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto as = new long[](10^^5+1);
long S;
foreach (a; readln.split.to!(long[])) {
S += a;
++as[a];
}
auto Q = readln.chomp.to!int;
foreach (_; 0..Q) {
auto bc = readln.split.to!(int[]);
auto B = bc[0];
auto C = bc[1];
S -= as[B] * B;
S += as[B] * C;
writeln(S);
as[C] += as[B];
as[B] = 0;
}
}
|
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 long mod = 10^^9 + 7;
void main() {
string s = readln.chomp;
string abc = "ABC";
int n = s.length.to!int;
auto dp = new long[][](n + 1, 4);
dp[0][0] = 1;
foreach (i ; 1 .. n + 1) {
foreach (j ; 0 .. 4) {
dp[i][j] = dp[i-1][j];
if (s[i-1] == '?') {
dp[i][j] *= 3;
dp[i][j] %= mod;
}
if (j > 0 && (s[i-1] == '?' || s[i-1] == abc[j-1])) {
dp[i][j] += dp[i-1][j-1];
dp[i][j] %= mod;
}
}
}
debug {
writeln(dp);
}
writeln(dp[n][3]);
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto nx = readln.split.to!(int[]);
auto N = nx[0];
long x = nx[1];
auto AS = readln.split.to!(long[]);
auto min_as = new long[](N);
min_as[] = long.max;
auto res = long.max;
foreach (i; 0..N) {
long rr = x * i;
foreach (j; 0..N) {
min_as[j] = min(min_as[j], AS[(i+j)%N]);
rr += min_as[j];
}
res = min(res, rr);
}
writeln(res);
}
|
D
|
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto sn=readln().split();
int n=to!int(sn[0]);
if(n<1000)
writeln("ABC");
else
writeln("ABD");
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.container.rbtree;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
string s;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
s = readln().strip();
}
}
auto main2(Input* input)
{
with (input)
{
if (s == "AAA" || s == "BBB")
return "No";
else
return "Yes";
}
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`ABA`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == "Yes");
}
unittest // example2
{
string example =
`BBA`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == "Yes");
}
unittest // example3
{
string example =
`BBB`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == "No");
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const H = readLong();
const W = readLong();
long ans;
if (H == 1) {
ans = 1;
} else if (W == 1) {
ans = 1;
} else {
ans = (H * W + 1) / 2;
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.numeric;
void main() {
int n, m;
scan(n, m);
auto adj = new int[][](m, 0);
auto ex = new bool[](m);
foreach (i ; 0 .. n) {
auto line = readln.split.to!(int[]);
int u = line[1] - 1;
ex[u] = 1;
if (line.length < 3) continue;
foreach (v ; line[2 .. $]) {
v--;
ex[v] = 1;
adj[u] ~= v;
adj[v] ~= u;
}
}
debug {
writeln(adj);
}
auto visited = new bool[](m);
void dfs(int u) {
visited[u] = 1;
foreach (v ; adj[u]) {
if (!visited[v]) {
dfs(v);
}
}
}
foreach (i ; 0 .. m) {
if (ex[i]) {
dfs(i);
break;
}
}
foreach (i ; 0 .. m) {
if (ex[i] && !visited[i]) {
writeln("NO");
return;
}
}
writeln("YES");
}
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.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 I = readln.split.to!(long[]);
auto N = I[0];
auto R = I[1];
long solve() {
return N >= 10 ? R : R + 100 * (10 - N);
}
solve().writeln;
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.array;
// import dcomp.ldc.inline;
int[] primeList(int n) {
bool[] used = new bool[n+1];
FastAppender!(int[]) ans;
foreach (i; 2..n+1) {
if (used[i]) continue;
ans ~= i;
foreach (j; iota(i, n+1, i)) {
used[j] = true;
}
}
return ans.data;
}
int[long] fastFactor(ulong x, in BarrettULong[] primes) {
int[long] mp;
foreach (p; primes) {
while (x%p == 0) {
mp[p.v]++;
x=x/p;
}
}
if (x) mp[x]++;
return mp;
}
long mul(long x, long y) {
if (10L^^12 / x < y) return 10L^^12;
return x*y;
}
int main() {
auto sc = new Scanner(stdin);
auto pr = primeList(100_100);
auto bpr = pr.map!(x => BarrettULong(x)).array;
int n;
sc.read(n);
int[2][long] g;
bool one = false;
foreach (i; 0..n) {
long s;
sc.read(s);
// writeln(fastFactor(s, bpr));
long d0 = 1, d1 = 1;
foreach (p, c; fastFactor(s, bpr)) {
c %= 3;
if (c == 1) {
d0 = mul(d0, p);
d1 = mul(d1, p);
d1 = mul(d1, p);
} else if (c == 2) {
d0 = mul(d0, p);
d0 = mul(d0, p);
d1 = mul(d1, p);
}
}
if (d0 == 1) {
one = true;
continue;
}
if (d0 < d1) {
if (d0 in g) {
g[d0][0]++;
} else {
g[d0] = [1, 0];
}
} else {
if (d1 in g) {
g[d1][1]++;
} else {
g[d1] = [0, 1];
}
}
}
int ans = 0;
if (one) ans++;
foreach (u; g) {
ans += max(u[0], u[1]);
}
writeln(ans);
return 0;
}
struct BarrettULong {
ulong v, u;
this(ulong x) {
this.v = x;
if (x == 1) return;
version(LDC) {
u = inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = add i128 %r0, 18446744073709551615
%r2 = udiv i128 %r1, %r0
%r3 = trunc i128 %r2 to i64
ret i64 %r3`, ulong)(x);
} else {
asm {
mov RBX, this;
mov RDX, 1;
mov RAX, x;
dec RAX;
div x;
mov u[RBX], RAX;
}
}
}
ulong opBinaryRight(string op:"/")(ulong x) const {
assert(v != 0);
if (v == 1) return x;
ulong r;
version(LDC) {
r = inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = mul i128 %r1, %r0
%r3 = lshr i128 %r2, 64
%r4 = trunc i128 %r3 to i64
ret i64 %r4`, ulong)(u, x);
} else {
asm {
mov RBX, this;
mov RAX, u[RBX];
mul x;
mov r, RDX;
}
}
return r;
}
ulong opBinaryRight(string op:"%")(ulong x) const {
return x - x/this*v;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, int N)(T[N] a) {return a;}
//this is not reference type!(please attention to copy)
struct FastAppender(A) {
import std.algorithm : max;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private size_t len, cap;
@property size_t length() {return len;}
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(4, cap*2));
}
_data[len++] = item;
}
void clear() {
len = 0;
}
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
unittest {
import std.stdio, std.algorithm;
auto u = FastAppender!(int[])();
u ~= 4; u ~= 5;
assert(equal(u.data, [4, 5]));
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */
// module dcomp.ldc.inline;
version(LDC) {
pragma(LDC_inline_ir)
R inlineIR(string s, R, P...)(P);
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
int[11] NS = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6];
void main()
{
auto N = readln.chomp.to!int;
int[int][11] memo;
int solve(int i, int n) {
if (n == 0) return 0;
if (i == 11) return n;
if (n in memo[i]) return memo[i][n];
if (n < NS[i]) {
return memo[i][n] = solve(i+1, n);
} else {
return memo[i][n] = min(solve(i+1, n), solve(i, n - NS[i]) + 1);
}
}
writeln(solve(0, N));
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
// ------ ここまでテンプレ ------ //
long a = read.to!long;
long b = read.to!long;
long c = read.to!long;
long ans = b / a;
if(ans > c) ans = c;
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, M;
scan(N, M);
auto adj = new int[][](N, 0);
foreach (i ; 0 .. M) {
int xi, yi;
scan(xi, yi);
xi--, yi--;
adj[yi] ~= xi;
}
auto dp = new int[](N);
dp[] = -1;
int rec(int v) {
if (dp[v] != -1) {
return dp[v];
}
dp[v] = 0;
foreach (u ; adj[v]) {
chmax(dp[v], rec(u) + 1);
}
return dp[v];
}
int ans;
foreach (i ; 0 .. N) {
chmax(ans, rec(i));
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
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
|
// This file is a "Hello, world!" in D language by DMD for wandbox.
import std.algorithm, std.conv, std.stdio, std.array, std.math, std.string;
void main()
{
const N = readln.chomp.to!long;
const V = readln.split.map!(to!long).array;
const C = readln.split.map!(to!long).array;
long ans;
foreach (b; 0..2.pow(N)) {
long X, Y;
foreach (n; 0..N) {
if (!(b >> n & 1)) continue;
X += V[n];
Y += C[n];
}
const temp = X - Y;
if (ans < temp) ans = temp;
}
ans.writeln;
}
// DMD reference:
// https://dlang.org/dmd-linux.html
// D language references:
// https://dlang.org
// http://www.kmonos.net/alang/d/ ( Japanese )
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(long[]), n = rd[0], ts = rd[1];
auto t = readln.split.to!(long[]);
auto ans = 0L;
foreach (i; 0..n-1)
ans += t[i+1]-t[i] > ts ? ts : t[i+1]-t[i];
ans += ts;
writeln(ans);
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, d;
rd(n, d);
struct P {
int x, y;
}
P[] vec = [P(d, -d), P(n - d, n - d), P(-d, d), P(d - n, d - n)];
P[] ps = [P(0, d), P(d, 0), P(n, n - d), P(n - d, n)];
int m;
rd(m);
while (m--) {
int x, y;
rd(x, y);
int[] o;
foreach (i, p; ps) {
auto u = P(x - p.x, y - p.y);
o ~= vec[i].x * u.y - vec[i].y * u.x;
}
auto inside = reduce!((r, e) => (r && e >= 0))(true, o);
if (inside) {
writeln("YES");
} else {
writeln("NO");
}
}
}
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;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == 0) break;
foreach (i; 0..x[0]) {
foreach (j; 0..x[1]) {
if (i == 0 || j == 0 || i == x[0] - 1 || j == x[1] - 1) write("#");
else write(".");
}
writeln("");
}
writeln("");
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Node = Tuple!(long, "p", long, "dist");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto TREE = new Node[][](n, 0);
auto color = new long[](n);
foreach (_; iota(n - 1))
{
long u, v, w;
scan(u, v, w);
TREE[u - 1] ~= Node(v - 1, w);
TREE[v - 1] ~= Node(u - 1, w);
}
// TREE.each!(x => x.writeln());
color[0] = 1;
dfs(TREE, color, 0, 0);
foreach (e; color)
writeln(e - 1);
}
void dfs(T)(T tree, long[] color, long current, long dist)
{
foreach (node; tree[current])
{
if (!color[node.p])
{
if ((dist + node.dist) % 2)
color[node.p] = 2;
else
color[node.p] = 1;
dfs(tree, color, node.p, dist + node.dist);
}
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio;
void main() {
string land = stdin.readln();
size_t position = 1;
foreach (char command; stdin.readln()) {
if (command == land[position - 1]) {
++position;
}
}
stdout.write(position);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto nml = readln.split.to!(int[]);
auto N = nml[0];
auto M = nml[1];
long L = nml[2];
auto G = new long[][](N, N);
foreach (ref g; G) g[] = long.max/3;
foreach (_; 0..M) {
auto abc = readln.split.to!(int[]);
auto A = abc[0]-1;
auto B = abc[1]-1;
long C = abc[2];
G[A][B] = C;
G[B][A] = C;
}
foreach (k; 0..N) foreach (i; 0..N) foreach (j; 0..N) {
if (G[i][j] > G[i][k] + G[k][j]) G[i][j] = G[i][k] + G[k][j];
}
auto H = new int[][](N, N);
foreach (ref h; H) h[] = int.max/3;
foreach (i; 0..N) foreach (j; 0..N) if (G[i][j] <= L) H[i][j] = 1;
foreach (k; 0..N) foreach (i; 0..N) foreach (j; 0..N) {
if (H[i][j] > H[i][k] + H[k][j]) H[i][j] = H[i][k] + H[k][j];
}
foreach (i; 0..N) foreach (j; 0..N) if (H[i][j] == int.max/3) H[i][j] = 0;
auto Q = readln.chomp.to!int;
foreach (_; 0..Q) {
auto st = readln.split.to!(int[]);
auto s = st[0]-1;
auto t = st[1]-1;
writeln(H[s][t]-1);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int count2(int n) {
int ret = 0;
while (n > 0 && n % 2 == 0) {
n /= 2;
ret++;
}
return ret;
}
void main() {
readint;
auto a = readints;
auto b = a.map!count2;
int ans = b.reduce!((a, b) => min(a, b));
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
while (1) {
auto str = readln.chomp;
if (str == "#") break;
int i = str.length.to!int-1;
long a, b = 1;
while (i > 0) {
a *= 2;
b *= 2;
if (str[i] == 'h') {
a -= 90;
a = max(0, a);
i -= 5;
} else {
a += 90;
a = min(90*b/2, a);
i -= 4;
}
}
if (b != 2) {
b /= 4;
a /= 2;
} else {
b /= 2;
}
if (b == 1) a.writeln;
else writeln(a, "/", b);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum mod = 10L^^9 + 7;
alias Pair = Tuple!(long, "s", long, "x");
void main() {
long n;
scan(n);
long[Pair] mem;
long func(long s, long x) {
if (Pair(s, x) in mem) {
return mem[Pair(s, x)];
}
if (s == 0) {
return 1;
}
if (x == 0) {
return s/2 + 1;
}
mem[Pair(s, x)] = (func(s/2, x/2) + func((s-1)/2, (x-1)/2)) % mod;
if (s >= 2) {
mem[Pair(s, x)] += func((s-2)/2, x/2);
}
mem[Pair(s, x)] %= mod;
return mem[Pair(s, x)];
}
writeln(func(n, n));
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(string, "s", long, "p");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = aryread();
auto oddary = new long[](n);
foreach (i; iota(n))
oddary[i] = (a[i] % 2 ? 1 : 2);
long rm = 1;
foreach (e; oddary)
rm *= e;
writeln(3 ^^ n - rm);
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm: canFind;
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(C >= A && C <= B) "Yes".writeln;
else "No".writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import std.math;
void main(){
auto z=readln.split.to!(int[]),a=z[0],b=z[1],c=z[2];
auto k=readln.chomp.to!int;
if(a>=b&&a>=c)writeln(b+c+a*(2^^k));
else if(b>=a&&b>=c)writeln(a+c+b*(2^^k));
else if(c>=a&&c>=b)writeln(a+b+c*(2^^k));
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
while(true){
string s = readln;
if (stdin.eof()) break;
int n = s.chomp.to!int;
int c = 0;
foreach(int i; 0..10)
foreach(int j; 0..10)
foreach(int k; 0..10)
foreach(int l; 0..10)
if (i + j + k + l == n){
c += 1;
}
writeln(c);
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main() {
string s = readln.strip;
long sm = 0, cnt = 0;
foreach(i,c;s) if(c == 'W')
sm += i - cnt++;
writeln(sm);
}
|
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 b, int c) {
if (a % 2 == 1 || b % 2 == 1 || c % 2 == 1) return 0;
if (a == b && b == c) return -1;
return calc((b + c) / 2, (a + c) / 2, (a + b) / 2) + 1;
}
void main() {
auto abc = readints;
int a = abc[0], b = abc[1], c = abc[2];
writeln(calc(a, b, c));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto S = RD;
auto W = RD;
writeln(W >= S ? "unsafe" : "safe");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
alias E = Tuple!(int, "from", int, "to", long, "c");
void main()
{
auto nmp = readln.split.to!(int[]);
auto N = nmp[0];
auto M = nmp[1];
long P = nmp[2];
E[] es;
foreach (_; 0..M) {
auto abc = readln.split.to!(int[]);
auto A = abc[0]-1;
auto B = abc[1]-1;
long C = abc[2];
es ~= E(A, B, C - P);
}
auto DP = new long[](N);
DP[] = long.min/3;
DP[0] = 0;
foreach (_; 0..N) {
foreach (e; es) {
if (DP[e.to] < DP[e.from] + e.c) {
DP[e.to] = DP[e.from] + e.c;
}
}
}
auto x = DP[N-1];
foreach (_; 0..N) {
foreach (e; es) {
if (DP[e.from] > long.min/4 && DP[e.to] < DP[e.from] + e.c) {
DP[e.to] = long.max/3;
}
}
}
writeln(DP[N-1] > x ? -1 : max(0, x));
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
int N = readln.chomp.to!int;
int[] A = readln.split.to!(int[]);
int[] B = new int[N];
B[0] = A.sum - 2 * iota(1, N-1, 2).map!(i => A[i]).sum;
foreach(i; 1..N) {
B[i] = 2*A[i-1] - B[i-1];
}
foreach(b; B) {
write(b, " ");
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Pair = Tuple!(long, "number", long, "times");
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()
{
auto s = sread();
sort(cast(ubyte[])s);
if(s[0] == s[1] && s[2] == s[3] && s[1] && s[1] != s[2])
writeln("Yes");
else
writeln("No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum CF = "CODEFESTIVAL2016";
void main()
{
auto S = readln.chomp;
int r;
foreach (i, c; S) if (c != CF[i]) ++r;
writeln(r);
}
|
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; long c; readV(n, c);
long[] x; int[] v; readC(n, x, v);
auto c1 = new long[](n);
c1[n-1] = v[n-1] - (c-x[n-1]);
foreach_reverse (i; 0..n-1)
c1[i] = c1[i+1] + v[i] - (x[i+1]-x[i]);
auto mc1 = new long[](n);
mc1[n-1] = c1[n-1];
foreach_reverse (i; 0..n-1)
mc1[i] = max(c1[i], mc1[i+1]);
auto c2 = new long[](n);
c2[0] = v[0] - x[0];
foreach (i; 1..n)
c2[i] = c2[i-1] + v[i] - (x[i]-x[i-1]);
auto mc2 = new long[](n);
mc2[0] = c2[0];
foreach (i; 1..n)
mc2[i] = max(c2[i], mc2[i-1]);
auto ans = max(0L, mc1.reduce!max, mc2.reduce!max);
foreach (i; 0..n-1) {
ans = max(ans, c2[i] - x[i] + mc1[i+1]);
ans = max(ans, c1[n-i-1] - (c-x[n-i-1]) + mc2[n-i-2]);
}
writeln(ans);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.