code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.array, std.math, std.typecons, std.bigint, std.conv;
void main()
{
int k;
scanf("%d", &k);
long res1 = 2UL^^k - 2;
BigInt n = 4;
BigInt res2 = powmod(n, to!BigInt(res1), to!BigInt(10^^9 + 7));
writeln((res2 * to!BigInt(6)) % (10^^9 + 7));
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) sc.read!true;
int n;
sc.read(n);
int[][] g = new int[][](n);
foreach (i; 1..n) {
int p;
sc.read(p); p--;
g[p] ~= i;
}
bool ans = true;
int dfs(int p) {
if (g[p].length == 0) return 1;
int u = 0;
foreach (d; g[p]) {
u += dfs(d);
}
if (u < 3) ans = false;
return 0;
}
dfs(0);
if (ans) writeln("Yes");
else writeln("No");
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(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
|
module sigod.codeforces.p287A;
import std.stdio;
import std.string;
void main()
{
bool[4][4] square = read();
bool answer = solve(square);
stdout.writeln(answer ? "YES" : "NO");
}
private
bool[4][4] read()
{
bool[4][4] square;
foreach (ref line; square) {
auto tmp = stdin.readln().strip();
foreach (i; 0 .. 4) {
line[i] = tmp[i] == '.';
}
}
return square;
}
bool solve(bool[4][4] square)
{
foreach (i, line; square[0 .. $ - 1]) {
foreach (j, e; line[0 .. $ - 1]) {
auto count = e + line[j + 1]
+ square[i + 1][j] + square[i + 1][j + 1];
if (count != 2) return true;
}
}
return false;
}
unittest {
bool[4][4] t1 = [
[false, false, false, false],
[true, false, true, true],
[false, false, false, false],
[true, true, true, true]
];
assert(solve(t1) == true);
bool[4][4] t2 = [
[false, false, false, false],
[true, true, true, true],
[false, false, false, false],
[true, true, true, true]
];
assert(solve(t2) == false);
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
}
long solve (long[] a) {
long curs = 0, maxs = 0;
foreach (x; a) {
curs += x;
if (curs < 0) {
curs = 0;
}
maxs = max (maxs, curs);
}
return maxs;
}
void main() {
auto r = new InputReader;
immutable n = r.next!int;
immutable x = r.next!int;
auto a = new long[n];
foreach (i; 0 .. n) {
a[i] = r.next!long;
}
long[3] curs;
long maxs = 0;
foreach (i; 0 .. n) {
foreach_reverse (state; 0 .. 3) {
long y = a[i];
if (state == 1) {
y *= x;
}
long s = long.min;
foreach (oldstate; max (0, state - 1) .. state + 1) {
s = max (s, curs[oldstate]);
}
s += y;
if (s < 0) {
s = 0;
}
curs[state] = s;
maxs = max (maxs, s);
}
}
writeln (maxs);
}
|
D
|
import std.stdio;
import std.ascii;
import core.stdc.stdio;
import std.algorithm;
int main()
{
int n = readInt!int;
auto a = new long[](n);
foreach(ref ai; a) ai = readInt!long;
int pos = cast(int) a.count!(ai => ai >= 0);
long[][] maxSum = new long[][](n + 1, n + 1);
maxSum[0][0] = (a[0] < 0)? 0 : a[0];
foreach(i; 1 .. n)
{
maxSum[i][0] = (a[i] < 0)? maxSum[i-1][0] : (maxSum[i-1][0] + a[i]);
}
foreach(p; 1 .. n)
{
foreach(i; 0 .. n)
{
if (a[i] < 0)
{
if (i == 0)
{
maxSum[i][p] = -1;
}
else
{
maxSum[i][p] = max(maxSum[i-1][p], maxSum[i-1][p-1] + a[i]);
}
}
else
{
if (i == 0)
{
maxSum[i][p] = -1;
}
else
{
maxSum[i][p] = (maxSum[i-1][p] >= 0)? (maxSum[i-1][p] + a[i]) : maxSum[i-1][p];
}
}
}
}
int p = 0;
while (p + 1 < n && maxSum[n-1][p + 1] >= 0)
{
p++;
}
writeln(pos + p);
return 0;
}
/* INPUT ROUTINES */
int currChar;
static this()
{
currChar = getchar();
}
char topChar()
{
return cast(char) currChar;
}
void popChar()
{
currChar = getchar();
}
auto readInt(T)()
{
T num = 0;
int sgn = 0;
while (topChar.isWhite) popChar;
while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
if (sgn) return -num; return num;
}
string readString()
{
string res = "";
while (topChar.isWhite) popChar;
while (!topChar.isWhite) { res ~= topChar; popChar; }
return res;
}
|
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); }
T binarySearch(alias pred, T)(T ok, T ng)
{
while (abs(ok-ng) > 1)
{
auto mid = (ok+ng)/2;
if (unaryFun!pred(mid)) ok = mid;
else ng = mid;
}
return ok;
}
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto x = RD;
bool f(long y)
{
return y * (y+1) / 2 < x;
}
auto r = binarySearch!(f)(0, x) + 1;
if (r * (r+1) / 2 == x+1)
{
ans[ti] = r+1;
}
else
{
ans[ti] = r;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 int[](t);
foreach (ti; 0..t)
{
auto r = RD!int;
auto c = RD!int;
auto s = new string[](r);
foreach (y; 0..r)
s[y] = RD!string;
bool ok1, ok2;
{
ok1 = true;
foreach (y; 0..r)
{
foreach (x; 0..c)
{
if (s[y][x] == 'P')
{
ok1 = false;
}
else
{
ok2 = true;
}
}
}
if (ok1)
{
ans[ti] = 0;
continue;
}
}
{
bool tmp1 = true;
bool tmp2 = true;
foreach (x; 0..c)
{
if (s[0][x] == 'P')
tmp1 = false;
if (s[$-1][x] == 'P')
tmp2 = false;
}
bool tmp3 = true;
bool tmp4 = true;
foreach (y; 0..r)
{
if (s[y][0] == 'P')
tmp3 = false;
if (s[y][$-1] == 'P')
tmp4 = false;
}
if (tmp1 || tmp2 || tmp3 || tmp4)
{
ans[ti] = 1;
continue;
}
}
{
if (s[0][0] == 'A' || s[r-1][0] == 'A' || s[0][c-1] == 'A' || s[r-1][c-1] == 'A')
{
ans[ti] = 2;
continue;
}
bool ok;
foreach (y; 0..r)
{
bool tmp = true;
foreach (x; 0..c)
{
if (s[y][x] == 'P')
{
tmp = false;
break;
}
}
if (tmp)
{
ok = true;
break;
}
}
if (ok)
{
ans[ti] = 2;
continue;
}
foreach (x; 0..c)
{
bool tmp = true;
foreach (y; 0..r)
{
if (s[y][x] == 'P')
{
tmp = false;
break;
}
}
if (tmp)
{
ok = true;
break;
}
}
if (ok)
{
ans[ti] = 2;
continue;
}
}
{
bool tmp1, tmp2;
foreach (x; 0..c)
{
if (s[0][x] == 'A')
tmp1 = true;
if (s[$-1][x] == 'A')
tmp2 = true;
}
bool tmp3, tmp4;
foreach (y; 0..r)
{
if (s[y][0] == 'A')
tmp3 = true;
if (s[y][$-1] == 'A')
tmp4 = true;
}
if (tmp1 || tmp2 || tmp3 || tmp4)
{
ans[ti] = 3;
continue;
}
}
if (ok2)
{
ans[ti] = 4;
}
else
{
ans[ti] = -1;
}
}
foreach (e; ans)
{
if (e == -1)
writeln("MORTAL");
else
writeln(e);
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.conv;
import std.array;
import std.stdio;
import std.string;
void main() {
string[][int] blocks;
readln; // to skip n
loop:
for (;;) {
auto command = readln.strip.split;
auto operation = command[0];
final switch (operation) {
case "push":
auto dst = command[1].to!int - 1;
auto block = command[2];
blocks[dst] ~= block;
break;
case "pop":
auto src = command[1].to!int - 1;
auto block = blocks[src][$-1];
blocks[src] = blocks[src][0 .. $-1];
writeln(block);
break;
case "move":
auto src = command[1].to!int - 1;
auto dst = command[2].to!int - 1;
auto block = blocks[src][$-1];
blocks[src] = blocks[src][0 .. $-1];
blocks[dst] ~= block;
break;
case "quit":
break loop;
}
}
}
|
D
|
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
long solve (int [] a, int pos)
{
auto n = a.length.to !(int);
auto half = (n + 1 - pos) / 2;
auto total = a.sum;
if (total != half)
{
return long.max;
}
long res = 0;
foreach (int i, ref c; a)
{
if (c & 1)
{
res += abs (i - pos);
pos += 2;
}
}
return res;
}
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;
a[] %= 2;
auto res = long.max;
res = min (res, solve (a, 0));
res = min (res, solve (a, 1));
if (res == long.max)
{
res = -1;
}
writeln (res);
}
}
|
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 r;
scan(r);
auto ans = 3 * r * r;
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
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto X = RD;
auto A = RD;
writeln(X < A ? 0 : 10);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.algorithm;
void main() {
auto a = readln.chomp.split(" ").map!(to!int).array;
foreach (size_t i, e;a) {
if (e == 0) {
writeln(i + 1);
break;
}
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.array;
int n, m; rd(n, m);
auto a=readln.split.to!(long[]).map!((e)=>(e%m)).array;
auto cul=new long[](n+1);
foreach(i; 0..n) cul[i+1]=(cul[i]+a[i])%m;
long[long] freq;
freq[0]=0;
long tot=0;
foreach(x; cul[1..$]){
if(x==0){
tot+=(++freq[x]);
continue;
}
if(x in freq){
tot+=freq[x]++;
}else{
freq[x]=1;
}
}
writeln(tot);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
char x, y;
scan(x, y);
char ans;
if (x < y) {
ans = '<';
}
else if (x > y) {
ans = '>';
}
else {
ans = '=';
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
string my_readln() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return (x - y) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto tokens = my_readln().split();
auto N = tokens[0].to!uint;
auto K = tokens[1].to!uint;
auto a = my_readln.split.to!(long[]);
auto dp = new long[][](105, 100005);
dp[0][0] = 1;
foreach (i; 0..N)
{
dp[i+1][0] = moda(dp[i+1][0], dp[i][0]);
foreach (j; 1..K+1)
{
dp[i+1][j] = moda(dp[i+1][j], dp[i][j]);
dp[i+1][j] = moda(dp[i+1][j], dp[i+1][j-1]);
if (j > a[i])
dp[i+1][j] = mods(dp[i+1][j] + mod, dp[i][j-a[i]-1] % mod);
}
}
writeln(dp[N][K]);
stdout.flush();
}
|
D
|
void main() {
string[dchar] s;
s['a'] = readln.chomp;
s['b'] = readln.chomp;
s['c'] = readln.chomp;
dchar now = 'a';
dchar ans;
while (true) {
dchar tmp = s[now][0];
s[now] = s[now][1..$];
now = tmp;
if (s[now].length == 0) {
ans = tmp;
break;
}
}
ans.toUpper.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.uni;
|
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!long;
long ans = 0;
foreach (i; 2..N) ans += i * (i + 1);
ans.writeln;
}
|
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 r = long.max;
foreach (x; 0..N+1) {
long rr;
auto a = x;
while (a) rr += a%6, a /= 6;
auto b = N-x;
while (b) rr += b%9, b /= 9;
r = min(r, rr);
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv;
void main()
{
foreach(n; stdin.byLine().map!(to!int))
{
bool flag;
foreach(i; 0..10)
{
if(n & 1)
{
if(flag) " ".write;
else flag = true;
(2 ^^ i).write;
}
n >>>= 1;
}
writeln;
}
}
|
D
|
/+ dub.sdl:
name "F"
dependency "dcomp" version=">=0.7.3"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
import std.typecons;
alias E = Tuple!(int, "to", int, "dist");
alias D = int;
int main() {
Scanner sc = new Scanner(stdin);
static struct PairingHeapAllAdd {
alias NP = Node*;
static struct Node {
E e;
D offset;
NP head, next;
this(E e) {
this.e = e;
offset = D(0);
}
}
NP n;
size_t length;
this(E[] e) {
length = e.length;
foreach (d; e) {
n = merge(n, new Node(d));
}
}
static NP merge(NP x, NP y) {
if (!x) return y;
if (!y) return x;
if (x.e.dist+x.offset < y.e.dist+y.offset) swap(x, y);
y.offset -= x.offset;
y.next = x.head;
x.head = y;
return x;
}
void C() { assert(n); }
E front() {C; return n.e; }
void removeFront() {
assert(n);
assert(length > 0);
length--;
NP x;
NP s = n.head;
while (s) {
NP a, b;
a = s; s = s.next; a.next = null; a.offset += n.offset;
if (s) {
b = s; s = s.next; b.next = null; b.offset += n.offset;
}
a = merge(a, b);
assert(a);
if (!x) x = a;
else {
a.next = x.next;
x.next = a;
}
}
n = null;
while (x) {
NP a = x; x = x.next;
n = merge(a, n);
}
}
void meld(PairingHeapAllAdd r) {
length += r.length;
n = merge(n, r.n);
}
ref D offset() {C; return n.offset; }
}
int n, m;
sc.read(n, m);
E[][] g = new E[][n];
foreach (i; 0..m) {
int a, b, c;
sc.read(a, b, c); a--; b--;
g[a] ~= E(i, c);
g[b] ~= E(i, c);
}
auto heap = new PairingHeapAllAdd[2*n];
foreach (i; 0..n) {
// writeln(i, " -> ", g[i]);
heap[i] = PairingHeapAllAdd(g[i]);
}
int[] pre = new int[m]; pre[] = -1;
bool[] fix = new bool[m];
long sm = 0;
int pc = n;
foreach (i; 0..2*n) {
if (i == pc) break;
while (heap[i].length && fix[heap[i].front.to]) heap[i].removeFront;
if (!heap[i].length) continue;
auto e = heap[i].front;
int c = e.dist;
int x = i;
if (pre[e.to] == -1) {
sm += c;
pre[e.to] = i;
continue;
}
int y = pre[e.to];
//merge x, y
fix[e.to] = true;
int P = pc++;
heap[P].meld(heap[x]);
heap[P].meld(heap[y]);
}
writeln(sm);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string input;
while ((input = readln.chomp).length != 0) {
auto n = input.to!int;
auto sum = 0;
auto a = 2;
sum += a;
if (n >= 2) {
sum += (n+2)*(n-1)/2;
}
writeln(sum);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n = lread();
auto alph = "abcdefghijklmnopqrstuvwxyz";
auto k = new long[](0);
while (n > 0)
{
k ~= n % 26;
n /= 26;
}
// k.writeln();
auto ans = new char[](0);
foreach (i, e; k)
{
if (e)
{
ans ~= alph[e - 1];
}
else
{
if (i + 1 < k.length)
{
ans ~= 'z';
long tmp = i + 1;
while (k[tmp] == 0)
{
k[tmp] = 25;
tmp++;
}
k[tmp]--;
}
}
}
ans.reverse().writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import 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;
scan(A, B);
long m = (A + B).max(A - B).max(A * B);
writeln(m);
}
|
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;
enum MOD = 998244353L;
struct Sum{
long k;
long s;
}
void main()
{
long N = scanElem;
string S = readln.strip;
auto list = S.group.array;
Sum[] sumList = [Sum()];
foreach(e; list)
{
if(e[0]=='#')
{
sumList~=Sum(sumList[$-1].k+e[1], sumList[$-1].s);
}else{
sumList~=Sum(sumList[$-1].k, sumList[$-1].s+e[1]);
}
}
long res=long.max;
foreach(i;0..sumList.length)
{
long a;
a+= sumList[$-1].s - sumList[i].s;
a+= sumList[i].k;
res = min(a, res);
}
writeln(res);
}
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;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int N, L; scan(N, L);
// N 個のリングのおいしさは、L, L + 1, L + 2, ..., L + N - 1
int t = iota(L, L + N).reduce!((a, b) => abs(a) < abs(b) ? a : b);
int ans = iota(L, L + N).sum - t;
writeln(ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto A = N.iota.map!(_ => readln.chomp.to!int - 1).array;
auto B = new int[](N);
foreach (i; 0..N) B[A[i]] = i;
int ans = 1;
int tmp = 1;
foreach (i; 0..N-1) {
if (B[i] < B[i+1])
tmp++;
else
tmp = 1;
ans = max(ans, tmp);
}
ans = N - ans;
ans.writeln;
}
|
D
|
import std.algorithm;
import std.stdio;
void main ()
{
auto s = readln;
auto x = (s[0] == 'a' || s[0] == 'h');
auto y = (s[1] == '1' || s[1] == '8');
writeln (8 - 3 * (x + y) + (x * y));
}
|
D
|
void main()
{
long x, y, z;
rdVals(x, y, z);
writeln(z, " ", x, " ", y);
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.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;
enum L = 2001;
void main() {
int M, D;
scan(M, D);
int ans;
foreach (m ; 1 .. M + 1) {
foreach (d ; 1 .. D + 1) {
auto d1 = d % 10;
auto d10 = d / 10;
if (d1 <= 1 || d10 <= 1) {
continue;
}
if (d1 * d10 == m) ans++;
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.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 P = s[1];
auto A = readln.split.map!(to!int).array;
auto dp = new long[][](N+1, 2);
dp[0][0] = 1;
foreach (i; 0..N) {
if (A[i] % 2 == 0) {
dp[i+1][0] = dp[i][0] + dp[i][0];
dp[i+1][1] = dp[i][1] + dp[i][1];
}
else {
dp[i+1][0] = dp[i][0] + dp[i][1];
dp[i+1][1] = dp[i][0] + dp[i][1];
}
}
dp[N][P].writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main() {
auto first = new char[0];
auto last = readln.chomp.dup;
uint queryMax = readln.chomp.to!uint;
foreach (i;0..queryMax) {
auto query = readln.chomp.split(' ');
if (query[0] == "1") {
swap(first, last);
} else if (query[0] == "2") {
if (query[1] == "1") {
first ~= query[2];
} else if (query[1] == "2") {
last ~= query[2];
} else {
assert(false);
}
} else {
assert(false);
}
}
foreach_reverse (c;first) {
write(c);
}
writeln(last);
}
|
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;
alias Point = Tuple!(int, "x", int, "y");
int maxBipartiteMatching(bool[][] g) {
bool mbm(int p, int[] left, int[] right, bool[][] g, bool[] used) {
for (int i = 0; i < right.length; i++) {
if (g[p][i] && right[i] == -1) {
left[p] = i;
right[i] = p;
return true;
}
}
for (int i = 0; i < right.length; i++) {
if (g[p][i] && !used[i]) {
used[i] = true;
if (mbm(right[i], left, right, g, used)) {
left[p] = i;
right[i] = p;
return true;
}
}
}
return false;
}
auto left = new int[g.length];
auto right = new int[g[0].length];
left[] = -1;
right[] = -1;
int cnt = 0;
for (int i = 0; i < left.length; i++) {
auto used = new bool[left.length];
if (mbm(i, left, right, g, used)) {
cnt++;
}
}
return cnt;
}
int calc(Point[] ab, Point[] cd) {
assert(ab.length == cd.length);
auto n = ab.length;
auto g = new bool[][](n, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (ab[i].x < cd[j].x && ab[i].y < cd[j].y) {
g[i][j] = true;
}
}
}
return maxBipartiteMatching(g);
}
void main() {
int n = readint;
Point[] ab, cd;
for (int i = 0; i < n; i++) {
auto xy = readints;
ab ~= Point(xy[0], xy[1]);
}
for (int i = 0; i < n; i++) {
auto xy = readints;
cd ~= Point(xy[0], xy[1]);
}
writeln(calc(ab, cd));
}
|
D
|
void main()
{
long n, q;
rdVals(n, q);
foreach (i; 0 .. q)
{
long v, w;
rdVals(v, w);
if (n == 1)
{
min(v, w).writeln;
continue;
}
while (v != w)
{
if (v < w) w = (w + n - 2) / n;
else v = (v + n - 2) / n;
}
v.writeln;
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.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 N = readln.chomp.to!long;
long solve() {
return N % 2 == 0 ? N/2 : N/2 + 1;
}
solve().writeln;
}
|
D
|
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format;
import std.algorithm,std.range,std.array;
T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
//END OF TEMPLATE
void main(){
ulong m,n;
ulong[] a;
scan(n,m);
a=readarr!ulong;
auto sum=a.csum;
(a.filter!(x=>x.to!real>=sum.to!real/(4*m).to!real).array.length>=m?"Yes":"No").writeln;
}
auto csum(ulong[] a){
ulong tmp;
foreach(t;a)tmp+=t;
return tmp;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a,b;
scan(a,b);
(a%b?1:0).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;
void main() {
int N, M;
scan(N, M);
auto a = new int[](N);
a[] = -1;
foreach (i ; 0 .. M) {
int si, ci;
scan(si, ci);
si--;
if (a[si] != -1 && a[si] != ci) {
writeln(-1);
return;
}
a[si] = ci;
}
if (a[0] == 0) {
writeln(N == 1 ? 0 : -1);
return;
}
if (a.all!"a == -1" && N == 1) {
writeln(0);
return;
}
foreach (i ; 0 .. N) {
if (a[i] != -1) {
write(a[i]);
}
else {
write(i == 0 ? 1 : 0);
}
}
writeln;
}
void scan(T...)(ref T args) {
auto 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);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
|
D
|
import std.stdio;
import std.string;
void main() {
string s = readln.chomp;
if (s == "SUN") writeln(7);
if (s == "MON") writeln(6);
if (s == "TUE") writeln(5);
if (s == "WED") writeln(4);
if (s == "THU") writeln(3);
if (s == "FRI") writeln(2);
if (s == "SAT") writeln(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;
void main() {
int n;
scan(n);
writeln(n % s(n) == 0 ? "Yes" : "No");
}
int s(int n) {
return n > 0 ? s(n / 10) + n % 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
|
void main() {
auto N = ri;
auto S = rs;
ulong res = ulong.max;
auto A = new int[](N);
// i番目含め左から今までのWの個数
A[0] = S[0] == 'W';
foreach(i; 1..N) A[i] = S[i] == 'W' ? A[i-1]+1 : A[i-1];
// i番目含め左から今までのEの個数
// C[i] = (i+1) - A[i];
// 下二行はどちらも同じ結果をもつ
auto C = iota(1, N+1).map!(a => a - A[a-1]).array;
// auto C = iota(N).map!(a => (a+1) - A[a]).array;
foreach(i; 0..N) {
ulong tmp;
tmp += C[$-1] - C[i]; // リーダーより右でEを向いている人の数
tmp += i < 1 ? 0 : A[i-1]; // リーダーより左でWを向いている人の数
res = min(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 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 n = readln.chomp.length;
foreach(i; 0..n) write("x");
writeln("");
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional;
import std.algorithm, std.container;
struct Qes{
char t;
char d;
}
long N;
string S;
Qes[] qs;
long calc(long s, long g)
{
long pos = (s+g)/2;
foreach(q; qs)
{
if(q.t!=S[pos-1])continue;
pos += q.d=='R'?1:-1;
if(pos==0)break;
if(pos==N+1)break;
}
if(s==g)
{
if(pos==0)
return s;
else
return 0;
}
if(abs(s-g)==1)
{
return max(calc(s, s) , calc(g, g));
}
return pos==0 ? calc((s+g)/2, g) : calc(s, (s+g)/2);
}
long calc2(long s, long g)
{
if(abs(s-g)==1)
{
return max(calc2(s, s) , calc2(g, g));
}
long pos = (s+g)/2;
foreach(q; qs)
{
if(q.t!=S[pos-1])continue;
pos += q.d=='R'?1:-1;
if(pos==N+1)break;
if(pos==0)break;
}
if(s==g)
{
if(pos==N+1)
return N-s+1;
else
return 0;
}
return pos==N+1 ? calc2(s, (s+g)/2) : calc2((s+g)/2, g);
}
void main()
{
N = scanElem;
long Q = scanElem;
S = readln.strip;
foreach(i; 0..Q)
{
char t = scanElem!char;
char d = scanElem!char;
qs ~= Qes(t,d);
}
auto l = calc(1, N);
auto r = calc2(1,N);
writeln(N-l-r);
}
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));
}
}
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto S = RD!string;
bool ans = true;
foreach (i; 0..S.length)
{
if (i % 2 == 0)
{
if (S[i] != 'h')
ans = false;
}
else if (S[i] != 'i')
ans = false;
}
writeln(S.length % 2 == 0 && ans ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std;
void main()
{
const K = readln().chomp().to!ulong();
ulong sum = 0;
foreach (i; 1 .. K + 1)
foreach (j; 1 .. K + 1)
foreach (k; 1 .. K + 1)
{
sum += gcd(gcd(i, j), k);
}
writeln(sum);
}
|
D
|
void main() {
auto N = ri;
ulong a = 0, b = 0;
while(4*a <= 100) {
ulong tmp = 4*a + 7*b;
while(tmp <= 100) {
if(tmp == N) {
writeln("Yes");
return;
}
b++;
tmp = 4*a + 7*b;
}
a++;
b = 0;
}
writeln("No");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp.to!(char[]);
if (s[0] == s[$-1] && s.length%2 == 0 || s[0] != s[$-1] && s.length%2 == 1) {
writeln("First");
} else {
writeln("Second");
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
bool ok;
foreach (i; 1 .. 10) {
foreach (j; 1 .. 10) {
if (i * j == n) ok = true;
}
}
writeln(ok ? "Yes" : "No");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ss = reads!string;
auto a = ss[0], b = ss[1], c = ss[2];
if (a[$-1] == b[0] && b[$-1] == c[0])
writeln("YES");
else
writeln("NO");
}
|
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;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
int n = cin.next!int;
writeln(n / 3);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto input = readln.split.to!(int[]);
auto A = input[0], B = input[1], C = input[2], D = input[3];
writeln( (C%B == 0 ? C/B : C/B+1) <= (A%D == 0 ? A/D : A/D+1) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto N = readln.chomp.to!int;
auto ts = readln.split.to!(int[]);
auto as = readln.split.to!(int[]);
auto xs = new long[](N);
foreach (i; 0..N) {
if (i == 0 || ts[i] > ts[i-1]) {
xs[i] = -ts[i];
} else {
xs[i] = ts[i];
}
}
foreach_reverse (i; 0..N) {
if (i == N-1 || as[i] > as[i+1]) {
if (xs[i] == -as[i] || xs[i] >= as[i]) {
xs[i] = -as[i];
} else {
writeln(0);
return;
}
} else {
xs[i] = min(xs[i], as[i]);
}
}
auto r = 1L;
foreach (x; xs) {
r = (r * max(1, x)) % P;
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
readln;
int c;
foreach (i; readln.split.to!(int[])) if (i%2 == 1) ++c;
writeln(c%2 == 0 ? "YES" : "NO");
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.algorithm;
import std.conv;
void main() {
auto buf = readln.chomp.split.map!(x => x.to!int);
int N = buf[0];
int x = buf[1];
ulong[] a = readln.chomp.split.map!(x => x.to!ulong).array;
ulong count = 0;
foreach(i; 0 .. N) {
if (i == 0) {
if (a[i] > x) {
count += a[i] - x;
a[i] -= count;
}
continue;
}
ulong sum = a[i-1] + a[i];
if (sum > x) {
count += sum - x;
a[i] -= sum - x;
}
}
writeln(count);
}
|
D
|
/+ dub.sdl:
name "C"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
long a, b, c, x, y;
sc.read(a, b, c, x, y);
a = min(a, 2*c);
b = min(b, 2*c);
c = min(2*c, a+b);
long z = min(x, y);
writeln(a*(x-z) + b*(y-z) + c*z);
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
int ds = digsum(n);
writeln(n % ds == 0 ? "Yes" : "No");
}
int digsum(int n) {
return n > 0 ? digsum(n / 10) + (n % 10) : 0;
}
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto s = sread();
long t;
foreach (e; s)
{
t += (e == '+' ? 1 : -1);
}
t.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, 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!T;r.popFront;}}
void readM(T)(size_t r,size_t c,ref T[][]t){t=new T[][](r);foreach(ref ti;t)readA(c,ti);}
void main()
{
auto t = 10;
int n; readV(n);
int[][] f; readM(n, t, f);
int[][] p; readM(n, t+1, p);
auto fb = new int[](n);
foreach (i; 0..n)
foreach (j; 0..t)
if (f[i][j]) fb[i] = fb[i].bitSet(j);
auto r = -10^^9;
foreach (i; 1..1<<t) {
auto s = 0;
foreach (j; 0..n) s += p[j][(i&fb[j]).popcnt];
r = max(r, s);
}
writeln(r);
}
pragma(inline) {
pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
import core.bitop;
pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto nmq = readints;
int n = nmq[0], m = nmq[1], Q = nmq[2];
auto g = new int[][](n + 1, n + 1);
for (int i = 0; i < m; i++) {
auto lr = readints;
g[lr[0]][lr[1]]++;
}
for (int r = n; r > 0; r--) {
for (int c = 1; c <= n; c++) {
g[r][c] += g[r][c - 1];
}
if (r < n) {
for (int c = 1; c <= n; c++) {
g[r][c] += g[r + 1][c];
}
}
}
for (int i = 0; i < Q; i++) {
auto pq = readints;
int p = pq[0], q = pq[1];
writeln(g[p][q]);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
void main() {
auto a = readln.chomp.dup;
auto b = readln.chomp.dup;
reverse(b);
writeln(a == b ? "YES" : "NO");
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
writeln(tmp[0] <= 8 && tmp[1] <= 8 ? "Yay!" : ":(");
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
auto T = ["dreamer", "eraser", "erase", "dream"];
long i = S.length;
w: while (0 < i)
{
foreach (t; T)
{
// writeln(max(0, i - cast(long)t.length), " ", min(i, S.length));
if (S[max(0, i - cast(long)t.length) .. min(i, S.length)] == t)
{
i -= t.length;
continue w;
}
}
writeln("NO");
return;
}
writeln("YES");
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
void main()
{
while (1) {
int cnt = 0;
string[] input = split(readln());
int n = to!(int)(input[0]);
int x = to!(int)(input[1]);
if (n == 0 && x == 0) break;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
for (int k = j + 1; k <= n; k++) {
if (i == j || i == k || j == k) continue;
if (i + j + k == x) cnt++;
}
}
}
writeln(cnt);
}
}
|
D
|
import std.stdio, std.conv, std.array, std.range, std.algorithm, std.string, std.typecons;
void main() {
auto s = readln.strip;
if (15 - s.length + s.count('o') >= 8) {
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;
import std.ascii;
void main()
{
auto s = readln.chomp;
foreach (e; s) {
if (e == '1') {
write('9');
} else if (e == '9') {
write('1');
} else {
write(e);
}
}
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 Pair = Tuple!(string, "x", long, "y");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = aryread();
auto s = a.sum();
bool flag = true;
foreach (i; iota(n))
if(a[i] >= s - a[i])
flag = false;
if(flag)
writeln("Yes");
else
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
void main() {
long[] tmp = readln.split.to!(long[]);
long n = tmp[0], a = tmp[1], b = tmp[2];
long[] x = readln.split.to!(long[]);
long result;
foreach (i; 1 .. n) {
long diff = x[i] - x[i-1];
result += min(a*diff, b);
}
result.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
bool[3] a;
a[0] = true;
a[1] = false;
a[2] = false;
while(true){
auto s = readln();
if(stdin.eof()) break;
s = chomp(s);
swap(a[to!int(s[0]-'A')],a[to!int(s[2]-'A')]);
}
for(int i=0;i<3;i++){
if(a[i]) writeln(to!char(i + to!int('A')));
}
}
|
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); }
T binarySearch(alias pred, T)(T ok, T ng)
{
while (abs(ok-ng) > 1)
{
auto mid = (ok+ng)/2;
if (unaryFun!pred(mid)) ok = mid;
else ng = mid;
}
return ok;
}
void main()
{
auto N = RD;
auto K = RD;
auto A = RDA;
bool f(long x)
{
long cnt;
foreach (i; 0..N)
{
cnt += (A[i]-1) / x;
}
return cnt <= K;
}
auto ans = binarySearch!(f)(10L^^9, 0);
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
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;
long modPow(long x, long k, long m) {
if (k == 0) return 1;
if (k % 2 == 0) return modPow(x * x % m, k / 2, m);
return x * modPow(x, k - 1, m) % m;
}
const MOD = 10^^9 + 7;
long calc(string s) {
long ans = 0;
int one = 0; // 1 の個数
for (int i = 0; i < s.length; i++) {
if (s[i] == '1') {
// s[i] = 0 とする
// 範囲 [0, i) の各桁の組み合わせは (0, 1), (1, 0) の 2 通り
// 範囲 [i + 1, $) の各桁の組み合わせは (0, 0), (0, 1), (1, 0) の 3 通り
long a = modPow(2, one, MOD);
long b = modPow(3, cast(int)s.length - i - 1, MOD);
ans = (ans + (a * b)) % MOD;
one++;
}
}
// 上の for ループで i 番目を 0 にしたが、どの桁の 1 も 0 にしない場合
ans = (ans + modPow(2, one, MOD)) % MOD;
return ans;
}
void main() {
string s = read!string;
writeln(calc(s));
}
|
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;
writeln(N * (N - 1) / 2);
stdout.flush();
debug readln();
}
|
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, std.datetime;
// 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 H, W, K;
scan(H, W, K);
auto S = new string[](H);
foreach (i; 0 .. H)
S[i] = sread();
long ans = long.max;
loop_split_row: foreach (uint split_row; 0 .. 1 << (H - 1))
{
long cnt = popcnt(split_row);
long row = cnt + 1;
auto sum = new long[][](row, W);
loop_w: foreach (i; 0 .. W)
{
long j;
foreach (k; 0 .. H)
{
// writefln("%010b %s %s", split_row & (1 << k), j, k);
sum[j][i] += S[k][i] - '0';
j += ((split_row & (1 << k)) != 0);
}
if (i == 0)
continue loop_w;
foreach (k; 0 .. row)
if (K < sum[k][i])
continue loop_split_row;
foreach (k; 0 .. row)
if (K < sum[k][i] + sum[k][i - 1])
{
// writeln(i);
cnt++;
continue loop_w;
}
foreach (k; 0 .. row)
sum[k][i] += sum[k][i - 1];
}
// writefln("%010b %s %s", split_row, sum, cnt);
ans = ans.min(cnt);
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
auto S = split(readln())[0];
auto year = to!int(S[0 .. 4]), month = to!int(S[5 .. 7]), day = to!int(S[8 .. 10]);
string result = (year < 2019 || year == 2019 && month < 4 || year == 2019 && month == 4 && day <= 30) ? "Heisei" : "TBD";
writeln(result);
}
|
D
|
import std.stdio, std.string, std.algorithm, std.conv, std.range, std.math;
void main() {
foreach (string line; lines(stdin)) {
if (line == "0\n")
break;
int n = line.chomp.to!int;
auto points = readln.chomp.split.map!(to!double);
auto avr = points.reduce!"a+b" / n;
double var = 0;
foreach (e; points) { var += pow(avr - e, 2); }
printf("%.8lf\n", sqrt(var / n));
}
}
|
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 = RDR.ARR;
long dir, last = A[0];
long ans = 1;
foreach (i; 0..N)
{
auto diff = A[i] - last;
if (sgn(diff) != 0)
{
if (sgn(diff) * dir < 0)
{
++ans;
dir = 0;
}
else
dir = sgn(diff);
}
last = A[i];
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
/*
int digits(int n) {
return n.to!string.map!(c => c - '0').sum;
}
*/
int digits(int n) {
if (n == 0) return 0;
int x = 0;
while (n > 0) {
x += n % 10;
n /= 10;
}
return x;
}
int calc(int n) {
int ans = int.max;
for (int a = 1; a < n; a++) {
int b = n - a;
ans = min(ans, digits(a) + digits(b));
}
return ans;
}
void main() {
int n = readint;
auto ans = calc(n);
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm, std.range;
void main()
{
0.iota(readln.chomp.to!int+1).reduce!"a+b".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;
import std.numeric;
void main()
{
auto x = readln.chomp.to!int;
while (x > 0) {
auto m = x % 100;
if (m > 5) {
m = 5;
}
x -= 100 + m;
}
writeln(x == 0 ? 1 : 0);
}
|
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.to!(int[]); }
alias Info = Tuple!(int, "l", int, "r", int, "d");
bool calc(int n, Info[] infos) {
auto uf = new UnionFind(n);
foreach (info; infos) {
uf.unite(info.l, info.r);
}
Info[][int] db;
foreach (info; infos) {
db[info.l] ~= info;
db[info.r] ~= info;
}
bool[int] used;
for (int i = 1; i <= n; i++) {
if (i in used) continue;
// used[i] = true;
int[int] map;
auto q = [i];
while (q.length > 0) {
int node = q[0];
q = q[1..$];
if (node in used) continue;
used[node] = true;
if (node in db) {
foreach (info; db[node]) {
int l = info.l;
int r = info.r;
if (l !in used) q ~= l;
if (r !in used) q ~= r;
if (l in map && r in map) {
int x = map[l];
int y = map[r];
if (y - x != info.d) return false;
}
else if (l in map) {
int x = map[l];
map[r] = x + info.d;
}
else if (r in map) {
int y = map[r];
map[l] = y - info.d;
}
else {
map[l] = 0;
map[r] = info.d;
}
}
}
}
}
return true;
}
void main() {
auto nm = readints;
int n = nm[0], m = nm[1];
Info[] infos;
for (int i = 0; i < m; i++) {
auto lrd = readints;
infos ~= Info(lrd[0], lrd[1], lrd[2]);
}
writeln(calc(n, infos) ? "Yes" : "No");
}
class UnionFind {
private int[] _data;
private int[] _nexts; // 次の要素。なければ -1
private int[] _tails; // 末尾の要素
this(int n) {
_data = new int[](n + 1);
_nexts = new int[](n + 1);
_tails = new int[](n + 1);
_data[] = -1;
_nexts[] = -1;
for (int i = 0; i < _tails.length; i++)
_tails[i] = i;
}
int root(int a) {
if (_data[a] < 0) return a;
return _data[a] = root(_data[a]);
}
bool unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb) return false;
// ra に rb を繋げる
_data[ra] += _data[rb];
_data[rb] = ra;
// ra の末尾の後ろに rb を繋げる
_nexts[_tails[ra]] = rb;
// ra の末尾が rb の末尾になる
_tails[ra] = _tails[rb];
return true;
}
bool isSame(int a, int b) {
return root(a) == root(b);
}
/// a が属する集合のサイズ
int groupSize(int a) {
return -_data[root(a)];
}
/// a が属する集合の要素全て
void doEach(int a, void delegate(int) fn) {
auto node = root(a);
while (node != -1) {
fn(node);
node = _nexts[node];
}
}
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner, dcomp.algorithm;
int main() {
auto sc = new Scanner(stdin);
int[] buf = new int[1000];
int a, b;
sc.read(a, b);
foreach (i; a..b) {
buf[i] += 1;
}
sc.read(a, b);
foreach (i; a..b) {
buf[i] += 1;
}
writeln(buf.filter!"a==2".array.length);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
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);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
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) {
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) {
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);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
|
D
|
import std.stdio, std.string, std.algorithm;
void main(){
readln.split.sort.join(" ").writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(long[]), n = rd[0], m = rd[1];
auto c = min(n, m/2);
m -= c*2;
c += m/4;
writeln(c);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int r = readint;
if (r < 1200) writeln("ABC");
else if (r < 2800) writeln("ARC");
else writeln("AGC");
}
|
D
|
import std.stdio, std.ascii;
void main() {
auto d = readln.dup;
foreach (c; d) c.isLower ? c.toUpper.write : c.toLower.write;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
readln;
auto as = readln.split.to!(long[]);
auto x = as[0];
foreach (a; as[1..$]) x = gcd(a, x);
writeln(x);
}
|
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()
{
(48-readln.chomp.to!int).writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
uint[string] table;
while(true) {
string line = readln.chomp;
if (stdin.eof) break;
auto data = line.split(",");
table[data[1]] += 1;
}
foreach(type; ["A", "B", "AB", "O"]) {
if (type in table)
writeln(table[type]);
else
writeln(0);
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (true) {
int n,q;
scan(n,q);
if (!n && !q) return;
auto a = iota(n).map!(i => readln.split.to!(int[])).array;
solve(n, q, a);
}
}
void solve(int n, int q, int[][] a) {
auto b = new int[](200);
foreach (ai ; a) {
foreach (d ; ai[1 .. $]) {
b[d]++;
}
}
int mx = b.reduce!max;
if (mx >= q) {
writeln(b.countUntil(mx));
}
else {
writeln(0);
}
}
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.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.bigint;
void main()
{
auto str1 = readln.chomp;
auto str2 = readln.chomp;
int s1, s2;
for (int i; i < str1.length; i+=2) {
if (str1[i] == '(' && str2[i] == '8') s1++;
else if (str1[i] == '8' && str2[i] == '[') s1++;
else if (str1[i] == '[' && str2[i] == '(') s1++;
else if (str2[i] == '(' && str1[i] == '8') s2++;
else if (str2[i] == '8' && str1[i] == '[') s2++;
else if (str2[i] == '[' && str1[i] == '(') s2++;
}
if (s1 > s2) "TEAM 1 WINS".writeln;
else if (s1 < s2) "TEAM 2 WINS".writeln;
else "TIE".writeln;
}
|
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; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
foreach(_; 0 .. scan!int){
long x = scan!long;
print(1, x - 1);
}
}
|
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;
long ans = 1;
foreach (i; 0..N)
{
ans.modm(i+1);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
long Q, H, S, D;
scan(Q, H, S, D);
long N = lread();
N *= 4;
long solve(long x)
{
if (x == 0)
return 0;
long ret = long.max;
if (8 <= x)
ret = ret.min((x / 8) * D + solve(x % 8));
if (4 <= x)
ret = ret.min((x / 4) * S + solve(x % 4));
if (2 <= x)
ret = ret.min((x / 2) * H + solve(x % 2));
if (1 <= x)
ret = ret.min((x / 1) * Q + solve(x % 1));
return ret;
}
solve(N).writeln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
void main() {
while (true) {
int H, N;
scanf("%d %d\n", &H, &N);
if (H == 0 && N == 0) break;
const int X = 20;
auto F = new string[2][X];
foreach (i; 0 .. H) {
F[i][0] = readln.chomp;
F[i][1] = readln.chomp;
}
foreach (i; H .. X) foreach (j; 0 .. 2) F[i][j] = "..";
int c = H;
struct S {
string[2][] F;
int del_n;
}
S[] states = [S(F, 0)];
foreach (n; 0 .. N) {
string[2][2] block;
foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
block[i][j] = readln.chomp; // which comes first is the lower one
}
bool empty(string[2] f) {
foreach (i; 0 .. 2) foreach (j; 0 .. 2) if (f[i][j] == '#') return false;
return true;
}
if (empty(block[0])) {
swap(block[0], block[1]);
}
S[] nstates;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
char[][2][2] nblock;
foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
nblock[i][j] = new char[2];
foreach (k; 0 .. 2) {
nblock[i][j][k] = '.';
}
}
foreach (k; 0 .. 2) {
foreach (i; 0 .. 2) {
foreach (j; 0 .. 2) {
if (block[k][i][j] == '#' && (i + dy < 0 || i + dy >= 2 || j + dx < 0 || j + dx >= 2)) {
goto next;
}
}
}
}
// this block can be slided by [dy, dx]
foreach (k; 0 .. 2) foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
if (block[k][i][j] == '#') {
nblock[k][i + dy][j + dx] = '#'; // this line cannot cause range violation
}
}
foreach (s; states) {
S drop() {
auto F = s.F.dup;
int del_n = s.del_n;
int h = 0;
bool collide(string[2] f, string[2] g) {
foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
if (f[i][j] == '#' && g[i][j] == '#') return true;
}
return false;
}
string[2] merge(string[2] f, string[2] g) {
assert(!collide(f, g));
char[2][2] r;
foreach (i; 0 .. 2) foreach (j; 0 .. 2) {
r[i][j] = ((f[i][j] == '#' || g[i][j] == '#') ? '#' : '.');
}
string[2] ret;
foreach (i; 0 .. 2) ret[i] = r[i].idup;
return ret;
}
for (int y = X - 2; y >= 0; y--) {
if (collide(F[y], cast(string[2])nblock[0]) || collide(F[y + 1], cast(string[2])nblock[1])) {
h = y + 1;
break;
}
}
foreach (i; 0 .. 2) {
F[h + i] = merge(F[h + i], cast(string[2])nblock[i]);
}
int c_height = 0;
foreach (y; 0 .. X) {
bool filled(string[2] f) {
foreach (i; 0 .. 2) foreach (j; 0 .. 2)
if (f[i][j] == '.') return false;
return true;
}
if (filled(F[y])) {
del_n++;
} else {
F[c_height] = F[y];
c_height++;
}
}
foreach (y; c_height .. X) {
F[y] = ["..", ".."];
}
return S(F, del_n);
}
auto nstate = drop();
nstates ~= nstate;
}
next:;
}
}
states = nstates;
}
int ans = 0;
foreach (state; states) {
ans = max(ans, state.del_n);
}
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.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.split.to!(int[]);
auto p = 10000;
int cnt;
foreach (i; 1..n) {
if (a[i-1] == a[i]) {
a[i] = p;
p--;
cnt++;
}
}
cnt.writeln;
}
|
D
|
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;
void main()
{
auto nw = readln.split.to!(int[]);
auto N = nw[0];
auto W = nw[1];
int[] vs, ws;
foreach (_; 0..N) {
auto vw = readln.split.to!(int[]);
vs ~= vw[0];
ws ~= vw[1];
}
auto DP = new int[][](N+1, W+1);
foreach (i; 0..N) {
foreach_reverse (j; 0..W+1) {
auto w = j-ws[i];
if (w >= 0) {
DP[i][w] = max(DP[i][w], DP[i][j] + vs[i]);
}
DP[i+1][j] = max(DP[i+1][j], DP[i][j]);
}
}
int r = -1;
foreach (dp; DP[N]) r = max(r, dp);
writeln(r);
}
|
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 x = 1;
while (x * 2 <= N)
{
x *= 2;
}
writeln(x);
}
|
D
|
void main()
{
string s = readln.chomp;
s[0..$-8].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.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 t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
long x, y;
foreach (i; 0..n-1)
{
if (i < n/2-1)
{
x += 2^^(i+1);
}
else
{
y += 2^^(i+1);
}
}
x += 2^^n;
ans[ti] = x - y;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
writeln((scanElem-1)*(scanElem-1));
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = new long[](N);
foreach (i; 0 .. N)
A[i] = lread();
auto dp = new long[](N + 1);
dp[] = long.min;
dp[0] = long.max;
long ans = long.min;
foreach (i; 0 .. N)
{
long j = () {
long ok = 0;
long ng = N + 2;
while (1 < ng - ok)
{
long m = (ng + ok) / 2;
if (A[i] <= dp[m])
{
ok = m;
}
else
{
ng = m;
}
}
return ng;
}();
dp[j] = A[i];
ans = ans.max(j);
}
writeln(ans);
}
|
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; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
long k = scan!long;
int n = scan!int;
long[] as = scan!long(n);
long g = k - as[$ - 1] + as[0];
foreach(i; 0 .. n - 1) g.raiseTo(as[i + 1] - as[i]);
(k - g).writeln;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.