code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container, std.typecons;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str) { return str.split.to!(T[]); }
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 mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
void main()
{
auto N = RD!long;
auto M = RD!long;
auto cnt = new long[](M+1);
foreach (i; 0..N)
{
auto K = RD!long;
foreach (j; 0..K)
{
auto A = RD!long;
++cnt[A];
}
}
long ans;
foreach (e; cnt)
{
if (e == N) ++ans;
}
writeln(ans);
stdout.flush();
}
|
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 A, B;
scan(A, B);
writeln(A * B);
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
void main() {
foreach (char[] line; stdin.lines) {
if (line == "-\n") break;
char[] s = line.chomp;
int i = readln.chomp.to!int;
for (int x = 0; x < i; x++) {
int n = readln.chomp.to!int;
s = s[n..$] ~ s[0..n];
}
s.writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
void main() {
auto s = readln.chomp;
(s.lastIndexOf('Z') - s.indexOf('A') + 1).writeln;
}
|
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;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
int dayInMonth(int y, int m) {
if(y % 3 == 0) {
return 20;
}
if(m%2 == 0) {
return 19;
}
return 20;
}
void main(){
auto n = readInt();
for(int i; i < n; ++i) {
auto ymd = readInts();
auto y = ymd[0];
auto m = ymd[1];
auto d = ymd[2];
int ans;
ans += dayInMonth(y, m) + 1 - d;
d = 1;
m += 1;
if(m > 10) {
y += 1;
m = 1;
}
while(y < 1000) {
ans += dayInMonth(y, m);
m += 1;
if(m > 10) {
y += 1;
m = 1;
}
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
auto x = readln.chomp.to!ulong;
auto ans = x / 11;
x -= ans * 11;
ans *= 2;
if (0 < x && x <= 6) ans += 1;
else if (x > 6) ans += 2;
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
import std.array;
import std.conv;
ubyte toNum(dchar e)
{
if('a' <= e && e <= 'z')
return cast(ubyte)(e - 'a');
else
return cast(ubyte)(e - 'A' + 26);
}
char toChar(ubyte u)
{
if(u < 26)
return cast(char)('a' + u);
else
return cast(char)('A' + u - 26);
}
void main()
{
while(1){
uint N = readln().chomp().to!uint;
if(N == 0)
break;
auto kc = array(readln().chomp().split().map!(to!uint)).cycle;
auto st = array(readln().chomp().map!toNum);
foreach(i, ubyte e; st){
e = (e + 52 - kc[i]) % 52;
write(toChar(e));
}
writeln();
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto ss = new bool[](N);
foreach (_; 0..K) {
readln;
foreach (i; readln.split.to!(int[])) ss[i-1] = true;
}
int r;
foreach (s; ss) if (!s) ++r;
writeln(r);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
string calc(string s, string t) {
bool match(int start) {
for (int i = 0; i < t.length; i++) {
if (s[start + i] == '?')
continue;
if (s[start + i] != t[i])
return false;
}
return true;
}
for (int i = cast(int)(s.length - t.length); i >= 0; i--) {
if (match(i)) {
auto cs = new char[s.length];
for (int j = 0; j < cs.length; j++) {
cs[j] = s[j] == '?' ? 'a' : s[j];
}
for (int j = 0; j < t.length; j++) {
cs[i + j] = t[j];
}
return to!string(cs);
}
}
return "UNRESTORABLE";
}
void main() {
string s = readln.chomp;
string t = readln.chomp;
auto ans = calc(s, t);
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int[] s; readA(3, s);
s.sort();
writeln(s.equal([5, 5, 7]) ? "YES" : "NO");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
int n;
scanf("%d\n", &n);
auto s = readln.chomp;
auto cache = '\n';
size_t cnt;
foreach(c; s) {
if (c != cache) {
cache = c;
++cnt;
}
}
cnt.write;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto X=readln.chomp.to!int;
auto A=readln.chomp.to!int;
auto B=readln.chomp.to!int;
writeln((X-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.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto n = RD!int;
auto q = RD!int;
auto k = RD!int;
auto a = RDA;
auto w = new long[](n);
a = 0L ~ a ~ (k+1L);
foreach (i; 0..n)
{
w[i] += a[i+1] - a[i] - 1;
w[i] += a[i+2] - a[i+1] - 1;
}
auto ww = new long[](n+1);
foreach (i; 0..n)
{
ww[i+1] = ww[i] + w[i];
}
debug writeln("ww:", ww);
auto ans = new long[](q);
foreach (i; 0..q)
{
auto l = RD!int-1;
auto r = RD!int-1;
ans[i] = a[l] + a[$-1] - a[r+2];
debug writeln("a[l]:", a[l] , " a[r+2]:", a[r+2] );
ans[i] += ww[r+1] - ww[l];
debug writeln("ww[r]:", ww[r+1], " ww[l]:", ww[l]);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto a = scanElem;
(a*a-scanElem).writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int t;
scan(t);
while (t--) {
int a, b, c;
scan(a, b, c);
auto ans = solve(a, b, c);
writeln(ans);
}
}
int solve(int a, int b, int c) {
int res;
foreach (comb ; 0 .. 1 << (1 << 3)) {
auto x = new int[](3);
foreach (i ; 0 .. 1 << 3) if (comb & 1 << i) {
foreach (j ; 0 .. 3) if (i & 1 << j) {
x[j]++;
}
}
if (x[0] <= a && x[1] <= b && x[2] <= c) {
chmax(res, comb.popcnt);
}
}
res--;
return res;
}
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
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
11
---+/
/+---test
136
---+/
/+---test
100000
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
const M = N.log10.to!long;
long ans;
if (M%2 == 0) ans += N - 10.pow(M) + 1;
foreach (i; 0..M) {
if (i%2 == 0) {
ans += 10.pow(i+1) - 10.pow(i);
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(ulong[]);
ulong x;
size_t i, j;
long r;
while (j < N) {
if (j < N && !(x&AS[j])) {
x |= AS[j];
r += j-i+1;
++j;
} else {
do {
x ^= AS[i++];
} while (i < N && x&AS[j]);
x |= AS[j];
r += j-i+1;
++j;
}
}
writeln(r);
}
|
D
|
void main() {
problem();
}
void problem() {
auto a = scan;
long solve() {
long ans;
long count;
foreach(c; a) {
if (c == 'R') {
count++;
if (ans < count) ans = count;
} else {
count = 0;
}
}
return ans;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import 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 H = RD;
auto W = RD;
auto A = new string[](H);
foreach (i; 0..H)
{
A[i] = RD!string;
}
bool check(long y, long x)
{
long cnt_p, cnt_n;
if (y == 0 && x == 0) cnt_p = 1;
if (y == H-1 && x == W-1) cnt_n = 1;
if (y != 0)
{
if (A[y-1][x] == '#') ++cnt_p;
}
if (x != 0)
{
if (A[y][x-1] == '#') ++cnt_p;
}
if (y != H-1)
{
if (A[y+1][x] == '#') ++cnt_n;
}
if (x != W-1)
{
if (A[y][x+1] == '#') ++cnt_n;
}
return cnt_p == 1 && cnt_n == 1;
}
bool ans = true;
foreach (y; 0..H)
{
foreach (x; 0..W)
{
if (A[y][x] == '#')
{
auto r = check(y, x);
if (!r)
ans = false;
}
}
}
writeln(ans ? "Possible" : "Impossible");
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (deq.length && deq[0] == i-m) {
deq.removeFront();
}
if (deq.length) {
ndp[i] = dp[deq[0]] + 1L * ph * a[i];
}
while (deq.length && dp[deq.back] <= dp[i]) {
deq.removeBack();
}
deq.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool mayNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload* p;
private void I() { if (mayNull && !p) p = new Payload(); }
private void C() const {
version(assert) if (mayNull && !p) throw new RangeError();
}
static if (!mayNull) {
@disable this();
}
//some value
private this(Payload* p) {
this.p = p;
}
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() { return Deque(new Payload()); }
@property bool havePayload() const { return (!mayNull || p); }
@property bool empty() const { return (!havePayload || p.empty); }
@property size_t length() const { return (havePayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.algorithm;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
pure ulong modComb(ulong n, ulong r, ulong m) {
if (n == r) return 1;
if (r == 0) return 1;
if (n < r) return 0;
ulong up = 1;
ulong down = 1;
for(ulong i = n-r+1; i <= n; i++) {
up *= i;
up %= m;
}
for(ulong i = 2; i <= r; i++) {
down *= i;
down %= m;
}
return up*modInv(down, m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong n, k;
readInto(n, k);
for (ulong i = 1; i <= k; i++) {
writeln(modComb(n-k+1, i, MOD)*modComb(k-1, i-1, MOD) % MOD);
}
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv;
void main(){
iota(1, 10).map!(a => iota(1, 10).map!(b => text(a, "x", b, "=", a*b)).join("\n")).join("\n").writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto ab = readln.split.to!(int[]);
writeln(ab[0] + ab[1] == 15 ? "+" : ab[0] * ab[1] == 15 ? "*" : "x");
}
|
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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
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 char[][][](t);
foreach (ti; 0..t)
{
auto h = RD!int;
auto w = RD!int;
ans[ti].length = h;
foreach (i; 0..h)
{
ans[ti][i].length = w;
foreach (j; 0..w)
{
ans[ti][i][j] = '0';
}
}
foreach (i; 0..w)
{
if (i % 2 == 0)
{
ans[ti][0][i] = '1';
ans[ti][h-1][i] = '1';
}
}
foreach (i; 2..h-2)
{
if (i % 2 == 0)
{
ans[ti][i][0] = '1';
ans[ti][i][w-1] = '1';
}
}
}
foreach (e; ans)
{
foreach (ee; e)
writeln(ee);
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto X = readln.chomp.to!long;
long r;
r += X/500 * 1000;
X -= X/500 * 500;
r += X/5 * 5;
writeln(r);
}
|
D
|
//aoj10018
import std.stdio;
import std.ascii;
void main()
{
char c;
dchar c1;
while(1)
{
scanf("%c", &c);
if(c == '\n') break;
if(isLower(c) != 0) c1 = toUpper(c);
else if(isUpper(c) != 0) c1 = toLower(c);
else c1 = c;
write(c1);
}
writeln();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
auto AS = readln.split.to!(long[]);
int[60] bs;
foreach (a; AS) {
foreach (i; 0..60) if (a & (1L<<i)) bs[60-i-1] += 1;
}
long X;
bool u;
foreach (i; 0..60) {
X <<= 1;
auto j = 60-i-1;
if (bs[i] <= N/2) {
if ((K & (1L<<j)) || u) X += 1;
} else {
if (K & (1L<<j)) u = true;
}
}
long r;
foreach (a; AS) r += a^X;
writeln(r);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
long powmod(long a, long x, long m) {
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
void main() {
immutable long MOD = 10^^9 + 7;
auto S = readln.chomp;
auto N = S.length.to!int;
long a = 0;
long b = 0;
long ans = 0;
char prev = 'x';
foreach (i; 0..N) {
if (S[i] == 'a' && prev == 'b') {
ans += b * (powmod(2, a, MOD) - 1) % MOD;
ans = (ans + MOD) % MOD;
a++;
b = 0;
}
else if (S[i] == 'a') {
a++;
}
else {
b++;
}
prev = S[i];
}
ans += b * (powmod(2, a, MOD) - 1) % MOD;
ans = (ans + MOD) % MOD;
ans.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(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
/*
If you find s_i > s_(i + 1), swap them.
Othewise, NO.
*/
void solve(){
int n = read!int;
string s = readln.chomp;
int i1;
bool f = 0;
foreach(i; 0 .. n - 1){
if(s[i] > s[i + 1]){
i1 = i;
f = 1;
break;
}
}
if(f == 0) writeln("NO");
else writeln("YES"), writeln(i1 + 1, " ", i1 + 2);
}
|
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();
if (N == 1) {
writeln(-1);
} else {
write(8);
foreach (i; 1 .. N) {
write(9);
}
writeln();
}
}
}
} catch (EOFException e) {
}
}
|
D
|
// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.algorithm : sort, sum;
import std.array : split;
import std.conv : to;
import std.stdio : readln, writeln;
import std.string : chomp;
auto gets() { return readln.chomp; }
auto getV(T)() { return readln.chomp.to!T; }
void main() {
auto s = gets;
auto k = getV!int;
if (s.length < k) {
writeln("impossible");
return;
}
int[char] tb;
foreach (ch; s) {
tb[ch]++;
}
if (tb.length >= k) {
writeln(0);
return;
}
if (tb.length < k) {
writeln(k - tb.length);
return;
}
writeln(tb.values.sort[0..$-k].sum);
}
|
D
|
import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,
std.container, std.typecons, std.conv, std.random, std.bigint, std.string;
void read(S...)(ref S args) {
auto input = readln.split;
assert(input.length == args.length);
foreach (i, ref arg; args) {
arg = input[i].to!(S[i]);
}
}
auto list(T)() { return readln.split.map!(e => e.to!T).array; }
alias Ath = int[];
bool sup(Ath a, Ath b) {
int t = 0;
foreach (i; 0 .. 5) {
if (a[i] < b[i]) t++;
}
return t >= 3;
}
void main() {
int T;
read(T);
while (T--) {
int n;
read(n);
Ath[] arr;
foreach (i; 0 .. n) {
arr ~= list!int;
}
auto rem = iota(n).array;
while (rem.length != 1) {
int[] rem2;
for (int i = 0; i < rem.length; i += 2) {
if (i == rem.length - 1) {
rem2 ~= rem[i];
} else {
if (sup(arr[rem[i]], arr[rem[i + 1]])) {
rem2 ~= rem[i];
} else {
rem2 ~= rem[i + 1];
}
}
}
rem = rem2;
}
int target = rem.front;
bool yes = true;
foreach (i, a; arr) {
if (i != target && !sup(arr[target], a)) {
yes = false;
}
}
if (!yes) {
writeln(-1);
} else {
writeln(target + 1);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static 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; }
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 q = RD!int;
auto ans = new bool[](q);
foreach (i; 0..q)
{
auto a = RD;
auto b = RD;
auto n = RD;
auto S = RD;
auto x = S / n;
S -= min(x, a) * n;
ans[i] = S <= b;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math;
immutable int mod = 10^^9 + 7;
int x1, y1, x2, y2;
int x, y;
void main() {
readVariables(x1, y1, x2, y2);
readVariables(x, y);
if ((x2 - x1) % x || (y2 - y1) % y) {
writeln("NO");
return;
}
int k = abs(x2 - x1) / x;
int h = abs(y2 - y1) / y;
if ((k & 1) == (h & 1)) {
writeln("YES");
}
else {
writeln("NO");
}
}
void readVariables(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg ; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
if (!line.empty) {
throw new Exception("args num < input num");
}
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.4.0"
+/
import std.stdio, std.algorithm, std.conv, std.range;
import std.typecons;
// import dcomp.scanner, dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
alias E = Tuple!(int, "to");
int n;
sc.read(n);
E[][] g = new E[][2*n-1];
foreach (i; 0..n-1) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= E(n+i);
g[n+i] ~= E(a);
g[b] ~= E(n+i);
g[n+i] ~= E(b);
}
string s;
sc.read(s);
n = 2*n-1;
int[2] searchMid() {
int[] rt = new int[n];
int[2] lng;
void dfs(int p, int b, int dps = 0) {
rt[p] = b;
if (lng[1] < dps) {
lng = [p, dps];
}
foreach (e; g[p]) {
int d = e.to;
if (d == b) continue;
dfs(d, p, dps+1);
}
}
lng = [-1, -1];
dfs(0, -1);
int v = lng[0];
lng = [-1, -1];
dfs(v, -1);
int[] path;
int u = lng[0];
while (u != v) {
path ~= u;
u = rt[u];
}
path ~= v;
// writeln(path);
return [path[path.length/2], path.length.to!int/2];
}
int[2] mid = searchMid();
// writeln(mid);
bool[] bc = new bool[n];
bool[] bsm = new bool[n];
int[] md = new int[n];
void dfs(int p, int b) {
if (p < (n+1)/2 && s[p] == '1') bc[p] = true;
bsm[p] = bc[p];
md[p] = 0;
foreach (e; g[p]) {
int d = e.to;
if (d == b) continue;
dfs(d, p);
bsm[p] |= bsm[d];
md[p] = max(md[p], md[d]+1);
}
}
dfs(mid[0], -1);
/* writeln(bc);
writeln(bsm);
writeln(md);*/
long ans = 0;
int calc(int p, int b) {
int mi = 10^^9;
foreach (e; g[p]) {
int d = e.to;
if (d == b) continue;
mi = min(mi, 1+calc(d, p));
}
int di = md[p];
if (bc[p]) {
ans += di/2+1;
return di;
} else {
if (mi < 10^^8) {
ans += (di-mi)/2+1;
return di;
}
return mi;
}
}
calc(mid[0], -1);
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(4L, 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (!p) p = new Payload(); }
private void C() const { assert(p, "this deque is not init"); }
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
@property bool empty() const { return (!p || p.empty); }
@property size_t length() const { return (p ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
string[] buf;
private bool succ() {
while (!buf.length) {
if (f.eof) return false;
buf = f.readln.split;
}
return true;
}
private bool readSingle(T)(ref T x) {
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
x = buf.front;
buf.popFront;
} else {
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf.map!(to!E).array;
buf.length = 0;
}
} else {
x = buf.front.to!T;
buf.popFront;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
writeln(readln.chomp.to!int^^2);
}
|
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;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
void main() {
string str = readln.chomp;
foreach(i; 0..str.length-1) {
if (str[i..i+2] == "AC") {
"Yes".writeln;
return;
}
}
"No".writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
import std.bigint;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto nk = readNums!long;
auto a = readNums!int;
auto visited = new int[](cast(int)nk[0]);
int now = 1, count = 1;
int loop_count;
visited[0] = 1;
while(true){
count++;
now = a[now-1];
if(visited[now-1] > 0){
loop_count = count - visited[now-1];
break;
} else {
visited[now-1] = count;
}
}
long move = (nk[1] - visited[now-1] + 1) % loop_count;
if(nk[1] - visited[now-1] + 1 <= 0){
move = nk[1];
now = 1;
}
foreach(i; 0 .. move){
now = a[now-1];
}
writeln(now);
}
|
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(){
auto a = readNums!int;
max(a[0]+a[1], a[0]-a[1], a[0]*a[1]).writeln;
}
|
D
|
unittest
{
assert( [ "?ABC" ].parse.expand.solve == 4 ); // 追加ケース
assert( [ "ABC?" ].parse.expand.solve == 4 ); // 追加ケース
assert( [ "A??C" ].parse.expand.solve == 8 );
assert( [ "ABCBC" ].parse.expand.solve == 3 );
assert( [ "????C?????B??????A???????" ].parse.expand.solve == 979596887 );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
const MOD = 10L ^^ 9 + 7;
auto dp = new long[][]( 4 + 1, s.length + 1 );
dp[ 0 ][ 0 ] = 1;
for( auto ci = 0L, c = 'A'; ci + 1 < dp.length; ci++, c++ )
{
for( auto si = 0L; si + 1 < dp[ ci ].length; si++ )
{
if( s[ si ] == c )
{
dp[ ci ][ si + 1 ] += dp[ ci ][ si ]; // cを選択しないパターン
dp[ ci + 1 ][ si + 1 ] += dp[ ci ][ si ]; // cを選択するパターン
}
else if( s[ si ] == '?' )
{
dp[ ci ][ si + 1 ] += dp[ ci ][ si ] * 3; // cを選択しないパターン(ここまでのパターン×3通りになる)
dp[ ci + 1 ][ si + 1 ] += dp[ ci ][ si ]; // cを選択するパターン
}
else
{
dp[ ci ][ si + 1 ] += dp[ ci ][ si ]; // cを選択しないパターン(cが無かったパターン)
}
dp[ ci ][ si + 1 ] %= MOD;
dp[ ci + 1 ][ si + 1 ] %= MOD;
}
}
return dp[ 3 ][ $ - 1 ];
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto a = readln.split.to!(int[]);
auto b = readln.split.to!(int[]);
long x, y;
foreach (i ; 0 .. N) {
if (a[i] >= b[i]) {
x += a[i] - b[i];
}
else {
y += (b[i] - a[i]) / 2;
}
}
yes(x <= y);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int[] x = cin.nextArray!int(5);
foreach (i; 1 .. 6) {
if (i != x[i - 1]) {
writeln(i);
break;
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array;
void main() {
string s = readln.chomp;
if(s[0]==s[1] && s[1]==s[2]) writeln("No");
else writeln("Yes");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
string[100] WS;
bool[string] MEMO;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
WS[i] = readln.chomp;
}
char c = WS[0][0];
foreach (w; WS[0..N]) {
if (c != w[0] || w in MEMO) {
writeln("No");
return;
}
MEMO[w] = true;
c = w[$-1];
}
writeln("Yes");
}
|
D
|
import std.stdio; // readln
import std.array; // split
import std.conv; // to
import std.typecons;
import std.range;
import std.algorithm;
void main(){
string s = readln();
int cnt = 0;
foreach (elem; s) {
if(elem == '1') cnt++;
}
writeln(cnt);
}
|
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 int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
if (n % 2)
{
auto loop = n / 2;
auto x = (k-1) / loop;
ans[ti] = (k-1+x) % n + 1;
}
else
{
ans[ti] = (k-1) % n + 1;
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
auto R = new int[](N-1);
foreach (d; 1..N) {
auto end = true;
auto s = true;
foreach (ref x; R) {
if (x == 0) {
end = false;
if (s) {
s = false;
x = d;
} else {
s = true;
}
}
}
if (end) break;
}
foreach (i; 1..N) writeln(R[0..N-i].to!(string[]).join(" "));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
void main()
{
while (1) {
auto x = readln.chomp.to!int;
if (!x) break;
int cnt;
while (x != 1) {
if (x % 2) x = x * 3 + 1;
else x /= 2;
cnt++;
}
cnt.writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
int i = 1;
while(1){
int a = readln().chomp().to!int();
if(a == 0)
break;
writeln("Case ", i, ": ", a);
i++;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto AS = readln.split.to!(long[]);
long r = 1;
foreach (i, a; AS) {
if (i < K) continue;
writeln(a > AS[i-K] ? "Yes" : "No");
}
}
|
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[]);
writeln(max(ab[0] + ab[1], ab[0] - ab[1], ab[0] * ab[1]));
}
|
D
|
import std.stdio, std.math;
void main(){
int n, m;
immutable mod = cast(long)(1e9 + 7);
scanf("%d%d", &n, &m);
auto Cs = new int[][](m,2), idxToC = new int[][](n+1, 2);
auto appear = new int[](n+1);
foreach(i; 0..m){
scanf("%d%d", &Cs[i][0], &Cs[i][1]);
foreach(j; 0..2){
int x = abs(Cs[i][j]);
idxToC[x][++appear[x]-1] = i;
}
}
int getAnother(int x, int c){
return idxToC[x][idxToC[x][0] == c ? 1 : 0];
}
long ans = 1;
auto visited = new bool[](m);
//single
foreach(i; 0..m)if(!visited[i]){
if( abs(Cs[i][0]) == abs(Cs[i][1]) ){
visited[i] = true;
ans = ans * (Cs[i][0] == Cs[i][1] ? 1 : 2) % mod;
}
}
void calcDp(int len, long[][] dp, bool[] same){
foreach(i; 0..len){
dp[i+1][0] = dp[i][same[i] ? 1 : 0];
dp[i+1][1] = (dp[i][0] + dp[i][1]) % mod;
}
}
//line
foreach(i; 1..n+1)if(appear[i] == 1){
int idx = idxToC[i][0], x = i, y = Cs[idx][abs(Cs[idx][0]) == x ? 1 : 0];
if(visited[idx]) continue;
visited[idx] = true;
bool[] same;
for(;appear[abs(y)] == 2;){
int nextIdx = getAnother(abs(y), idx);
int which = abs(Cs[nextIdx][0]) == abs(y) ? 0 : 1;
int nx = Cs[nextIdx][which], ny = Cs[nextIdx][which^1];
same ~= y == nx;
visited[nextIdx] = true;
x = nx; y = ny; idx = nextIdx;
}
auto dp = new long[][](same.length+1, 2);
dp[0][0] = 1;
dp[0][1] = 2;
calcDp(cast(int)same.length, dp, same);
ans = ans * (dp[$-1][0] + dp[$-1][1]) % mod;
}
//cycle
foreach(i; 0..m)if(!visited[i]){
int idx = i, x = Cs[i][0], y = Cs[i][1];
bool[] same;
visited[idx] = true;
for(;;){
int nextIdx = getAnother(abs(y), idx);
int which = abs(Cs[nextIdx][0]) == abs(y) ? 0 : 1;
int nx = Cs[nextIdx][which], ny = Cs[nextIdx][which^1];
same ~= y == nx;
if(visited[nextIdx]){
break;
}
visited[nextIdx] = true;
x = nx; y = ny; idx = nextIdx;
}
long tmp = 0;
auto dp = new long[][](same.length, 2);
dp[0][0] = dp[0][1] = 1;
calcDp(cast(int)same.length-1, dp, same);
tmp += dp[$-1][same[$-1] ? 1 : 0];
dp[0][0] = 0; dp[0][1] = 1;
calcDp(cast(int)same.length-1, dp, same);
tmp += dp[$-1][same[$-1] ? 0 : 1];
ans = ans * tmp % mod;
}
writeln(ans);
}
|
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;
}
}
void main() {
auto r = new InputReader;
immutable n = r.next!int;
immutable m = r.next!int;
immutable ta = r.next!long;
immutable tb = r.next!long;
immutable k = r.next!int;
auto a = r.nextA!long(n).map!(t => t + ta).array;
auto b = r.nextA!long(m);
auto e = b.assumeSorted;
long test () {
if (a.length <= k || b.length <= k) {
return -1L;
}
long res = long.min;
foreach (i; 0 .. k + 1) {
int d = k - i;
auto r = e.upperBound (a[i] - 1);
if (r.length <= d) {
return -1;
}
res = max (res, r.drop(d).front);
}
return res + tb;
}
writeln (test ());
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto L = RD;
long ans, best = long.max;
foreach (i; 0..N)
{
if (abs(L+i) < abs(best))
best = L+i;
ans += L+i;
}
writeln(ans - best);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.string;
void main() {
auto nd = readln.split.map!(to!int).array;
int max_ans1, max_ans2;
foreach (idx; 0 .. nd[1]) {
bool d_idx = readln.strip.map!(to!char).array.any!(d => d == '0');
if (d_idx) {
++max_ans1;
if (max_ans1 > max_ans2) {
max_ans2 = max_ans1;
}
} else {
max_ans1 = 0;
}
}
writeln(max_ans2);
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
string s;
cin.scan(s);
if (s == "ABC") writeln("ARC");
else writeln("ABC");
}
|
D
|
import std.array, std.conv, std.stdio, std.string;
void main()
{
auto buf = readln.chomp.split.to!(int[]);
((buf[0] * 500 >= buf[1]) ? "Yes" : "No").writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto ab = readln.chomp.split.to!(int[]);
auto s = readln.chomp.split("-");
auto f = false;
if (s.length == 2 && s[0].length == ab[0] && s[1].length == ab[1]) {
f = true;
}
writeln(f ? "Yes" : "No");
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto next = new int[][](26, N);
foreach (i; 0..26) fill(next[i], -1);
foreach (c; 0..26) {
for (int i = N - 1; i >= 0; i--) {
if (S[i] - 'a' == c) {
next[c][i] = i + 1;
} else if (i < N - 1) {
next[c][i] = next[c][i+1];
}
}
}
auto mem = new Tuple!(int, int)[](N);
fill(mem, tuple(1 << 29, 1 << 29));
Tuple!(int, int) dfs(int n) {
if (n == -1) return tuple(0, -1);
if (n >= N) return tuple(1, -1);
if (mem[n][0] != 1 << 29) return mem[n];
auto ret = tuple(1 << 29, 1 << 29);
foreach (i; 0..26) {
auto t = dfs(next[i][n]);
if (t[0] < ret[0]) ret = tuple(t[0], i);
}
mem[n] = tuple(ret[0] + 1, ret[1]);
return mem[n];
}
dfs(0);
string ans = "";
for (int i = 0; i != -1;) {
if (mem[i][1] == -1) break;
char c = (mem[i][1].to!char + 'a').to!char;
ans ~= c;
i = next[c - 'a'][i];
}
ans.writeln;
}
|
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 CS = new long[][](10, 10);
foreach (i; 0..10) {
foreach (j, c; readln.split.to!(long[])) CS[i][j] = c;
}
foreach (k; 0..10) {
foreach (i; 0..10) {
foreach (j; 0..10) {
if (CS[i][j] > CS[i][k] + CS[k][j]) {
CS[i][j] = CS[i][k] + CS[k][j];
}
}
}
}
long r;
foreach (_; 0..H) {
foreach (n; readln.split.to!(int[])) if (n != -1) {
r += CS[n][1];
}
}
writeln(r);
}
|
D
|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long n;
scan(n);
if (n % 2 == 0)
{
writeln(n / 2);
}
else
{
writeln(n / 2 + 1);
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
auto binary_search(long key, long[] l)
{
long left = 0; //lの左端
long right = l.length - 1; //lの右端
while (right >= left)
{
long mid = left + (right - left) / 2; //区間の真ん中
if (l[mid] == key)
{
return mid;
}
else if (l[mid] > key)
{
right = mid - 1;
}
else if (l[mid] < key)
{
left = mid + 1;
}
}
return -1;
}
// auto a = [1, 14, 32, 51, 51, 51, 243, 419, 750, 910];
// auto a = aryread();
// writeln(a);
// long len_a = a.length;
// writeln(len_a);
// long ans;
// ans = binary_search(51, a);
// writeln(ans);
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main() {
int N = readln.chomp.to!int;
auto cards = readln.chomp.split;
auto cards2 = cards.dup;
string b = cards.bubbleSort.join(" ");
b.writeln;
"Stable".writeln;
string s = cards2.selectionSort.join(" ");
s.writeln;
writeln(s == b ? "Stable" : "Not stable");
}
string[] bubbleSort(string[] cc) {
string[] c = cc;
auto n = c.length;
for (int i = 0; i < n - 1; i++) {
for (int j = n.to!int - 1; j > i; j--) {
if (c[j][1] < c[j - 1][1]) {
swap(c[j], c[j - 1]);
}
}
}
return c;
}
string[] selectionSort(string[] cc) {
string[] c = cc;
auto n = c.length;
for (int i = 0; i < n - 1; i++) {
int minv = c[i][1];
int minj = i;
for (int j = i + 1; j < n; j++) {
if (minv > c[j][1]) {
minv = c[j][1];
minj = j;
}
}
swap(c[i], c[minj]);
}
return c;
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
immutable int mod = 998_244_353;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
auto f = new long [n];
auto c = new long [n];
long total = 0;
foreach (k; 0..n)
{
int cur = a[k];
int i;
for (i = k - 1; i >= 0; i--)
{
int parts = (a[i] + cur - 1) / cur;
cur = a[i] / parts;
auto key = cast (long) cur << 32 | parts;
if (key == c[i])
{
break;
}
f[i] = parts - 1;
c[i] = key;
}
for (i++; i <= k; i++)
{
if (i > 0)
{
f[i] = (f[i] * (i + 1) +
f[i - 1]) % mod;
}
}
total = (total + f[k]) % mod;
}
writeln (total);
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(string, "s", long, "p");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
long n, m;
scan(n, m);
auto a = aryread();
auto s = a.sum();
long cnt;
foreach (e; a)
{
if (4 * m * e >= s)
cnt++;
}
if (cnt >= m)
writeln("Yes");
else
writeln("No");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
void main() {
string s = readln.chomp;
int n = s.to!int;
int m;
foreach (x; s) {
m += x -'0';
}
writeln(n % m == 0 ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.uni;
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string RD() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto tokens = RD.split;
auto N = tokens[0].to!long;
auto S = RD;
auto e = new long[](N+1);
auto w = new long[](N+1);
foreach (i, c; S)
{
w[i+1] = w[i] + (c == 'W' ? 1 : 0);
}
foreach_reverse (i, c; S)
{
e[i] = e[i+1] + (c == 'E' ? 1 : 0);
}
long ans = long.max;
foreach (i; 0..N)
{
ans = min(ans, w[i] + e[i+1]);
}
writeln(ans);
stdout.flush();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int h, w; readV(h, w);
int n; readV(n);
int[] a; readA(n, a);
auto b = new int[](h*w), j = 0;
foreach (int i, ai; a) {
b[j..j+ai][] = i+1;
j += ai;
}
auto putLine(R)(R r)
{
foreach (i, ri; r.enumerate) {
write(ri);
if (i < w-1) write(" ");
}
writeln;
}
foreach (i; 0..h)
if (i%2 == 0) putLine(b[i*w..(i+1)*w]);
else putLine(b[i*w..(i+1)*w].retro);
}
|
D
|
void main()
{
string s = rdStr;
long len = s.length;
foreach (i; 0 .. len)
{
'x'.write;
}
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 ipElems(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.string;
import std.stdio;
import std.algorithm;
void main() {
auto s = readln.chomp.dup;
auto t = readln.chomp.dup;
if (t.length < s.length) {
swap(s, t);
}
auto dp0 = new char[][s.length + 1];
auto dp1 = new char[][s.length + 1];
foreach (ref e;dp0) {
e = new char[0];
}
dp1[0] = new char[0];
foreach (te;t) {
foreach (size_t i, se;s) {
if (te == se) {
dp1[i + 1] = dp0[i];
dp1[i + 1] ~= te;
} else if (dp0[i + 1].length >= dp1[i].length) {
dp1[i + 1] = dp0[i + 1];
} else {
dp1[i + 1] = dp1[i];
}
}
swap(dp0, dp1);
}
writeln(dp0[s.length]);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nq = readln.split.to!(int[]);
auto N = nq[0];
auto Q = nq[1];
auto S = readln.chomp;
char[] ts, ds;
foreach (_; 0..Q) {
auto td = readln;
ts ~= td[0];
ds ~= td[2];
}
int i, j = N-1;
foreach_reverse (k; 0..Q) {
auto t = ts[k];
auto d = ds[k];
if (d == 'L') {
if (i < N && S[i] == t) ++i;
if (j < N-1 && S[j+1] == t) ++j;
} else {
if (j >= 0 && S[j] == t) --j;
if (i > 0 && S[i-1] == t) --i;
}
}
writeln(max(0, j - i + 1));
}
|
D
|
void main() {
auto S = rs;
string s;
foreach(i; 1..6) {
s ~= "hi";
if(S == s) {
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.string, std.conv, std.algorithm;
void main(string[] args) {
auto x = readln().chomp;
foreach(int i, char c; x){
if(i%2==0){c.write;}
}
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;
// dfmt on
void main()
{
long a = lread();
writeln(a + a * a + a * a * a);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
const days = [0,31,28,31,30,31,30,31,31,30,31,30,31];
void main()
{
auto rd = readln.split.to!(int[]), x = rd[0], y = rd[1];
writeln(days[x] == days[y] ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == 0 && x[1] == 0) break;
foreach (i; 0..x[0]) {
foreach (j; 0..x[1]) write("#");
writeln("");
}
writeln("");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
(readln.chomp.to!int / 3).writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(string a, string b, string c) {
int n = cast(int) a.length;
int ans = 0;
for (int i = 0; i < n; i++) {
bool[char] d;
d[a[i]] = true;
d[b[i]] = true;
d[c[i]] = true;
switch (d.keys.length) {
case 2:
ans += 1;
break;
case 3:
ans += 2;
break;
default:
break;
}
}
return ans;
}
void main() {
readint;
string a = read!string;
string b = read!string;
string c = read!string;
writeln(calc(a, b, c));
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math; // math functions
alias point = Point!int;
void main()
{
auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1];
auto a = new point[](n), c = new point[](m);
foreach (i; 0..n) {
auto rd2 = readln.split.to!(int[]), x = rd2[0], y = rd2[1];
a[i] = point(x, y);
}
foreach (i; 0..m) {
auto rd2 = readln.split.to!(int[]), x = rd2[0], y = rd2[1];
c[i] = point(x, y);
}
foreach (ai; a) {
auto minD = int.max, minI = size_t(0);
foreach (j, ci; c) {
auto p = ci - ai, d = p.x.abs + p.y.abs;
if (d < minD) {
minD = d;
minI = j;
}
}
writeln(minI+1);
}
}
struct Point(T)
{
T x, y;
pure auto opBinary(string op: "+")(Point!T rhs) const { return Point!T(x + rhs.x, y + rhs.y); }
pure auto opBinary(string op: "-")(Point!T rhs) const { return Point!T(x - rhs.x, y - rhs.y); }
pure auto opBinary(string op: "*")(Point!T rhs) const { return x * rhs.x + y * rhs.y; }
pure auto opBinary(string op: "*")(T a) const { return Point!T(x * a, y * a); }
pure auto opBinary(string op: "/")(T a) const { return Point!T(x / a, y / a); }
pure auto hypot2() const { return x ^^ 2 + y ^^ 2; }
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
immutable long MAX = 2*10^^6+1;
immutable long MOD = 10^^9+7;
auto modinv = new long[](MAX);
modinv[0] = modinv[1] = 1;
foreach(i; 2..MAX) {
modinv[i] = modinv[MOD % i] * (MOD - MOD / i) % MOD;
}
auto f_mod = new long[](MAX);
auto f_modinv = new long[](MAX);
f_mod[0] = f_mod[1] = 1;
f_modinv[0] = f_modinv[1] = 1;
foreach(i; 2..MAX) {
f_mod[i] = (i * f_mod[i-1]) % MOD;
f_modinv[i] = (modinv[i] * f_modinv[i-1]) % MOD;
}
long nck(long n, long k) {
return f_mod[n] * f_modinv[n-k] % MOD * f_modinv[k] % MOD;
}
int H, W, A, B;
scanf("%d %d %d %d", &H, &W, &A, &B);
long ans = 0;
foreach (i; 0..H-A) {
ans = (ans + nck(B-1+i, i) * nck(W-B+H-i-2, H-i-1) % MOD) % MOD;
}
writeln( ans >= 0 ? ans : ans+MOD);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int[] factors(int n) {
assert(n > 1);
int[] ps;
while (n % 2 == 0) {
ps ~= 2;
n /= 2;
}
for (int i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
ps ~= i;
n /= i;
}
}
if (n > 1) ps ~= n;
return ps;
}
long calc(int n) {
const mod = cast(int)1e9 + 7;
long[int] d;
for (int i = 2; i <= n; i++) {
foreach (x; factors(i)) {
d[x]++;
}
}
long ans = 1;
foreach (k, v; d) {
ans = (ans * (v + 1)) % mod;
}
return ans;
}
void main() {
int n = readint;
writeln(calc(n));
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto si = readln.split.to!(int[]);
auto q = readln.chomp.to!size_t;
auto ti = readln.split.to!(int[]);
writeln(ti.count!(t => si.canFind(t)));
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, k; rd(n, k);
auto x=new long[](n), y=new long[](n);
foreach(i; 0..n) rd(x[i], y[i]);
long mn=5_000_000_000_000_000_000;
foreach(i1; 0..n)foreach(i2; (i1+1)..n){
foreach(j1; 0..n)foreach(j2; (j1+1)..n){
auto x1=x[i1], x2=x[i2];
auto y1=y[j1], y2=y[j2];
if(x1>x2) swap(x1, x2);
if(y1>y2) swap(y1, y2);
if((x2-x1)*(y2-y1)>=mn) continue;
int cnt=0;
foreach(t; 0..n){
if(x1<=x[t] && x[t]<=x2 && y1<=y[t] && y[t]<=y2) cnt++;
}
if(cnt>=k) mn=min(mn, (x2-x1)*(y2-y1));
}
}
writeln(mn);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
// 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 H, W;
scan(H, W);
auto S = new string[](H);
foreach (i; 0 .. H)
S[i] = sread();
auto R = new long[][](H, W);
auto C = new long[][](H, W);
foreach (w; 0 .. W)
{
long h = 0;
while (h < H)
{
long len = 0;
while (h + len < H && S[h + len][w] == '.')
len++;
foreach (i; h .. h + len)
C[i][w] = len;
h += max(1, len);
}
}
foreach (h; 0 .. H)
{
long w = 0;
while (w < W)
{
long len = 0;
while (w + len < W && S[h][w + len] == '.')
len++;
R[h][w .. w + len] = len;
w += max(1, len);
}
}
long ans = 0;
foreach (h; 0 .. H)
foreach (w; 0 .. W)
ans = ans.max(R[h][w] + C[h][w] - 1);
writeln(ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto t = readln.split;
auto N = t[0].to!int;
auto L = t[1].to!long;
auto S = N.iota.map!(_ => readln.chomp).array;
auto tr = new Trie(L);
foreach (s; S) {
tr.add_string(s);
}
writeln(tr.search ? "Alice" : "Bob");
}
class Node {
int d;
Node[2] children;
this(int d) {
this.d = d;
}
}
class Trie {
Node root;
long L;
this(long L) {
root = new Node(0);
this.L = L;
}
void add_string(string s) {
Node cur = root;
foreach (c; s) {
if (cur.children[c-'0'] is null) {
cur.children[c-'0'] = new Node(cur.d+1);
}
cur = cur.children[c-'0'];
}
}
long search() {
return search(root);
}
long search(Node n) {
long l = L - n.d;
long i = 1;
while (l != 0 && l % (i * 2) == 0) i *= 2;
if (n.children[0] is null && n.children[1] is null) {
return 0;
} else if (n.children[0] is null) {
return search(n.children[1]) ^ i;
} else if (n.children[1] is null) {
return search(n.children[0]) ^ i;
} else {
return search(n.children[0]) ^ search(n.children[1]);
}
}
}
|
D
|
import std.stdio,std.array,std.conv,std.string;
void main(){
int n=readln().chomp().to!int;
string k=to!string(n);
int m=0;
for(int i=0;i<k.length;i++){
m+=to!int(k[i])-48;
}
if(n%m==0) writeln("Yes");
else writeln("No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto x = RD;
auto y = RD;
if (x == 0 && y == 0) continue;
auto r2 = x^^2 + y^^2;
bool ok;
for (long i = 1; i*i <= r2; ++i)
{
if (i*i == r2)
{
ok = true;
break;
}
}
if (ok)
ans[ti] = 1;
else
ans[ti] = 2;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
void calc(int n) {
string get() { return readln.chomp; }
writeln(0);
stdout.flush();
auto s = get();
int lo = 1;
int hi = n - 1;
for (int i = 0; i < 19; i++) {
int m = (lo + hi) / 2;
writeln(m);
stdout.flush();
auto t = get();
if (t == "Vacant") break;
if (m % 2 == 0) {
if (s == t)
lo = m + 1;
else
hi = m - 1;
}
else {
if (s == t)
hi = m - 1;
else
lo = m + 1;
}
}
}
void main() {
int n = readint;
calc(n);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
int[] a = readln.chomp.split.to!(int[]);
if (a[0] <= 8 && a[1] <= 8) {
writeln("Yay!");
}
else {
writeln(":(");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
enum DREAM = "dream";
enum DREAM_CNT = DREAM.length;
enum DREAMER = "dreamer";
enum DREAMER_CNT = DREAMER.length;
enum ERASE = "erase";
enum ERASE_CNT = ERASE.length;
enum ERASER = "eraser";
enum ERASER_CNT = ERASER.length;
void main()
{
auto s = readln.chomp;
while (!s.empty) {
if (s.endsWith(DREAMER)) {
s = s[0..$-DREAMER_CNT];
} else if (s.endsWith(ERASER)) {
s = s[0..$-ERASER_CNT];
} else if (s.endsWith(DREAM)) {
s = s[0..$-DREAM_CNT];
} else if (s.endsWith(ERASE)) {
s = s[0..$-ERASE_CNT];
} else {
writeln("NO");
return;
}
}
writeln("YES");
return;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
writeln(A <= 9 && B <= 9 ? A*B : -1);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
void main() {
auto input = readln.split.map!(to!int);
int N = input[0];
int T = input[1];
auto A = readln.split.map!(to!int).array;
auto M = new int[](N);
M[N-1] = A[N-1];
foreach (i; iota(N-2, -1, -1))
M[i] = max(M[i+1], A[i]);
auto L = new int[](N);
int m = 0;
foreach (i; iota(N-1)) {
L[i] = M[i+1] - A[i];
m = max(m, L[i]);
}
int ans = 0;
foreach (i; iota(N-1))
if (L[i] == m)
ans++;
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abc = readln.split.to!(int[]);
auto d = abc[0] + abc[1] + abc[2];
writeln(d + max(abc[0], abc[1], abc[2]) * 9);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main() {
auto n = readln.chomp.to!(int);
auto ans = 0;
if (n / 100 == 9)
ans += 100;
else
ans += 900;
if (n % 100 >= 90)
ans += 10;
else
ans += 90;
if (n % 10 == 9)
ans += 1;
else
ans += 9;
ans.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long D, N;
scan(D, N);
if (N == 100)
{
writeln("101" ~ repeat('0').take(D * 2).array);
}
else
{
writeln(text(N) ~ repeat('0').take(D * 2).array);
}
}
|
D
|
void main() {
string b = readln.chomp;
b == "A" && "T".writeln;
b == "T" && "A".writeln;
b == "C" && "G".writeln;
b == "G" && "C".writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
string s = readln.chomp;
long ans;
string old = "", now = "";
foreach(c; s){
now ~= c;
if(now != old){
ans += 1;
old = "" ~ now;
now = "";
}
log("old:", old, "now:", now, "ans:", ans);
}
ans.writeln;
}
/*
long n = read.to!long;
long m = read.to!long;
long k = read.to!long;
long a = read.to!long;
long b = read.to!long;
long x = read.to!long;
long y = read.to!long;
long z = read.to!long;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
long[] bs;
foreach(i; 0 .. n) bs ~= read.to!long;
long[] as;
foreach(j; 0 .. m) as ~= read.to!long;
long[] bs;
foreach(j; 0 .. m) bs ~= read.to!long;
struct S{
long a, b;
}
S[] xs;
foreach(i; 0 .. n) xs ~= S(read.to!long, read.to!long);
*/
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto dp = new bool[][][][](N+1, 11, 11, 11);
foreach (i; 0..N) {
dp[i + 1] = dp[i].dup;
foreach (j; 0..11) {
foreach (k; 0..11) {
if (dp[i][10][j][k]) {
dp[i + 1][j][k][S[i] - '0'] = true;
}
}
}
dp[i + 1][10][10][S[i] - '0'] = true;
}
int ans = 0;
foreach (i; 0..10) foreach (j; 0..10) foreach (k; 0..10) ans += dp[N][i][j][k];
ans.writeln;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = new long[](0);
while(abs(n) > 0)
{
if(n == 1)
{
a ~= 1;
break;
}
if(abs(n % (-2)) > 0)
{
a ~= 1;
n -= 1;
}
else
{
a~= 0;
}
n /= (-2);
}
foreach_reverse (e; a)
{
e.write();
}
if(a.empty())
write(0);
writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
import std.bigint, std.functional;
void main() {
auto I = readln.split.to!(long[]);
auto A = I[0];
auto B = I[1];
auto C = I[2];
bool solve() {
auto x = C - A - B;
if (x < 0) return false;
return 4*A*B < x*x;
}
writeln(solve() ? "Yes" : "No");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto a = readln.chomp;
auto b = readln.chomp;
auto s = max(a.length, b.length);
auto a2 = a.rightJustify(s, '0');
auto b2 = b.rightJustify(s, '0');
if (a2 == b2)
writeln("EQUAL");
else if (a2 < b2)
writeln("LESS");
else
writeln("GREATER");
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.