code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1];
auto g = new bool[][](n, n);
foreach (_; 0..m) {
auto rd2 = readln.split.to!(size_t[]), a = rd2[0]-1, b = rd2[1]-1;
g[a][b] = g[b][a] = true;
}
auto isValid(size_t[] p)
{
if (!g[0][p[0]]) return false;
foreach (i; 0..n-2)
if (!g[p[i]][p[i+1]]) return false;
return true;
}
auto p = iota(1, n).array, ans = 0;
do {
if (isValid(p)) ++ans;
} while (p.nextPermutation);
writeln(ans);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto s = sread();
auto t = cast(ubyte[]) s.dup;
bool b = t.sort().uniq().array().length == s.length;
writeln(b ? "yes" : "no");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
long[] rs, cs;
foreach (_; 0..N) {
auto rc = readln.split.to!(long[]);
rs ~= rc[0];
cs ~= rc[1];
}
auto DP = new long[][](N, N);
foreach (ref dp; DP) dp[] = long.max/3;
foreach (i; 0..N-1) {
DP[i][i] = 0;
DP[i][i+1] = rs[i] * cs[i] * cs[i+1];
}
DP[N-1][N-1] = 0;
foreach (jj; 2..N) {
foreach (i; 0..N-2) {
auto j = jj + i;
if (j >= N) break;
foreach (k; i..j) {
DP[i][j] = min(DP[i][j], DP[i][k] + DP[k+1][j] + rs[i] * cs[k] * cs[j]);
}
}
}
writeln(DP[0][N-1]);
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
/*
int l;
long mod;
long calc(int n, int a, int b, int c, int d) {
assert(c >= 0 && d >= 0);
assert(c == 0 || d == 0);
if (n == 0) return 1;
long ans = 0;
// off
{
int nc = d;
if (a && b >= l + 1) {
assert(a >= 1 && l - a >= 1);
nc = (nc == 0) ? l - a : min(nc, l - a);
}
ans = (ans + calc(n - 1, b, 0, nc, 0)) % mod;
}
// on
if (c != 1 && (a < l || b < l)) {
ans = (ans + calc(n - 1, b, a + 1, d, max(0, c - 1))) % mod;
}
return ans;
}*/
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n, k;
long mod;
sc.read(n, k, mod);
int l = k / 2 + 1;
if (k % 2 == 0) {
long[] dp = new long[n / 2 + 5];
dp[0] = 1;
foreach (i; 1 .. n / 2 + 5) {
foreach (j; max(0, i - l) .. i) {
dp[i] += dp[j];
dp[i] %= mod;
}
}
writeln(dp[n / 2 + 1] * dp[(n + 1) / 2 + 1] % mod);
return 0;
}
//writeln(calc(n, 0, 0, 0, 0));
//long[][][][] dp = new long[][][][](n / 2 + 2, n / 2 + 2, l + 1, l + 1);
//dp[0][0][0][0] = 1;
int to_i(int a, int b, int c, int d) {
return a * 80 * 80 * 80 + b * 80 * 80 + c * 80 + d;
}
long[] dp = new long[80 * 80 * 80 * 80];
dp[0] = 1;
long[] ndp = new long[80 * 80 * 80 * 80];
foreach (_n; 0..n) {
//long[][][][] ndp = new long[][][][](n / 2 + 2, n / 2 + 2, l + 1, l + 1);
foreach (a; 0..n/2 + 2) {
foreach (b; 0..n/2 + 2) {
foreach (c; 0..l + 1) {
ndp[to_i(a, b, c, 0)] = 0;
}
foreach (d; 1..l + 1) {
ndp[to_i(a, b, 0, d)] = 0;
}
}
}
void calc(int a, int b, int c, int d) {
if (dp[to_i(a, b, c, d)] == 0) return;
// off
{
int nc = d;
if (a && b >= l + 1) {
assert(a >= 1 && l - a >= 1);
nc = (nc == 0) ? l - a : min(nc, l - a);
}
ndp[to_i(b, 0, nc, 0)] += dp[to_i(a, b, c, d)];
ndp[to_i(b, 0, nc, 0)] %= mod;
}
// on
if (c != 1 && (a < l || b < l)) {
ndp[to_i(b, a+1, d, max(0, c - 1))] += dp[to_i(a, b, c, d)];
ndp[to_i(b, a+1, d, max(0, c - 1))] %= mod;
}
}
foreach (a; 0..n/2 + 2) {
foreach (b; 0..n/2 + 2) {
foreach (c; 0..l + 1) {
calc(a, b, c, 0);
}
foreach (d; 1..l + 1) {
calc(a, b, 0, d);
}
}
}
swap(dp, ndp);
//dp = ndp;
}
writeln(dp.sum % mod);
// writeln(dp.map!(v => v.map!(v => v.map!(v => v.sum % mod).sum % mod).sum % mod).sum % mod);
return 0;
}
/* IMPORT /home/yosupo/Programs/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/Programs/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/Programs/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;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
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 : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto k = readln.splitter.map !(to !(long)).array;
auto h = readln.splitter.map !(to !(long)).array;
foreach (i; 0..n)
{
foreach (j; i + 1..n)
{
h[i] = max (h[i], h[j] - k[j] + k[i]);
}
}
long res = 0;
long cur = 0;
foreach (i; 0..n)
{
if (i > 0 && h[i] > k[i] - k[i - 1])
{
res += cur * 1L * (k[i] - k[i - 1]);
res += (k[i] - k[i - 1]) *
(k[i] - k[i - 1] + 1L) / 2;
cur += k[i] - k[i - 1];
}
else
{
res += h[i] * (h[i] + 1L) / 2;
cur = h[i];
}
}
writeln (res);
}
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], t = tmp[2];
writeln(t / a * b);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
/+ dub.sdl:
name "F"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int k;
// sc.read(k);
k = 38;
int n = k*(k-1)+1;
bool[][] g = new bool[][](n, n);
foreach (i; 0..k) {
g[i][0] = true;
g[0][i] = true;
}
foreach (i; 0..k) {
int s = 1 + i * (k-1);
foreach (j; 0..k-1) {
g[s+j][i] = true;
g[i][s+j] = true;
}
}
foreach (i; 0..k-1) {
int si = k + i * (k-1);
foreach (j; 0..k-1) {
int sj = k + j * (k-1);
foreach (l; 0..k-1) {
int f = (i * j) % (k-1);
g[si + l][sj + (l + f) % (k-1)] = true;
}
}
}
/* foreach (i; 0..n) {
foreach (j; 0..n) {
write(g[i][j] ? '#' : '.');
}
writeln();
}*/
writeln(n, " ", k);
foreach (i; 0..n) {
foreach (j; 0..n) {
if (g[i][j]) {
write(j+1, " ");
}
}
writeln();
}
return 0;
}
/* IMPORT /Users/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 /Users/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;
}
}
/* IMPORT /Users/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);
}
}
}
}
import core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/*
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, std.string, std.conv, std.algorithm;
void main() {
int[] tmp = readln.chomp.split.to!(int[]);
int n = tmp[0], k = tmp[1];
string s = readln.chomp;
int cnt;
foreach (i; 1 .. n) {
if (s[i] == s[i-1]) ++cnt;
}
min(n-1, cnt+2*k).writeln;
}
|
D
|
void main()
{
long k = rdElem - 1;
long[] a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51];
a[k].writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
int N;
void dfs(int pos, int num, string s) {
if (pos == N) {
writeln(s);
return;
}
foreach (a; 0 .. num) {
dfs(pos + 1, num, s ~ cast(char)('a' + a));
}
dfs(pos + 1, num + 1, s ~ cast(char)('a' + num));
}
void main() {
try {
for (; ; ) {
N = readInt();
dfs(0, 0, "");
}
} catch (EOFException e) {
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto rd = readln.chomp.splitter;
auto ai = new int[](n);
foreach (i; n.iota) {
ai[i] = rd.front.to!int;
rd.popFront;
}
auto k = ai.reduce!(max);
auto bi = countingSort(ai, k);
foreach (i, b; bi) {
write(b);
if (i < n - 1) write(" ");
}
writeln;
}
int[] countingSort(int[] ai, int k)
{
auto ci = new int[](k + 1);
foreach (a; ai) ++ci[a];
foreach (i; 1..k+1) ci[i] += ci[i - 1];
auto bi = new int[](ai.length);
foreach_reverse (a; ai) {
bi[ci[a] - 1] = a;
--ci[a];
}
return bi;
}
|
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; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
long ans1;
foreach (i, c; S)
{
if ([c].to!long == i % 2) ++ans1;
}
long ans2;
foreach (i, c; S)
{
if ([c].to!long != i % 2) ++ans2;
}
writeln(min(ans1, ans2));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
enum lim = 10^^5 + 1;
void main() {
int N, W;
scan(N, W);
auto w = new int[](N);
auto v = new int[](N);
iota(N).each!(i => scan(w[i], v[i]));
auto dp = new long[](lim);
dp[] = infl;
dp[0] = 0;
foreach (i ; 0 .. N) {
foreach_reverse (j ; v[i] .. lim) {
chmin(dp[j], dp[j - v[i]] + w[i]);
}
}
foreach_reverse (i ; 0 .. lim) {
if (dp[i] <= W) {
writeln(i);
return;
}
}
}
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;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
int n = readNum!int;
writeln(180 * (n - 2));
}
|
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[] D;
foreach (_; 0 .. N)
D ~= lread();
D.sort().uniq.count.writeln();
}
|
D
|
import std.stdio;
import std.string; // chomp????????????????????????(?????????????????????)
import std.conv; // to????????????????????????
import std.array; // split?????????????????????
void main() {
auto input = readln.split;
auto a = input[0].to!int;
auto b = input[1].to!int;
writeln(a * b, " ", 2 * (a + b));
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto a=scanElem;
auto b=scanElem;
auto c=scanElem;
if(b-a==c-b)end("YES");end("NO");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm;
void main() {
auto input = getStdin!(string[]);
string needle = input[0].toLower;
string[] haystack = input[1..$-1].join(" ").replace(".", "").replace(",", "").toLower.split(" ");
auto count = haystack.count(needle);
count.writeln;
}
T getStdin(T)() {
string[] cmd;
string line;
while ((line = chomp(stdin.readln())) != "") cmd ~= line;
return to!(T)(cmd);
}
|
D
|
void main() {
auto S = rs;
string str = "keyence";
if(S == str) { writeln("YES"); return; }
foreach(i; 0..str.length) {
auto l = str[0..i];
auto r = str[i..$];
if(S.startsWith(l) && S.endsWith(r)) {
writeln("YES");
return;
}
}
writeln("NO");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.range, std.conv, std.string, std.math;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
void main() {
long N = readln().strip.to!long;
long minCount=long.max;
for (long i = 0; i < 5; ++i) {
long n = readln().strip.to!long;
minCount = min(n, minCount);
}
writeln(ceil(N/(minCount.to!real)-1).to!long+5);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
const long mod = 998244353;
void solve(){
int a = scan!int, b = scan!int, c = scan!int, d = scan!int;
auto sf = new long[][](c + 1, d + 1);
foreach(i; a .. c + 1) foreach(j; b .. d + 1){
if(i == a && j == b) sf[i][j] = 1;
else sf[i][j] = (sf[i][j - 1] * i + sf[i - 1][j] * j
+ mod - sf[i - 1][j - 1] * (i - 1) * (j - 1) % mod) % mod;
}
sf[c][d].print;
}
|
D
|
void main()
{
string s = rdStr;
string t;
foreach (x; s)
{
if (x == 'B')
{
if (t.length) t = t[0..$-1];
}
else
{
t ~= x;
}
}
t.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
auto A = scanElem;
auto B = scanElem;
writeln(A-B+1);
}
long gcd(long a, long b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
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!(T[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto N = to!ulong(tokens[0]);
long[] h;
foreach (token; split(my_readln()))
{
h ~= to!long(token);
}
ulong[] costs;
costs.length = N;
fill(costs, ulong.max);
costs[0] = 0;
foreach (i; 0..N-2)
{
costs[i+1] = min(costs[i+1], costs[i] + abs(h[i] - h[i+1]));
costs[i+2] = min(costs[i+2], costs[i] + abs(h[i] - h[i+2]));
}
costs[N-1] = min(costs[N-1], costs[N-2] + abs(h[N-2] - h[N-1]));
writeln(costs[N-1]);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
long r, d, x;
scan(r, d, x);
foreach (i ; 0 .. 10) {
x = r * x - d;
writeln(x);
}
}
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.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto N = RD!long;
bool ans = true;
long[2] pos;
long lastTime;
foreach (i; 0..N)
{
auto t = RD!long;
auto x = RD!long;
auto y = RD!long;
auto d = abs(x - pos[0]) + abs(y - pos[1]);
auto use_t = t - lastTime;
if (d > use_t || (use_t - d) % 2 == 1)
{
ans = false;
break;
}
pos = [x, y];
lastTime = t;
}
writeln(ans ? "Yes" : "No");
stdout.flush();
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner, dkh.numeric.prime;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
long n;
sc.read(n);
auto dlist = n.divisorList;
long sm;
foreach (d; dlist) {
d--;
if (!d) continue;
if (n / d == n % d) sm += d;
}
writeln(sm);
return 0;
}
/* IMPORT /home/yosupo/Programs/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;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
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 : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/numeric/prime.d */
// module dkh.numeric.prime;
import std.traits;
// import dkh.int128;
ulong ulongPowMod(U)(ulong x, U n, ulong md)
if (isIntegral!U || is(U == BigInt)) {
x %= md;
ulong r = 1;
while (n) {
if (n & 1) {
r = mul128(r, x).mod128(md);
}
x = mul128(x, x).mod128(md);
n >>= 1;
}
return r % md;
}
T[] divisorList(T)(T x) {
import std.algorithm : sort;
T[] res;
for (T i = 1; i*i <= x; i++) {
if (x%i == 0) {
res ~= i;
if (i*i != x) res ~= x/i;
}
}
sort(res);
return res;
}
T[] factorList(T)(T x) {
T[] res;
for (T i = 2; i*i <= x; i++) {
while (x % i == 0) {
res ~= i;
x /= i;
}
}
if (x > 1) res ~= x;
return res;
}
// import dkh.numeric.primitive;
bool isPrime(ulong n) {
// import dkh.int128;
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
ulong d = n-1;
while (d % 2 == 0) d /= 2;
ulong[] alist = [2,3,5,7,11,13,17,19,23,29,31,37];
foreach (a; alist) {
if (n <= a) break;
ulong y = ulongPowMod(a, d, n);
ulong t = d;
while (t != n-1 && y != 1 && y != n-1) {
y = mul128(y, y).mod128(n);
t <<= 1;
}
if (y != n-1 && t % 2 == 0) {
return false;
}
}
return true;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/numeric/primitive.d */
// module dkh.numeric.primitive;
import std.traits, std.bigint;
T lcm(T)(in T a, in T b) {
import std.numeric : gcd;
return a / gcd(a,b) * b;
}
Unqual!T pow(T, U)(T x, U n)
if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) {
return pow(x, n, T(1));
}
Unqual!T pow(T, U, V)(T x, U n, V e)
if ((isIntegral!U || is(U == BigInt)) && is(Unqual!T == Unqual!V)) {
Unqual!T b = x, v = e;
Unqual!U m = n;
while (m) {
if (m & 1) v *= b;
b *= b;
m /= 2;
}
return v;
}
T powMod(T, U, V)(T x, U n, V md)
if (isIntegral!U || is(U == BigInt)) {
T r = T(1);
Unqual!U m = n;
while (m) {
if (m & 1) r = (r*x)%md;
x = (x*x)%md;
m >>= 1;
}
return r % md;
}
T[3] extGcd(T)(in T a, in T b)
if (!isIntegral!T || isSigned!T)
{
if (b==0) {
return [T(1), T(0), a];
} else {
auto e = extGcd(b, a%b);
return [e[1], e[0]-a/b*e[1], e[2]];
}
}
T invMod(T)(T x, T md) {
auto r = extGcd!T(x, md);
assert(r[2] == 1);
auto z = r[0];
return (z % md + md) % md;
}
/* IMPORT /home/yosupo/Programs/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/Programs/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/Programs/dunkelheit/source/dkh/ldc/inline.d */
// module dkh.ldc.inline;
version(LDC) {
pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P);
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/int128.d */
// module dkh.int128;
version(LDC) {
// import dkh.ldc.inline;
}
version(LDC) version(X86_64) {
version = LDC_IR;
}
ulong[2] mul128(ulong a, ulong b) {
ulong[2] res;
version(LDC_IR) {
ulong upper, lower;
inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = mul i128 %r1, %r0
%r3 = trunc i128 %r2 to i64
%r4 = lshr i128 %r2, 64
%r5 = trunc i128 %r4 to i64
store i64 %r3, i64* %2
store i64 %r5, i64* %3`, void)(a, b, &lower, &upper);
return [lower, upper];
} else version(D_InlineAsm_X86_64) {
ulong upper, lower;
asm {
mov RAX, a;
mul b;
mov lower, RAX;
mov upper, RDX;
}
return [lower, upper];
} else {
ulong B = 2UL^^32;
ulong[2] a2 = [a % B, a / B];
ulong[2] b2 = [b % B, b / B];
ulong[4] c;
foreach (i; 0..2) {
foreach (j; 0..2) {
c[i+j] += a2[i] * b2[j] % B;
c[i+j+1] += a2[i] * b2[j] / B;
}
}
foreach (i; 0..3) {
c[i+1] += c[i] / B;
c[i] %= B;
}
return [c[0] + c[1] * B, c[2] + c[3] * B];
}
}
ulong div128(ulong[2] a, ulong b) {
version(LDC_IR) {
return inlineIR!(`
%r0 = zext i64 %0 to i128
%r1 = zext i64 %1 to i128
%r2 = shl i128 %r1, 64
%r3 = add i128 %r0, %r2
%r4 = zext i64 %2 to i128
%r5 = udiv i128 %r3, %r4
%r6 = trunc i128 %r5 to i64
ret i64 %r6`,ulong)(a[0], a[1], b);
} else version(D_InlineAsm_X86_64) {
ulong upper = a[1], lower = a[0];
ulong res;
asm {
mov RDX, upper;
mov RAX, lower;
div b;
mov res, RAX;
}
return res;
} else {
if (b == 1) return a[0];
while (!(b & (1UL << 63))) {
a[1] <<= 1;
if (a[0] & (1UL << 63)) a[1] |= 1;
a[0] <<= 1;
b <<= 1;
}
ulong ans = 0;
foreach (i; 0..64) {
bool up = (a[1] & (1UL << 63)) != 0;
a[1] <<= 1;
if (a[0] & (1UL << 63)) a[1] |= 1;
a[0] <<= 1;
ans <<= 1;
if (up || b <= a[1]) {
a[1] -= b;
ans++;
}
}
return ans;
}
}
ulong mod128(ulong[2] a, ulong b) {
version(D_InlineAsm_X86_64) {
ulong upper = a[1], lower = a[0];
ulong res;
asm {
mov RDX, upper;
mov RAX, lower;
div b;
mov res, RDX;
}
return res;
} else {
return a[0] - div128(a, b) * b;
}
}
/*
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.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 X = RD;
long ans = 2;
if (X != 2)
{
foreach (i; X..10^^9)
{
bool ok = true;
for (long j = 2; j*j <= X; ++j)
{
if (i % j == 0)
{
ok = false;
break;
}
}
if (ok)
{
ans = i;
break;
}
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
writeln(A >= 13 ? B : A >= 6 ? B/2 : 0);
stdout.flush();
debug readln();
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
ulong MIDDLE = 100_000;
alias Side = Tuple!(long, "from", long, "to");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
auto numbers = new long[](MIDDLE + 1);
foreach (e; a)
numbers[e]++;
long max_x;
foreach (i; iota(1, MIDDLE))
{
long x = numbers[i - 1] + numbers[i] + numbers[i + 1];
max_x = max(x, max_x);
}
max_x.writeln();
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.algorithm, std.range;
void main() {
auto N = readln.split[0].to!int;
auto H = readln.split.to!(int[]);
H ~= int.max;
auto maxNum = 0;
auto num = 0;
size_t i = 0;
while(i < H.length - 1) {
if(H[i+1]<=H[i]) {
num++;
} else {
maxNum = max(maxNum, num);
num = 0;
}
i++;
}
maxNum.writeln;
}
|
D
|
void main() {
("A" ~ rs[8] ~ "C").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
|
void main() {
int[] tmp = readln.split.to!(int[]);
int x = tmp[0], y = tmp[1] / 2;
writeln(x + y);
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio,std.conv,std.string,std.algorithm;
void main(){
auto nab = readln.chomp.split.map!(to!int);
int n=nab[0],a=nab[1],b=nab[2];
if(n*a<b) writeln(n*a);
else writeln(b);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long d, t, s;
scan(d, t, s);
double tmp = double(d) / double(s);
// writeln(tmp);
if (tmp <= t)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import 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);
if (A == B)
{
writeln("Draw");
return;
}
if (A == 1 || (B != 1 && B < A))
{
writeln("Alice");
}
else
{
writeln("Bob");
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math, std.typecons;
void main() {
int n, t;
scan(n, t);
auto imos = new int[](t + 2);
foreach (i ; 0 .. n) {
int li, ri; scan(li, ri);
imos[li]++;
imos[ri]--;
}
foreach (i ; 0 .. t + 1) {
imos[i + 1] += imos[i];
}
writeln(imos.reduce!max);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
writeln((A + B) % 24);
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.map!(to!int);
auto w = rd[0], h = rd[1], x = rd[2], y = rd[3], r = rd[4];
writeln(x >= r && x <= w - r && y >= r && y <= h - r ? "Yes" : "No");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nx = readln.split.to!(int[]);
auto N = nx[0];
auto X = nx[1];
auto xs = readln.split.to!(int[]);
auto d = xs[0];
X = abs(X - d);
foreach (x; xs[1..$]) {
X = gcd(X, abs(x - d));
}
writeln(X);
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
}
// }}}
void main()
{
auto cin = new Scanner;
int n, m, x;
cin.scan(n, m, x);
int[] a = cin.nextArray!int(m);
int t;
for (int i = x; i <= n; i++)
{
foreach (e; a)
{
if (e == i) t++;
}
}
int y;
for (int i = x; i >= 0; i--)
{
foreach (e; a)
{
if (e == i) y++;
}
}
writeln(min(t, y));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
readln.chomp.split(" ").sort().uniq.count.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
auto s = sread();
auto a = s[0 .. $ - 1].all!(c => c == s[0]);
auto b = s[1 .. $].all!(c => c == s[1]);
writeln((a || b) ? "Yes" : "No");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
void main() {
auto N = readln.chomp.to!int;
auto X = new long[](N);
auto Y = new long[](N);
auto A = new long[](N);
auto B = new long[](N);
foreach (i; 0..N) {
auto s = readln.split.map!(to!long);
X[i] = s[0];
Y[i] = s[1];
}
foreach (i; 0..N) {
auto s = readln.split.map!(to!long);
A[i] = s[0];
B[i] = s[1];
}
long[long][long] mp;
foreach (i; 0..N) {
foreach (j; 0..N) {
long x = X[i] + A[j];
long y = Y[i] + B[j];
mp[x][y] += 1;
}
}
foreach (x; mp.keys) {
foreach (y; mp[x].keys) {
if (mp[x][y] == N) {
writeln(x, " ", y);
return;
}
}
}
}
|
D
|
import core.stdc.stdio;
import std.algorithm;
void main(){
int n,k;
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++){
int a,b;
scanf("%d%d",&a,&b);
int m = min(a,n+1-a,b,n+1-b);
printf("%d\n",(m-1)%3+1);
}
}
|
D
|
import std.stdio, std.range, std.conv, std.string, std.array, std.functional;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
import std.container.binaryheap;
void main()
{
auto N = readln().strip.to!(long);
foreach(n; N..1000)
{
if(n%10 == n/10%10 && n%10 == n/100%10)
{
writeln(n);
return;
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0.
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
void main() {
readln.chomp.find!"a=='A'".retro.find!"a=='Z'".array.length.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){
int n = scan!int;
char[] s = scan!(char[]);
char[] ans;
foreach(c; s) ans ~= c;
long best = 0;
foreach(k; 0 .. n){
char[] t;
foreach(c; s[k .. n]) t ~= c;
if((n - k) % 2) foreach_reverse(c; s[0 .. k]) t ~= c;
else foreach(c; s[0 .. k]) t ~= c;
log("s:", s, "k:", k, "t:", t, "ans:", ans, "best:", best);
if(ans <= t) continue;
else ans = t, best = k;
}
ans.writeln;
(best + 1).writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
import std.math;
import std.range;
void main() {
auto S = readln.chomp.dup;
auto T = readln.chomp.dup;
auto findResult = iota(S.length-T.length+1).retro
.find!(i =>
iota(T.length)
.all!(j => S[i+j] == T[j] || S[i+j] == '?'));
if (findResult.length == 0) {
writeln("UNRESTORABLE");
return;
}
auto minPos = findResult[0];
S = S.replace("?", "a");
S[minPos..minPos+T.length] = T;
writeln(S);
}
|
D
|
import std.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv,
std.format,
std.math;
void main()
{
auto r = readln.chomp.to!int;
auto g = readln.chomp.to!int;
writeln(2*g-r);
}
|
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 N, M;
scan(N, M);
auto C = new ulong[](M);
auto A = new long[](M);
foreach (i; 0 .. M)
{
long a, b;
scan(a, b);
auto cs = aryread();
A[i] = a;
foreach (c; cs)
{
C[i] = C[i] | (1 << (c - 1));
}
}
auto dp = new long[][](M + 1, 1 << N);
foreach (i; 0 .. M + 1)
dp[i][] = long.max / 4;
dp[0][0] = 0;
foreach (i; 0 .. M)
foreach (ulong s; 0UL .. 1UL << N)
{
dp[i + 1][s] = dp[i + 1][s].min(dp[i][s]);
dp[i + 1][s | C[i]] = dp[i + 1][s | C[i]].min(dp[i][s] + A[i]);
}
writeln(dp[M][$ - 1] == long.max / 4 ? -1 : dp[M][$ - 1]);
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
int n, k;
int[] d;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto ar = readln().strip().split().map!(to!int).array();
n = ar[0];
k = ar[1];
d = readln().strip().split().map!(to!int).array();
}
}
auto main2(Input* input)
{
with (input)
{
bool[10] like;
like[] = true;
foreach (dd; d)
like[dd] = false;
int minLikeGte(int i)
{
for (; i < like.length; i++)
if (like[i])
return i;
return -1;
}
int[5] price;
price[] = -1;
auto temp = n;
for (int i = 4; i >= 0; i--)
{
price[i] = temp % 10;
temp /= 10;
}
int[5] payment;
payment[] = 0;
LOOP_KETA:
for (int keta = 0; keta < 5; keta++)
{
if (price[keta] == 0)
continue;
payment[keta] = minLikeGte(price[keta]);
if (payment[keta] == price[keta])
continue;
for (int kuriage = keta; kuriage >= 0; kuriage--)
{
auto newValue = minLikeGte(price[kuriage] + 1);
if (newValue != -1)
{
payment[kuriage] = newValue;
payment[kuriage+1..$] = minLikeGte(0);
break LOOP_KETA;
}
}
}
int result = payment[0];
for (size_t keta = 1; keta < 5; keta++)
{
result *= 10;
result += payment[keta];
}
return result;
}
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`1000 8
1 3 4 5 6 7 8 9`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 2000);
}
unittest // example2
{
string example =
`9999 1
0`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 9999);
}
unittest // example3
{
string example =
`1129 1
0 2 9`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 1131);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
alias retType = ReturnType!main2;
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
|
D
|
import std.stdio;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
void main(string[] argv)
{
long x = readln.chomp.to!long;
long a = readln.chomp.to!long;
long b = readln.chomp.to!long;
x -= a;
writeln(x%b);
}
|
D
|
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
auto X = readln.chomp.to!int;
auto Y = readln.chomp.to!int;
if(N>K){
writeln((K*X)+((N-K)*Y));
}else{
writeln(N*X);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
const H = 19;
const W = 15;
const INF = int.max / 2;
void main() {
auto F = new string[19];
foreach (ref L; F) L = readln.chomp;
int sy, sx;
void find_init_pos() {
foreach (i; 0 .. H) {
foreach (j; 0 .. W) {
if (F[i][j] == 'O') {
sy = i;
sx = j;
return;
}
}
}
}
find_init_pos();
if (! (F[H - 2].any!((c) => c == 'X') || F[H - 1].any!((c) => c == 'X'))) {
writeln(-1);
return;
}
bool[W][H] used;
bool dfs(int cy, int cx, int d) {
if (d == 0) return false;
//log([cy, cx, t, d]);
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dy == 0 && dx == 0) continue;
int y = cy + dy, x = cx + dx;
if (! (0 <= y && y < H && 0 <= x && x < W && F[y][x] == 'X' && !used[y][x])) continue;
while (0 <= y && y < H && 0 <= x && x < W && F[y][x] == 'X' && !used[y][x]) {
y += dy;
x += dx;
}
if (y == H - 1 && 0 <= x && x < W) return true;
if (y >= H) return true;
if (x < 0 || x >= W) continue;
y = cy + dy, x = cx + dx;
while (0 <= y && y < H && 0 <= x && x < W && F[y][x] == 'X' && !used[y][x]) {
used[y][x] = true;
y += dy;
x += dx;
}
if (dfs(y, x, d - 1)) return true;
y -= dy, x -= dx;
while (y != cy || x != cx) {
used[y][x] = false;
y -= dy;
x -= dx;
}
}
}
return false;
}
for (int d = 1; d <= 20; d++) {
if (dfs(sy, sx, d)) {
writeln(d);
return;
}
}
writeln(-1);
}
|
D
|
import std.stdio,std.ascii,std.conv,std.string,std.algorithm,std.range,std.functional,std.math,core.bitop;
void main()
{
auto a=readln.chomp.to!int;
auto b=readln.chomp.to!int;
writeln((a*a)-b);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
auto A = aryread();
foreach (i; 1 .. N)
{
A[i] += A[i - 1];
}
long ans = long.max;
foreach (B; [true, false])
{
long cnt;
long x;
foreach (i; 0 .. N)
{
if (B == (i & 1))
{
long tmp = (A[i] + x <= 0) ? abs(A[i] + x) + 1 : 0;
x += tmp;
cnt += tmp;
}
else
{
long tmp = (0 <= A[i] + x) ? abs(A[i] + x) + 1 : 0;
x -= tmp;
cnt += tmp;
}
}
ans = ans.min(cnt);
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
import std.string;
void main()
{
while (1) {
string[] input = split(readln());
int m = to!(int)(input[0]);
int f = to!(int)(input[1]);
int r = to!(int)(input[2]);
if ( m == -1 && f == -1 && r == -1) break;
if (r == -1) r = 0;
char g;
if (m == -1 || f == -1) g = 'F';
else if (m + f >= 80) g = 'A';
else if (m + f >= 65 && m + f < 80) g = 'B';
else if (m + f >= 50 && m + f < 65) g = 'C';
else if (m + f >= 30 && m + f < 50) {
if (r >= 50) g = 'C';
else g = 'D';
} else g = 'F';
writeln(g);
}
}
|
D
|
import std.container;
import std.range;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
import std.stdio;
import std.container;
void main() {
auto s = readln.chomp;
auto c = s.count!(x => x == 'R');
if (c == 2 && s[1] != 'R') {
writeln(1);
} else {
writeln(c);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
char[][] MAP;
MAP.length = H;
int s;
foreach (i; 0..H) {
MAP[i] = readln.chomp.to!(char[]);
foreach (c; MAP[i]) if (c == '#') ++s;
}
int x, y, t;
for (;;) {
++t;
if (x < W-1 && MAP[y][x+1] == '#') {
++x;
} else if (y < H-1 && MAP[y+1][x] == '#') {
++y;
} else {
break;
}
}
writeln(s == t ? "Possible" : "Impossible");
}
|
D
|
void main()
{
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1];
if (a + b == 15)
{
"+".writeln;
}
else if (a * b == 15)
{
"*".writeln;
}
else
{
"x".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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
writeln((a - 1) * (b - 1));
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
int odd, two, four;
foreach (a; A) {
if (a % 2 == 1) odd += 1;
else if (a % 4 == 0) four += 1;
else two += 1;
}
int mode = 0;
foreach (i; 0..N) {
if (mode == 0) {
if (odd > 0) odd -= 1, mode = 4;
else if (two > 0) two -= 1, mode = 2;
else four -= 1, mode = 0;
} else if (mode == 2) {
if (two > 0) two -= 1, mode = 2;
else if (four > 0) four -= 1, mode = 0;
else {writeln("No"); return;}
} else if (mode == 4) {
if (four > 0) four -= 1, mode = 0;
else {writeln("No"); return;}
}
}
writeln("Yes");
}
|
D
|
void main(){
int x = _scan();
( (x/500)*1000 + (x%500)/5*5).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
// 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()
{
auto input = aryread!string();
writeln(input[0].to!long + (input[1] == "+" ? 1 : -1) * input[2].to!long);
}
|
D
|
void main() {
auto N = ri;
auto S = rs;
int res = 0;
int tmp = 0;
foreach(i; S) {
i == 'I' ? tmp++ : tmp--;
res = max(res, tmp);
}
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.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;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto l = new long[](n+1);
l[0] = 2;
l[1] = 1;
foreach (i; 2..n+1) {
l[i] = l[i-1] + l[i-2];
}
l[n].writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int [] get (string s)
{
auto f = new int [10];
foreach (c; s)
{
f[c - '0']++;
}
f[2] += f[5];
f[5] = 0;
f[6] += f[9];
f[9] = 0;
return f;
}
void main ()
{
string s;
while ((s = readln ().strip ()) != null)
{
string t = readln ().strip ();
auto f = get (s);
auto g = get (t);
int res = int.max;
foreach (i; 0..10)
{
if (f[i] > 0)
{
res = min (res, g[i] / f[i]);
}
}
writeln (res);
}
}
|
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,m;
scan(n);
scan(m);
int ans;
if (n > 30) {
ans = m;
}
else {
ans = m % 2^^n;
}
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.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD;
auto m = RD;
auto q = RD!int;
auto g = gcd(n, m);
auto l1 = n / g;
auto l2 = m / g;
auto ans = new bool[](q);
foreach (i; 0..q)
{
auto sx = RD;
auto sy = RD-1;
auto ex = RD;
auto ey = RD-1;
long pos1, pos2;
if (sx == 1)
pos1 = sy / l1;
else
pos1 = sy / l2;
if (ex == 1)
pos2 = ey / l1;
else
pos2 = ey / l2;
ans[i] = pos1 == pos2;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const numCases = readInt;
foreach (caseId; 0 .. numCases) {
const N = readInt;
const S = readToken;
char[] ans;
if (N >= 2 && S[0] == S[1]) {
ans ~= S[0];
ans ~= S[0];
} else {
char[] cs;
foreach (i; 0 .. N) {
if (i > 0 && S[i - 1] < S[i]) {
break;
}
cs ~= S[i];
}
ans ~= cs;
cs.reverse;
ans ~= cs;
}
writeln(ans);
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
long cnt;
foreach (i; 0..n)
{
cnt += a[i];
if (cnt < 0)
{
ans[ti] += abs(cnt);
cnt = 0;
}
}
}
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; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto S = RD!string;
int p;
foreach_reverse (i; 0..n)
{
if (S[i] != ')')
{
p = i+1;
break;
}
}
ans[ti] = p*2 < n;
}
foreach (e; ans)
writeln(e ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
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;
int test () {
auto s = readln.strip;
auto t = readln.strip;
immutable l = s.length.to!int;
auto next = new int[26][l+1];
next[l][] = l;
foreach_reverse (i; 0 .. l) {
next[i][] = next[i+1][];
immutable k = s[i].to!int - 97;
next[i][k] = i;
}
foreach (c; t) {
immutable k = c.to!int - 97;
if (next[0][k] >= l) return -1;
}
int i, res;
foreach (c; t) {
immutable k = c.to!int - 97;
while (true) {
i = next[i][k];
if (i >= l) {
++res;
i = 0;
} else {
break;
}
}
++i;
}
res++;
return res;
}
void main() {
immutable nt = readln.strip.to!int;
foreach (tid; 0 .. nt) {
writeln (test ());
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const A = readLong();
const B = readLong();
const N = readLong();
long ans;
long a = A, b = B;
for (; a <= N && b <= N; ) {
if (a > b) {
swap(a, b);
}
a += b;
++ans;
}
writeln(ans);
}
}
} catch (EOFException e) {
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, m, q; rd(n, m, q);
auto s=readln.chomp.to!(char[]);
auto t=readln.chomp.to!(char[]);
auto ok=new bool[](s.length+1);
for(int i=0; i+t.length<=s.length; i++){
if(s[i..(i+t.length)]==t) ok[i]=true;
}
while(q--){
int l, r; rd(l, r);
l--; r--;
int w=0;
for(int i=l; i<=r; i++){
if(ok[i] && i+t.length-1<=r) w++;
}
writeln(w);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
readln.strip.length.writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
string s;
scan(s);
int n = s.length.to!int;
long ans;
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n) {
foreach (k ; j + 1 .. n) {
if (s[i] == 'Q' && s[j] == 'A' && s[k] == 'Q') 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);
}
}
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this(int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head = (head + 1) % N;
}
void clear() {
head = tail = 0;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto a = readln.split.to!(long[]);
auto N = a[0];
auto A = a[1];
auto B = a[2];
auto C = a[3];
auto D = a[4];
foreach (i; 0..N-1) {
auto l = A + C * i - D * (N-i-1);
auto r = A + D * i - C * (N-i-1);
if (l <= B && B <= r) {
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
void main() {
auto r = ri;
writeln(r*r);
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
class Node {
Node[] to;
Node[] from;
int n;
}
void main() {
int m = to!int(readln().strip());
int n = to!int(readln().strip());
auto nodes = new Node[m];
foreach(i; 0..m) {
nodes[i] = new Node;
nodes[i].n = i+1;
}
foreach(i; 0..n) {
auto s = map!(a => to!int(a)-1)(readln().strip().split());
nodes[s[0]].to ~= nodes[s[1]];
nodes[s[1]].from ~= nodes[s[0]];
}
Node[] S;
foreach(node; nodes) {
if(node.from.empty())
S ~= node;
}
Node[] L;
while(!S.empty()) {
L ~= S.front;
S.popFrontN(1);
foreach(node; L.back.to) {
node.from = remove!(a => a == L.back)(node.from);
if(node.from.empty())
S ~= node;
}
}
foreach(node; L) {
writeln(node.n);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
writeln(max(A+B, A-B, A*B));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new int[][](t, 26);
foreach (i; 0..t)
{
auto s = RD!string;
long cnt = 1;
char last = s[0];
foreach (j; 1..s.length)
{
if (s[j] == last)
{
++cnt;
}
else
{
if (cnt % 2 == 1)
{
ans[i][last-'a'] = 1;
}
cnt = 1;
last = s[j];
}
}
if (cnt % 2 == 1)
{
ans[i][last-'a'] = 1;
}
}
foreach (e; ans)
{
foreach (i; 0..e.length)
{
if (e[i] == 1)
write(cast(char)('a'+i));
}
writeln();
}
stdout.flush();
debug readln();
}
|
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 S = readln.chomp;
auto DP = new long[][](S.length+1, 4);
DP[S.length][3] = 1;
foreach_reverse (i; 0..S.length) {
auto c = S[i];
foreach (j; 0..4) {
if (c == 'A' || c == '?') {
DP[i][j] = (DP[i][j] + DP[i+1][j]) % P;
if (j == 0) DP[i][j] = (DP[i][j] + DP[i+1][j+1]) % P;
}
if (c == 'B' || c == '?') {
DP[i][j] = (DP[i][j] + DP[i+1][j]) % P;
if (j == 1) DP[i][j] = (DP[i][j] + DP[i+1][j+1]) % P;
}
if (c == 'C' || c == '?') {
DP[i][j] = (DP[i][j] + DP[i+1][j]) % P;
if (j == 2) DP[i][j] = (DP[i][j] + DP[i+1][j+1]) % P;
}
}
}
writeln(DP[0][0]);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Cake = Tuple!(long, "beauty", long, "tasty", long, "popular");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto r = lread();
(3 * (r ^^ 2)).writeln();
}
|
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 N = lread();
auto A = aryread();
auto B = aryread();
long a, b;
foreach (i; 0 .. N)
{
a += max((B[i] - A[i]) / 2, 0);
b += max(A[i] - B[i], 0);
}
writeln(a < b ? "No" : "Yes");
}
|
D
|
void main() {
problem();
}
void problem() {
auto X = scan!int(5);
int solve() {
foreach(i; 0..5) {
if (X[i] == 0) return i+1;
}
return 0;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio;
import std.string;
void main()
{
auto S = readln().chomp();
if ((S == "AAA") || (S == "BBB"))
{
writeln("No");
return;
}
writeln("Yes");
}
|
D
|
void main() {
int n = readln.chomp.to!int;
string[] w = new string[n];
int ok = true;
foreach (i; 0 .. n) {
string s = readln.chomp;
if (i > 0) {
if (s[0] != w[i-1][$-1] || w.canFind(s)) {
ok = false;
break;
}
}
w[i] = s;
}
writeln(ok ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
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;
alias Edge = Tuple!(int, "u", int, "v");
void main() {
int n, k;
scan(n, k);
auto es = new Edge[](n - 1);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n - 1) {
int u, v;
scan(u, v);
u--, v--;
adj[u] ~= v;
adj[v] ~= u;
es[i] = Edge(u, v);
}
int dfs(int v, int p, int dep, int lim) {
int res = dep > lim;
foreach (u ; adj[v]) if (u != p) {
res += dfs(u, v, dep + 1, lim);
}
return res;
}
int ans = n;
if (k & 1) {
foreach (e ; es) {
int u = e.u;
int v = e.v;
ans = min(ans, dfs(u, v, 0, k / 2) + dfs(v, u, 0, k / 2));
}
}
else {
foreach (v ; 0 .. n) {
ans = min(ans, dfs(v, -1, 0, k / 2));
}
}
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.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;
immutable long MOD = 10^^9 + 7;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto A = readln.split.map!(to!int).array;
auto dp = new long[][](N+1, K+10);
dp[0][K] = 1;
foreach (i; 0..N) {
foreach (j; 0..K+1) {
(dp[i+1][max(0, j-A[i])] += dp[i][j]) %= MOD;
(dp[i+1][j+1] -= dp[i][j]) %= MOD;
}
foreach (j; 0..K+1) {
(dp[i+1][j+1] += dp[i+1][j]) %= MOD;
}
}
long ans = (dp[N][0] + MOD) % MOD;
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
string[3] s;
scan(s[0]);
scan(s[1]);
scan(s[2]);
long[3] n;
long p;
while (n[p] < s[p].length)
{
auto x = p;
p = s[x][n[x]] - 'a';
n[x]++;
}
writeln("ABC"[p]);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
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;
}
}
long apple(long a)
{
return a * 3;
}
void main()
{
long a, p;
scan(a, p);
p += apple(a);
writeln(p / 2);
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long[] op = [1];
foreach (i; 0 .. n)
{
if (6 ^^ (i + 1) <= n)
{
op ~= 6 ^^ (i + 1);
}
if (9 ^^ (i + 1) <= n)
{
op ~= 9 ^^ (i + 1);
}
if (6 ^^ (i + 1) > n)
{
break;
}
}
op.sort!"a < b";
long m = op.length;
long[] dp = new long[n+1];
dp[] = long.max;
dp[0] = 0;
foreach (x; op)
{
foreach (i; x .. n+1)
{
dp[i] = min(dp[i], dp[i-x]+1);
}
}
dp[n].writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
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(){
int n = scan!int;
int[] as = scan!int(n);
int amax = as[0];
foreach(a; as) amax.raiseTo(a);
int[] acnt = new int[](amax + 1);
foreach(a; as) acnt[a] += 1;
string ans = "Possible";
foreach(a, cnt; acnt){
if(a * 2 < amax){
if(cnt > 0) ans = "Impossible";
}
if(a * 2 == amax){
if(cnt != 1) ans = "Impossible";
}
if(a * 2 == amax + 1){
if(cnt != 2) ans = "Impossible";
}
if(a * 2 > amax + 1){
if(cnt <= 1) ans = "Impossible";
}
}
ans.writeln;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2];
writeln(min(c, b/a));
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
import std.array;
import std.math;
import std.range;
import std.typecons;
void main()
{
for(string[] inputs; (inputs = readln.split)[0] != "0";)
writeln( solve( inputs[0].to!int, inputs[1].to!int ) );
}
int[][] G, D;
bool[][][] M;
int solve(int n, int m)
{
G = new int[][](n, n);
D = new int[][](n, 2);
M = new bool[][][](n, n, 2);
foreach(i; 0..n){
foreach(j; 0..n){
G[i][j] = (i == j ? 0 : int.max / 2);
M[i][j][0] = M[i][j][1] = false;
}
D[i][0] = D[i][1] = int.max / 2;
}
foreach(_; 0..m){
auto inputs = readln.split;
int a = inputs[0].to!int, b = inputs[1].to!int, c = inputs[2].to!int;
G[a - 1][b - 1] = c;
G[b - 1][a - 1] = c;
M[a - 1][b - 1][0] = true;
M[b - 1][a - 1][0] = true;
}
foreach(i; 0..n)
foreach(j; 0..n)
foreach(k; 0..n)
if(M[i][k][0] && M[k][j][0])
M[i][j][1] = true;
dfs(n, 0, 1, 0);
return min(D[n - 1][0], D[n - 1][1]);
}
void dfs(int n, int now, int magic, int cost)
{
if(D[now][magic] <= cost) return;
D[now][magic] = cost;
foreach(next; 0..n){
if(M[now][next][0]) dfs(n, next, magic, cost + G[now][next]);
if(M[now][next][1] && 0 < magic) dfs(n, next, magic - 1, cost);
}
}
/*
現在いる場所、そこまでの費用、特別乗車券を使ったかどうかで最小値を更新するDFSをします。
特別乗車券を使えるのはi,jの間にパス2の経路が存在する場合だけなので、それをMに記録していきます。
あるkを選択する時iからk、kからjまでの経路が存在すれば特別乗車券が使えます。
*/
/*
2 1
1 2 5
3 2
1 2 5
2 3 5
6 9
1 2 7
1 3 9
1 5 14
2 3 10
2 4 15
3 4 11
3 5 2
4 5 9
4 6 8
0 0
*/
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto N = RD!string;
long cnt;
foreach (c; N)
{
cnt += c-'0';
}
writeln(cnt % 9 == 0 ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto x = readln.chomp.split.to!(int[]);
auto a = new int[](3);
a[0] = x[0] * x[1];
a[1] = x[0] + x[1];
a[2] = x[0] - x[1];
writeln(max(a[2],max(a[0], a[1])));
}
|
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;
import std.typecons;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
final T[] nextA(T) (int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
//alias T = Tuple!(int, int, int);
alias T = Tuple!(int, int);
void main() {
auto r = new InputReader;
immutable n = r.next!int;
T[] t1, t2;
foreach (i; 1 .. n + 1) {
int u = r.next!int;
int v = r.next!int;
//auto t = tuple (r.next!int, r.next!int);
if (u < v) {
//t1 ~= tuple (u, v, i);
t1 ~= tuple (v, i);
} else {
//t2 ~= tuple (u, v, i);
t2 ~= tuple (u, i);
}
}
if (t1.length >= t2.length) {
t1.sort ();
writeln (t1.length);
bool f = true;
foreach_reverse (u; t1) {
if (f) f = false; else write (' ');
write (u[1]);
}
writeln;
} else {
writeln (t2.length);
t2.sort ();
bool f = true;
foreach (u; t2) {
if (f) f = false; else write (' ');
write (u[1]);
}
writeln;
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.