code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main() {
auto data = readln().split();
auto S = data[0].to!string();
auto first = S;
auto num = 0;
foreach (c; S) { if (c == '1') num++; }
writeln(2*min( num, S.length-num ));
}
|
D
|
import std.stdio;
void main() {
int n;
scanf("%u", &n);
int a5, a0;
while(n--) {
int t;
scanf("%u", &t);
if(t == 5) a5++;
else a0++;
}
if(a0 == 0) {
write(-1);
return;
}
int m, c;
foreach(i; 0..a5)
if((m += 5) % 9 == 0) c = i + 1;
if(!c)
a0 = 1;
while(c--) write(5);
while(a0--) write(0);
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int H, W, d;
sc.read(H, W, d);
string s = "RYGB";
int[][] res = new int[][](H, W);
foreach (i; 0..H) {
foreach (j; 0..W) {
int y = (i-j + 10000*d) / d;
int x = (i+j + 10000*d) / d;
write(s[y%2*2+x%2]);
}
writeln();
}
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
alias A = Tuple!(int, "u", int, "v", int, "w");
void calc(int n, A[] a) {
auto refs = new int[n];
foreach (e; a) {
refs[e.u]++;
refs[e.v]++;
}
int leaf = -1;
for (int i = 0; i < refs.length; i++) {
if (refs[i] == 1) {
leaf = i;
break;
}
}
A[][10^^5 + 10] adj;
foreach (e; a) {
adj[e.u] ~= e;
adj[e.v] ~= e;
}
assert(leaf != -1);
auto used = new int[n];
auto colors = new int[n];
void rec(int node, int color) {
if (used[node]++) return;
colors[node] = color;
foreach (next; adj[node]) {
if (next.w % 2 == 0) {
rec(next.u, color);
rec(next.v, color);
}
else {
rec(next.u, color ^ 1);
rec(next.v, color ^ 1);
}
}
}
rec(leaf, 0);
for (int i = 0; i < colors.length; i++) {
writeln(colors[i]);
}
}
void main() {
int n = readint;
A[] a;
for (int i = 0; i < n - 1; i++) {
auto x = readints;
a ~= A(x[0] - 1, x[1] - 1, x[2]);
}
calc(n, a);
}
|
D
|
import std.conv;
import std.math;
import std.stdio;
import std.string;
private const JUMP_TIME = 1;
private const EAT_TIME = 1;
void main() {
auto
trees_count = read(),
position = read(),
time = position + EAT_TIME;
foreach (i; 1 .. trees_count) {
auto tree = read();
time += abs(position - tree) + EAT_TIME + JUMP_TIME;
position = tree;
}
stdout.write(time);
}
int read() {
return to!int(strip(stdin.readln()));
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto K = readln.chomp.to!int;
auto dp = new int[][][](N, N, K+1);
foreach (i; 0..N) dp[i][i][0] = 1;
if (K > 0) foreach (i; 0..N-1) dp[i][i+1][S[i]!=S[i+1]] = 2;
else foreach (i; 0..N-1) if (S[i] == S[i+1]) dp[i][i+1][0] = 2;
foreach (len; 1..N+1) {
foreach (il; 0..N-len+1) {
foreach (k; 0..K+1) {
int ir = il + len - 1;
if (il > 0) dp[il-1][ir][k] = max(dp[il-1][ir][k], dp[il][ir][k]);
if (ir < N - 1) dp[il][ir+1][k] = max(dp[il][ir+1][k], dp[il][ir][k]);
if (il > 0 && ir < N - 1) {
int cost = S[il-1] != S[ir+1];
if (k + cost <= K) {
dp[il-1][ir+1][k+cost] = max(dp[il-1][ir+1][k+cost], dp[il][ir][k] + 2);
}
}
}
}
}
dp[0][N-1].reduce!max.writeln;
}
|
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() {
readln;
((long[] as) => as.map!"a/2".array).repeat.cumulativeFold!"b(a)"(readln.split.to!(long[]).map!"a*2".array).countUntil!(any!"a%2==1").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 core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto a = lread();
(a%2?a*2:a).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[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
long cnt = 1;
long ans;
foreach (a; A)
if (a != cnt)
{
ans++;
}
else
{
cnt++;
}
if (ans == N)
writeln(-1);
else
writeln(ans);
}
|
D
|
import std.stdio;import std.conv;import std.string;
void main(){ writeln( (readln().chomp().to!int())^^3 ); }
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format, std.datetime;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
auto dp = new long[](N + 1);
dp[0] = 1;
foreach (i; 0 .. N)
dp[i + 1] = dp[i] * ((A[i] % 2 == 0) + 1);
writeln(3 ^^ N - dp[N]);
}
|
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,b=scanElem,c=scanElem;
if(a<=c&&b>=c)end("Yes");end("No");
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int h, w; rd(h, w);
auto d=new int[][](10, 10);
foreach(i; 0..10) d[i]=readln.split.to!(int[]);
auto a=new int[][](h, w);
foreach(i; 0..h) a[i]=readln.split.to!(int[]);
foreach(_; 0..10)foreach(i; 0..10)foreach(j; 0..10){
d[i][j]=min(d[i][j], d[i][_]+d[_][j]);
}
int sum=0;
foreach(i; 0..h)foreach(j; 0..w){
if(a[i][j]>=0) sum+=d[a[i][j]][1];
}
writeln(sum);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abc = readln.split.to!(long[]);
auto A = abc[0];
auto B = abc[1];
auto C = abc[2];
int r;
foreach (_; 0..1000) {
if (A%2 == 1 || B%2 == 1 || C%2 == 1) {
writeln(r);
return;
}
++r;
auto a = (B+C)/2;
auto b = (A+C)/2;
auto c = (A+B)/2;
A = a;
B = b;
C = c;
}
writeln(-1);
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto M = scan!int;
auto K = scan!long;
auto A = scan!long(N);
auto B = scan!long(M);
long solve() {
long[] sumA = new long[N+1];
foreach(i; 1..N+1) sumA[i] = sumA[i-1] + A[i-1];
long[] sumB = new long[M+1];
foreach(i; 1..M+1) sumB[i] = sumB[i-1] + B[i-1];
sumA.deb;
sumB.deb;
long ans;
long b = M;
foreach(a; 0..N+1) {
if (sumA[a] > K) continue;
long rest = K - sumA[a];
foreach_reverse(bi; 0..b+1) {
if (sumB[bi] <= rest) {
b = bi;
break;
}
}
if (ans < a + b) ans = a + b;
}
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.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
int[char] d;
foreach (c; s) {
d[c]++;
}
bool ok = d.values.all!(e => e == 2);
writeln(ok ? "Yes" : "No");
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc177/tasks/abc177_b
// implementation, string manipulation
import std.algorithm;
import std.stdio;
import std.string;
void main() {
string s = readln.strip;
string t = readln.strip;
long minima = t.length;
for(int i = 0; i + t.length <= s.length; ++i) {
long mismatches = 0;
for(int j = 0; j < t.length; ++j)
if(t[j] != s[i + j])
mismatches += 1;
minima = min(minima, mismatches);
}
minima.writeln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto xs = readln.split.to!(int[]);
if (xs[0] != xs[1]) writeln(xs.reduce!max * 2 - 1);
else writeln(xs[0] * 2);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
int[int] c;
foreach (ai ; a) {
int b = ai;
for (int j = 2; j*j <= ai; j++) {
int t;
while (b % j == 0) {
t++;
b /= j;
}
if (j in c) c[j] = max(c[j], t);
else c[j] = t;
}
if (b > 1) {
if (b in c) c[b] = max(c[b], 1);
else c[b] = 1;
}
}
debug {
writeln(c);
}
long M = 1;
foreach (k, v ; c) {
(M *= k^^v) %= mod;
}
long ans;
foreach (ai ; a) {
auto b = M * powmod(ai, mod - 2) % mod;
(ans += b) %= mod;
}
writeln(ans);
}
long powmod(int x, int y) {
return y > 0 ? powmod(x, y>>1)^^2 % mod * x^^(y&1) % mod : 1;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip, std.numeric;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
void main() {
auto cin = new Scanner;
long k;
cin.scan(k);
long res;
foreach (i; 1 .. k + 1) {
foreach (j; 1 .. k + 1) {
foreach (l; 1 .. k + 1) {
res += gcd(gcd(i, j), l);
}
}
}
writeln(res);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
string s;
while ((s = readln.chomp).length > 0) {
if (s == "0")
break;
int sum = s.map!(e => e - '0').sum;
writeln(sum);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1];
if(A+B<10)writeln(A+B);
else writeln("error");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
struct Cmb
{
void init(size_t n)
{
long powmod(long a, long p)
{
long ans = 1;
long mul = a;
while (p > 0)
{
if ((p & 1) == 1)
ans.modm(mul);
p >>= 1;
mul.modm(mul);
}
return ans;
}
++n;
fact = new long[2][](n);
fact[0][0] = 1;
foreach (i; 1..n)
{
fact[i][0] = fact[i-1][0];
fact[i][0].modm(i);
}
fact[n-1][1] = powmod(fact[n-1][0], mod - 2);
foreach_reverse (i; 0..n-1)
{
fact[i][1] = fact[i+1][1];
fact[i][1].modm(i+1);
}
}
long get(size_t n, size_t r)
{
long res = fact[n][0];
res.modm(fact[r][1]);
res.modm(fact[n-r][1]);
return res;
}
long[2][] fact;
}
void main()
{
auto n = RD!int;
auto m = RD!int;
Cmb cmb;
cmb.init(m);
long ans;
foreach (i; 1..n-1)
{
long pat = cmb.get(n-2, i);
pat.modm(i);
ans.moda(pat);
}
long pat2;
foreach (i; n-1..m+1)
{
pat2.moda(cmb.get(i-1, n-2));
}
ans.modm(pat2);
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
auto n = read.to!long;
auto q = read.to!long;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
debug writeln("as: ", as);
long[] bs;
foreach_reverse(a; as) bs ~= a;
debug writeln("bs: ", bs);
long[] sums;
foreach(i; 0 .. n){
if(i == 0) sums ~= bs[i];
else sums ~= sums[$ - 1] + bs[i];
}
debug writeln("sums: ", sums);
long[] evensums;
for(int i = 0; i * 2 < n; i ++){
if(i == 0) evensums ~= bs[i * 2];
else evensums ~= evensums[$ - 1] + bs[i * 2];
}
debug writeln("evensums: ", evensums);
long evensum = evensums[$ - 1];
debug writeln("evensum: ", evensum);
long[] cs;
for(int i = 0; i * 2 < n; i ++) cs ~= bs[i] + bs[i * 2];
debug writeln("cs: ", cs);
foreach(t; 0 .. q){
auto x = read.to!long;
auto i = uplimit(0, (n - 1) / 2, i => (x * 2 <= cs[i]));
if(i < 0) i = 0;
debug writeln("x: ", x, " i: ", i);
auto ans = sums[i] + evensum - evensums[i];
ans.writeln;
}
}
// fをみたす最大(二分探索; binary search)
long uplimit(long a, long c, bool delegate(long) f){
if(f(c)) return c;
if(! f(a)) return a - 1;
while(a + 1 < c){
long b = (a + c) / 2;
if(f(b)) a = b;
else c = b;
}
return a;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto ni = readln.split.to!(int[]);
writeln(ni[0] - ni[1] + 1);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main()
{
auto tokens = split(chomp(readln()));
auto n = to!int(tokens[0]);
auto m = to!int(tokens[1]);
string s = chomp(readln());
int[][] node;
node.length = n;
foreach (i; 0..m)
{
auto tokens2 = split(chomp(readln()));
auto n1 = to!int(tokens2[0]) - 1;
auto n2 = to!int(tokens2[1]) - 1;
node[n1] ~= n2;
node[n2] ~= n1;
}
bool[] nodeS;
nodeS.length = n;
fill(nodeS, true);
void compute(int i)
{
bool isDone1 = false, isDone2 = false;
char c1 = s[i];
foreach (j; node[i])
{
if (!nodeS[j]) continue;
char c2 = s[j];
if (c1 == c2) isDone1 = true;
else isDone2 = true;
}
if (!isDone1 || !isDone2)
{
nodeS[i] = false;
foreach (j; node[i])
{
if (!nodeS[j]) continue;
compute(j);
}
}
}
foreach (i, node2; node)
{
compute(cast(int)i);
}
bool isComplete = false;
foreach (e; nodeS) if (e) isComplete = true;
writeln(isComplete ? "Yes" : "No");
stdout.flush();
}
|
D
|
void main()
{
string n = rdStr;
writeln(n.canFind('7') ? "Yes" : "No");
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
enum MOD = 10 ^^ 9 + 7;
void main()
{
long N = lread();
auto A = aryread();
auto dp = new long[][](N + 1, 2);
dp[0][] = dp[1][] = long.min / 2;
dp[0][0] = 0;
// writeln(dp[0]);
foreach (i; 0 .. N)
{
dp[i + 1][0] = dp[i + 1][0].max(dp[i][0] + A[i]);
dp[i + 1][0] = dp[i + 1][0].max(dp[i][1] - A[i]);
dp[i + 1][1] = dp[i + 1][1].max(dp[i][0] - A[i]);
dp[i + 1][1] = dp[i + 1][1].max(dp[i][1] + A[i]);
}
writeln(dp[N][0]);
}
|
D
|
import std.stdio, std.conv, std.string;
long search(long n, long k, long cur)
{
if (n == 0) { return cur; }
long a = search(n-1, k, cur+k);
long b = search(n-1, k, cur*2);
return (a>b) ? b : a;
}
void main() {
long n, k;
scanf("%d %d", &n ,&k);
writeln(search(n, k, 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;
enum mod = 1_000_000_007L;
void main() {
auto s = readln.split.to!(int[]).sum();
writeln(s >= 22 ? "bust" : "win");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
int n = readln.chomp.to!int;
int[] as = readln.chomp.split.map!(to!int).array;
int c = 0;
foreach(a; as) if(a % 2) c += 1;
string ans;
if(c % 2) ans = "NO"; else ans = "YES";
ans.writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main()
{
auto tokens = split(chomp(readln()));
auto x1 = to!int(tokens[0]);
auto y1 = to!int(tokens[1]);
auto x2 = to!int(tokens[2]);
auto y2 = to!int(tokens[3]);
int dx = x2 - x1;
int dy = y2 - y1;
int x3 = x2 + -1 * dy;
int y3 = y2 + dx;
int dx2 = x3 - x2;
int dy2 = y3 - y2;
int x4 = x3 + -1 * dy2;
int y4 = y3 + dx2;
writeln(x3, " ", y3, " ", x4, " ", y4);
stdout.flush();
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
readln.chomp.map!(c => c == '+' ? +1 : -1).sum.writeln;
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int x = tmp[0], y = tmp[1], z = tmp[2];
writeln((x - z) / (y + z));
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
alias Pair = Tuple!(int, "val", int, "num");
void main() {
int n, m, k;
scan(n, m, k);
auto size = n * m;
auto mc = ModComb(size);
auto okikata = mc.c(size - 2, k - 2);
long ans = (sumKyori(n, m) + sumKyori(m, n)) % mod * okikata % mod;
writeln(ans);
}
long sumKyori(int n, int m) {
long ans;
foreach (i ; 0 .. m - 1) {
ans += 1L * (i + 1) * (m - 1 - i) % mod * n % mod * n % mod;
}
return ans;
}
struct ModComb {
int _N;
long[] _fact, _factinv;
long _mod;
this(int n, long mod = 1_000_000_007L) {
_N = n;
_fact = new long[](_N + 1);
_factinv = new long[](_N + 1);
_mod = mod;
_fact[0] = 1;
foreach (i ; 1 .. _N + 1) {
_fact[i] = (_fact[i-1] * i) % mod;
}
_factinv[_N] = _powmod(_fact[_N], mod - 2);
foreach_reverse (i ; 0 .. _N) {
_factinv[i] = (_factinv[i+1] * (i+1)) % mod;
}
}
long c(int n, int k) {
return f(n) * finv(n - k) % _mod * finv(k) % _mod;
}
long p(int n, int r) {
return f(n) * finv(n - r) % _mod;
}
long f(int n) {
return _fact[n];
}
long finv(int n) {
return _factinv[n];
}
long _powmod(long x, long y) {
return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1;
}
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main(){
int n, m, q; rd(n, m, q);
auto cul=new int[][](n+1, n+1);
foreach(_; 0..m){
int l, r; rd(l, r);
cul[l][r]++;
}
for(int i=1; i<=n; i++){
foreach(j; 0..n) cul[i][j+1]+=cul[i][j];
}
while(q--){
int l, r; rd(l, r);
int num=0;
for(int i=l; i<=r; i++){
num+=cul[i][r]-cul[i][i-1];
}
writeln(num);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
auto a = aryread();
auto ans = new long[](n + 1);
foreach (i; 0 .. n)
{
ans[a[i]] = i + 1;
}
// writeln(ans);
foreach (i; 1 .. (ans.length))
{
write(ans[i], ' ');
}
}
//https://rclone.org/
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio,std.array,std.conv,std.algorithm,std.range,std.string;
void main(string[] args) {
string[2] a;
a[0] = readln.chomp;
a[1] = readln.chomp;
if(a[0][0]==a[1][2]&&a[0][1]==a[1][1]&&a[0][2]==a[1][0]){
"YES".writeln;
}else{
"NO".writeln;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
long a, b;
scan(a, b);
bool x = a <= 8 && b <= 8;
writeln(x?"Yay!":":(");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
auto input = readln.chomp.split.to!(long[]);
long a = input[0];
long b = input[1];
long x = input[2];
writeln((a <= x && x <= a + b)?"YES":"NO");
}
|
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, B;
scan(A, B);
writeln(A < 10 && B < 10 ? A * B : -1);
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
void main() {
while (true) {
auto input = readln.chomp.split.map!(to!int);
if (input.array == [0, 0]) break;
int n = input[0];
int x = input[1];
int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
for (int k = j+1; k <= n; k++) {
if (i + j + k == x) {
cnt++;
}
}
}
}
cnt.writeln;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
long P = 10^^9+7;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
writeln(pow(K, N));
}
|
D
|
void main() {
int[][] c = new int[][](3, 3);
foreach (i; 0 .. 3) {
int[] tmp = readln.split.to!(int[]);
foreach (j, x; tmp) {
c[i][j] = x;
}
}
bool ok = true;
foreach (i; 1 .. 3) {
int[] diff = new int[3];
foreach (j; 0 .. 3) {
diff[j] = c[i][j] - c[i-1][j];
}
if (!diff.all!(x => x == diff[0])) {
ok = false;
}
}
writeln(ok ? "Yes" : "No");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
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); }
struct Cmb
{
void init(size_t n)
{
long powmod(long a, long p)
{
long ans = 1;
long mul = a;
while (p > 0)
{
if ((p & 1) == 1)
ans.modm(mul);
p >>= 1;
mul.modm(mul);
}
return ans;
}
++n;
fact = new long[2][](n);
fact[0][0] = 1;
foreach (i; 1..n)
{
fact[i][0] = fact[i-1][0];
fact[i][0].modm(i);
}
fact[n-1][1] = powmod(fact[n-1][0], mod - 2);
foreach_reverse (i; 0..n-1)
{
fact[i][1] = fact[i+1][1];
fact[i][1].modm(i+1);
}
}
long get(size_t n, size_t r)
{
long res = fact[n][0];
res.modm(fact[r][1]);
res.modm(fact[n-r][1]);
return res;
}
long[2][] fact;
}
void main()
{
auto t = RD!int;
auto ans = new long[](t);
Cmb cmb;
cmb.init(2*(10^^5));
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
if (k == 0)
{
ans[ti] = 1;
continue;
}
if (n == 1)
{
ans[ti] = 2;
ans[ti].modpow(k);
continue;
}
long pat;
foreach (i; 0..n+1)
{
if (i % 2) continue;
pat.moda(cmb.get(n, i));
}
if (n % 2)
pat.moda(1);
else
pat.mods(1);
debug writeln("pat:", pat);
auto dp = new long[](k+1);
dp[0] = 1;
foreach (i; 0..k)
{
if (n % 2 == 0)
{
long tmp = 2;
tmp.modpow(cast(long)n * (k-i-1));
tmp.modm(dp[i]);
ans[ti].moda(tmp);
}
dp[i+1] = dp[i];
dp[i+1].modm(pat);
}
ans[ti].moda(dp[k]);
debug writeln(dp);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto N = readln.chomp.to!int;
if (N == 0) {
writeln(0);
return;
}
int[] po;
while (N != 0) {
const c = abs(N % 2);
po ~= c;
N = (N - c) / (-2);
}
po.retro.each!write;
writeln;
}
|
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 A = readLong();
const B = readLong();
const C = readLong();
// 2 sqrt(A B) < C - A - B
bool ans = (0 < C - A - B && 4 * A * B < (C - A - B)^^2);
writeln(ans ? "Yes" : "No");
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons, std.math;
void main() {
ulong N = readln.chomp.to!ulong;
ulong res = ulong.max;
ulong m = 1 + cast(ulong)ceil(sqrt(cast(double)N));
foreach(i; 1..m) {
if(N % i == 0) {
ulong j = N / i;
res = min(res, i+j-2);
}
}
res.writeln;
}
|
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 nzw = readln.split.to!(int[]);
auto N = nzw[0];
auto Z = nzw[1];
auto W = nzw[2];
auto as = readln.split.to!(int[]).reverse.array;
auto DP = new int[][](2, N+1);
foreach (ref dp; DP) dp[] = -1;
int solve(int t, int i) {
auto a = i == N ? W : as[i];
if (DP[t][i] == -1) {
auto r = abs(a - as[0]);
foreach (j; 1..i) {
r = t == 0 ? max(r, solve(1, j)) : min(r, solve(0, j));
}
DP[t][i] = r;
}
return DP[t][i];
}
writeln(solve(0, N));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
T gcd(T)(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int calc(int[] a) {
int n = cast(int)a.length;
// lt[i] = A_0, A_1, ..., A_{i-1} の最大公約数
auto lt = new int[n + 1];
// rt[i] = A_i, A_{i+1}, ..., A_{N-1} の最大公約数
auto rt = new int[n + 1];
for (int i = 0; i < n; i++)
lt[i + 1] = gcd(lt[i], a[i]);
for (int i = n - 1; i >= 0; i--)
rt[i] = gcd(rt[i + 1], a[i]);
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, gcd(lt[i], rt[i + 1]));
}
return ans;
}
// 別解:
// 1 つしか削除できないのだから、任意の 2 つの約数の最大値が答えになる
int calc2(int[] a) {
size_t f(int n) {
return a.count!(e => e % n == 0);
}
int n = cast(int)a.length;
int ans = 0;
foreach (x; a.take(2)) {
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) {
if (f(i) >= n - 1) ans = max(ans, i);
if (f(x / i) >= n - 1) ans = max(ans, x / i);
}
}
}
return ans;
}
void main() {
readint;
auto a = readints;
// writeln(calc(xs));
writeln(calc2(a));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
import std.math;
import std.range;
void main() {
auto tmp = readln.split.to!(int[]);
auto H = tmp[0], W = tmp[1];
char[][] S;
foreach (i; 0..H) {
S ~= readln.chomp.dup;
}
foreach (i; 0..W) {
foreach (j; 0..H) {
if (S[j][i] == '#') continue;
auto c = 0;
foreach (k; -1..2) {
if (i+k == -1) continue;
if (i+k == W) continue;
foreach (l; -1..2) {
if (j+l == -1) continue;
if (j+l == H) continue;
if (S[j+l][i+k] == '#') c++;
}
}
S[j][i] = cast(char)(c + '0');
}
}
foreach (i; 0..H) {
writeln(S[i]);
}
}
|
D
|
import std.stdio, std.string, std.conv, std.math;
void main() {
int n = stdin.readln.chomp.to!int;
long debt = 100000;
foreach(i;0..n) {
debt += debt * 0.05;
debt = (((debt / 1000.0).ceil) * 1000).to!long;
}
debt.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto s = RD!string;
writeln(s[0] ~ to!string(s.length-2) ~ s[$-1]);
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
int[] deg = new int[n];
foreach (i; 0..m) {
int a, b;
sc.read(a, b); a--; b--;
deg[a]++; deg[b]++;
}
if (deg.filter!(x => x%2 == 1).empty) {
writeln("YES");
} else {
writeln("NO");
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
|
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 R = RD;
writeln(R < 1200 ? "ABC" : R < 2800 ? "ARC" : "AGC");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
auto d = readln.chomp.to!(char[]);
d.reverse;
d.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 N, M;
scan(N, M);
auto S = sread();
auto T = sread();
long g = gcd(N, M);
foreach (i; 0 .. g)
{
if (S[i * (N / g)] != T[i * (M / g)])
{
writeln(-1);
return;
}
}
writeln((N * M) / g);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int m;
scan(m);
auto ans = 24 - m + 24;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
void main()
{
long n, t;
rdVals(n, t);
long[] a = rdRow;
long total;
long time;
foreach (x; a)
{
if (x < time)
{
long diff = t - time + x;
total += diff;
time += diff;
}
else
{
total += t;
time = x + t;
}
}
total.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
/*
(1からNではなく、0からN-1として考える)
0からiまでを並べ、s[0 .. i] に従い、最後が j で終わる場合の数をxs[i][j]とする
xs[0][0] = 1
xs[0][j] = 0
s[i] = '<' の場合
xs[i][j] = sum(j'; 0 .. j) xs[i - 1][j']
s[i] = '>' の場合
xs[i][j] = sum(j'; j .. i) xs[i - 1][j']
このsumは愚直にループを回すとTLEになるが、累積和ならOK
求めるものは sum(j; 0 .. j) xs[n][j]
----------------------------------------
*/
const mod = 1_000_000_007;
void main(){
long n = read.to!long;
string s = readln.chomp;
long[][] xs = new long[][](n, n);
xs[0][0] = 1;
debug writeln("i:", 0, " xs:", xs);
foreach(i; 1 .. n){
if(s[i - 1] == '<'){
long sum = 0;
foreach(j; 0 .. i + 1){
if(j > 0) sum += xs[i - 1][j - 1], sum %= mod;
debug writeln("i:", i, " j:", j, " sum:", sum);
xs[i][j] = sum;
}
}
else{
long sum = 0;
foreach_reverse(j; 0 .. i + 1){
if(j < i) sum += xs[i - 1][j], sum %= mod;
debug writeln("i:", i, " j:", j, " sum:", sum);
xs[i][j] = sum;
}
}
debug writeln("i:", i, " xs:", xs);
}
long ans = 0;
foreach(j; 0 .. n) ans += xs[n - 1][j], ans %= mod;
ans.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto xs = readints;
int a = xs[0], b = xs[1], c = xs[2], d = xs[3];
if (abs(a - c) <= d || (abs(a - b) <= d && abs(b - c) <= d)) {
writeln("Yes");
}
else writeln("No");
}
|
D
|
import std.stdio, std.conv, std.algorithm, std.array, std.string;
void main(){
int n = to!int(readln.chomp);
foreach(_; 0..n){
auto ns = array(map!(to!int)(readln.chomp.split));
writeln(solve(ns) ? "YES" : "NO");
}
}
bool solve(int[] ns){
foreach(bit; 0..1<<ns.length){
int[2] prev;
bool ok = true;
foreach(i; 0..ns.length){
if(prev[(bit>>i)&1] < ns[i]){
prev[(bit>>i)&1] = ns[i];
}
else{
ok = false;
break;
}
}
if(ok){
return true;
}
}
return false;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum mod = 10L^^9 + 7;
enum inf = 10^^9;
void main() {
int n, ma, mb;
scan(n, ma, mb);
auto a = new int[](n);
auto b = new int[](n);
auto c = new int[](n);
foreach (i ; 0 .. n) {
scan(a[i], b[i], c[i]);
}
auto dp = new int[][][](n + 1, 401, 401);
fillAll(dp, inf);
dp[0][0][0] = 0;
foreach (i ; 1 .. n + 1) {
foreach (ag ; 0 .. 401) {
foreach (bg ; 0 .. 401) {
dp[i][ag][bg] = dp[i-1][ag][bg];
if (ag - a[i-1] >= 0 && bg - b[i-1] >= 0) {
dp[i][ag][bg] = min(dp[i][ag][bg],
dp[i-1][ag - a[i-1]][bg - b[i-1]] + c[i-1]);
}
}
}
}
long ans = inf;
int aa = ma, bb = mb;
while (aa <= 400 && bb <= 400) {
debug {
writeln(dp[n][aa][bb]);
}
ans = min(ans, dp[n][aa][bb]);
aa += ma;
bb += mb;
}
writeln(ans < inf ? ans : -1);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
foreach(_; 0 .. scan!int){
int n = scan!int;
long[] as = scan!long(n);
bool[long] aset;
foreach(a; as) aset[a] = 1;
aset.length.print;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.concurrency;
void times(alias fun)(int n) {
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
long MOD = 10L^^9+7;
void main() {
readln.chomp.to!long.iota.map!"a+1".fold!(
(a, b) => a*b%MOD
)(1L).writeln;
}
// ----------------------------------------------
// 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);
}
}
}
}
// 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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = lreads(N);
A[] -= 1;
auto visited = new bool[](N);
long ans;
long cur = 0;
while (cur != 1)
{
if (visited[cur])
{
writeln(-1);
return;
}
visited[cur] = true;
cur = A[cur];
ans++;
}
writeln(ans);
}
|
D
|
void main()
{
long n = rdElem;
string s = rdStr;
long len = s.length;
foreach (i; 0 .. 4)
{
string t;
t ~= (i & 1 ? 'S' : 'W'), t ~= ((i >> 1) & 1 ? 'S' : 'W');
foreach (j; 1 .. len+1)
{
if ((t[j] == 'S') == (s[j%len] == 'o')) t ~= t[j-1];
else t ~= (t[j-1] == 'S' ? 'W' : 'S');
}
if (t[0..2] != t[len..len+2]) continue;
t[0..len].writeln;
return;
}
writeln(-1);
}
enum long mod = 10L^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
int[] input = readln.split.to!(int[]);
int W = input[0];
int H = input[1];
int N = input[2];
int ans = 0;
int[] point = readln.split.to!(int[]);
foreach(int i; 1..N) {
int[] _point = readln.split.to!(int[]);
int dx = _point[0]-point[0];
int dy = _point[1]-point[1];
if (dx*dy > 0) {
dx = abs(dx);
dy = abs(dy);
ans += min(dx, dy) + max(dx-dy, dy-dx);
} else {
ans += abs(dx)+abs(dy);
}
point = _point;
}
ans.writeln;
}
|
D
|
void main() {
long n = readln.chomp.to!int;
long[] a = readln.split.to!(long[]);
long l, r = a.sum;
long diff = long.max;
foreach (i; 0 .. n) {
if (abs(r-l-2*a[i]) < diff) {
l += a[i];
r -= a[i];
diff = abs(r-l);
} else {
break;
}
}
abs(r-l).writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm,
std.string,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
int n = readln.chomp.to!int;
int m = 1;
while (m < n) {
m *= 2;
}
writeln(m / (m == n ? 1 : 2));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
s[0..$-8].writeln;
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// 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!(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
*/
// }}}
long[] divisor(long n) {
long[] res;
for (long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
res ~= i;
if (i != n / i) {
res ~= n / i;
}
}
}
return res;
}
void main() {
auto cin = new Scanner;
long n;
cin.scan(n);
long res = divisor(n - 1).length - 1;
auto div = divisor(n);
foreach (k; div) {
if (k == 1) continue;
long d = n;
while (d >= k) {
if (d % k == 0) {
d /= k;
} else {
d %= k;
}
}
if (d == 1) res++;
}
writeln(res);
}
|
D
|
void main()
{
long[] tmp = rdRow;
long h = tmp[0], a = tmp[1];
writeln((h + a - 1) / a);
}
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 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.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
auto rd = readln.split.map!(to!int);
auto a = rd[0], b = rd[1], c = rd[2];
writeln(a < b && b < c ? "Yes" : "No");
}
|
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 m = RD;
writeln((n-1)*(m-1));
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int A = to!int(inputs[0]);
int B = to!int(inputs[1]);
if(A % 3 == 0 || B % 3 == 0 || (A + B) % 3 == 0) "Possible".writeln;
else "Impossible".writeln;
}
|
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;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
int test (const int n, const long[] a) {
if (isSorted (a)) return 0;
long d = 1;
long x = 0;
long lastD;
long maxD;
foreach (i; 1 .. n) {
long t = max (0L, (a[i-1] + lastD) - a[i]);
maxD = max (maxD, t);
lastD = t;
}
debug stderr.writeln ("maxD = ", maxD);
foreach (l; 1 .. 50) {
x += d;
if (x >= maxD) return l;
d *= 2;
}
return -1;
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
const n = r.next!uint;
auto a = r.nextA!long(n);
writeln (test (n, a));
}
}
|
D
|
import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;
void main() {
string s = readln.strip;
int w = 0, l = 0;
foreach (i, h; s) {
if (i % 2 == 0 && h == 'p') {
l++;
} else if (i % 2 == 1 && h == 'g') {
w++;
}
}
writeln(w - l);
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void play(){
int n;
n = rd!int;
auto mat = new ll[][](n, n);
foreach(i; 0..n){
mat[i][] = 0;
mat[i][i] = 1;
mat[i][(i + 1) % n] = 1;
}
foreach(i; 0..n){
mat[i].each!(a => write(a, " "));
writeln;
}
}
int main(){
long t = 1;
t = rd; // Toggle!
while(t--) play(); // Let's play!
stdout.flush;
return 0;
}
/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, "x", long, "y");
T max(T = long)(T a, T b){ return (a > b) ? a : b; }
T min(T = long)(T a, T b){ return (a < b) ? a : b; }
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "next", long, "cost");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = new long[](n);
n.iota.each!(x => a[x] = lread());
auto dp = new long[][](n, 2);
dp[0][0] = a[0] / 2;
dp[0][1] = a[0] ? (a[0] - 1) / 2 : -INF;
foreach (i; iota(n - 1))
{
dp[i + 1][0] = max(dp[i][0] + a[i + 1] / 2, dp[i][1] + (a[i + 1] + 1) / 2);
dp[i + 1][1] = a[i + 1] ? max(dp[i][0] + (a[i + 1] - 1) / 2, dp[i][1] + a[i + 1] / 2) : -INF;
}
// dp.each!(x => x.writeln());
max(dp[n - 1][0], dp[n - 1][1]).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
|
void main() {
string s = readln.chomp;
long ind;
long cnt;
long op;
while (ind < s.length)
{
if (s[ind] == 'A')
{
++cnt;
}
else
{
if (ind < s.length - 1 &&s[ind] == 'B' && s[ind+1] == 'C')
{
op += cnt;
++ind;
}
else
{
cnt = 0;
}
}
++ind;
}
op.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias E = Tuple!(int, "to", int, "cap", size_t, "rev");
E[][100+100+2] G;
bool[100+100+2] USED;
void main()
{
auto hw = readln.split.to!(int[]);
auto H = hw[0];
auto W = hw[1];
auto lake = new char[][](H, W);
int sx, sy, tx, ty;
foreach (int y; 0..H) {
foreach (int x, c; readln.chomp.to!(char[])) {
lake[y][x] = c;
if (c == 'S') {
sx = x;
sy = y;
}
if (c == 'T') {
tx = x;
ty = y;
}
if (c == 'o') {
G[x] ~= E(100+y, 1, G[100+y].length);
G[100+y] ~= E(x, 1, G[x].length-1);
}
}
}
G[200] ~= E(sx, 100000, G[sx].length);
G[sx] ~= E(200, 0, 0);
G[200] ~= E(sy+100, 100000, G[sy+100].length);
G[sy+100] ~= E(200, 0, 1);
G[201] ~= E(tx, 0, G[tx].length);
G[tx] ~= E(201, 100000, 0);
G[201] ~= E(ty+100, 0, G[ty+100].length);
G[ty+100] ~= E(201, 100000, 0);
if (sx == tx || sy == ty) {
writeln(-1);
return;
}
int solve(int i) {
if (i == 201) return 1;
USED[i] = true;
foreach (ref e; G[i]) if (e.cap > 0 && !USED[e.to]) {
if (solve(e.to)) {
--e.cap;
++G[e.to][e.rev].cap;
return 1;
}
}
return 0;
}
int r;
while (solve(200)) {
USED[] = false;
++r;
}
writeln(r);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto as=readln.split.to!(int[]);
auto t1=new int[](n), t2=new int[](n);
foreach(i, a; as)
t1[i]=a-1, t2[i]=1_000_000-a;
auto mn=1_000_000_000;
for(int i=0; i<=n; i++){
if(i==0) mn=min(mn, t2[i]);
else if(i==n) mn=min(mn, t1[n-1]);
else mn=min(mn, max(t1[i-1], t2[i]));
}
writeln(mn);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.datastructure.unionfind;
int main() {
auto sc = new Scanner(stdin);
int m, h;
sc.read(m, h);
int n = 2*h+2;
int[][] g = new int[][](n, n);
int id(int x, int y) {
if (y == 0) return (x-1) * 2; //(o, x-1)
return (y-1)*2 + 1; //(□, y-1)
}
foreach (i; 0..m) {
int a, b, c, d;
sc.read(a, b, c, d);
int l = id(a, c);
int r = id(b, d) ^ 1;
debug writeln(l, " ", r);
g[l][r]++;
}
debug foreach (i; 0..n) {
write(i, " ");
write(iota(n).map!(x => g[i][x]).sum);
write(" ");
write(iota(n).map!(x => g[x][i]).sum);
writeln();
}
foreach (i; 0..n-2) {
int ic = iota(n).map!(x => g[i][x]).sum;
int jc = iota(n).map!(x => g[x][i]).sum;
if (i % 2 == 0) {
//left
if (ic < jc) {
writeln("NO");
return 0;
}
g[n-2][i] = ic-jc;
} else {
if (ic > jc) {
writeln("NO");
return 0;
}
g[i][n-1] = jc-ic;
}
}
auto uf = UnionFind(n);
foreach (i; 0..n) {
foreach (j; 0..n) {
if (g[i][j]) uf.merge(i, j);
}
}
foreach (i; 0..n) {
foreach (j; 0..n) {
if (g[i][j] && !uf.same(n-1, i)) {
writeln("NO");
return 0;
}
}
}
writeln("YES");
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/datastructure/unionfind.d */
// module dcomp.datastructure.unionfind;
struct UnionFind {
import std.algorithm : map, swap, each;
import std.range : iota, array;
int[] id;
int[][] groups;
int count;
this(int n) {
id = iota(n).array;
groups = iota(n).map!(a => [a]).array;
count = n;
}
void merge(int a, int b) {
if (same(a, b)) return;
count--;
int x = id[a], y = id[b];
if (groups[x].length < groups[y].length) swap(x, y);
groups[y].each!(a => id[a] = x);
groups[x] ~= groups[y];
groups[y] = [];
}
int[] group(int i) {
return groups[id[i]];
}
bool same(int a, int b) {
return id[a] == id[b];
}
}
|
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;
void solve(ref int[][] g, bool[int[]] obs, int x, int y, int t, int tmax) {
if(t > tmax) return;
if(([x,y] in obs) is null){
//writeln(x, " ", y, " ", t);
if(g[x][y] > t) {
g[x][y] = t;
int[][] dx = [[0, 1], [0, -1], [1, 0], [1, 1], [-1, -1], [-1, 0]];
foreach(dxi; dx) {
solve(g, obs, x+dxi[0], y+dxi[1], t+1, tmax);
}
}
}
}
void main(){
while(true) {
auto tn = readInts();
auto t = tn[0];
auto n = tn[1];
if((t|n) == 0) return;
auto offset = [100, 100];
bool[int[]] obs;
for(int i; i < n; ++i) {
int[] xy = readInts();
xy[] += offset[];
obs[xy.idup] = true;
}
int[] s = readInts();
s[] += offset[];
auto g = new int[][](200,200);
for(int i; i < 200; ++i) {
for(int j; j < 200; ++j) {
g[i][j] = int.max/2;
}
}
solve(g, obs, s[0], s[1], 0, t);
int ans;
foreach(gi; g) {
foreach(gii; gi) {
if(gii <= t) {
++ans;
}
}
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD!uint;
auto A = RDR.ARR!uint(-1);
long[3][3] cnt;
cnt[0][1] = 3;
cnt[0][2] = 4;
cnt[1][0] = 3;
cnt[1][2] = long.min;
cnt[2][0] = 4;
cnt[2][1] = long.min;
long ans;
foreach (i; 0..N-1)
{
ans += cnt[A[i]][A[i+1]];
if (ans < 0) break;
if (i < N - 2)
{
if (A[i] == 2 && A[i+1] == 0 && A[i+2] == 1)
--ans;
}
}
if (ans > 0)
{
writeln("Finite");
writeln(ans);
}
else
{
writeln("Infinite");
}
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string s; readV(s);
writeln(s[0] == s[2] ? "Yes" : "No");
}
|
D
|
/+ dub.sdl:
name "F"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.datastructure.lazyseg;
int main() {
auto sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
int[] l = new int[n], r = new int[n];
int[][] rg = new int[][m+2];
foreach (i; 0..n) {
sc.read(l[i], r[i]);
rg[r[i]] ~= i;
}
auto seg = LazySeg!(int, int, (a, b) => min(a, b), (a, b) => a+b, (a, b) => a+b, 10^^9, 0)(m+2);
foreach (i; 0..m+2) {
seg.add(i, i+1, -(10^^9) + i);
}
int ans = min(n, m);
int off = n;
foreach_reverse (ri; 2..m+2) {
//right use [ri .. m+1]
int buf = m+1 - ri;
//add r[i] = ri
foreach (i; rg[ri]) {
off--;
// writeln("wow ", l[i]);
seg.add(0, l[i], 1);
}
// writeln("debug : ", iota(ri).map!(a => seg[0..a].sum()).map!(to!string).join(" "));
// writeln(buf, " ", off, " ", seg[0..ri-1].sum());
ans = min(ans, buf + off + seg[0..ri-1].sum());
}
writeln(n - ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/datastructure/lazyseg.d */
// module dcomp.datastructure.lazyseg;
struct LazySeg(T, L, alias opTT, alias opTL, alias opLL, T eT, L eL) {
const int n, sz, lg;
T[] d;
L[] lz;
@disable this();
this(int n) {
import std.algorithm : fill, each;
import core.bitop : bsr;
if (n == 0) return;
int lg = n.bsr;
if ((2^^lg) < n) lg++;
this.n = n;
this.sz = 2^^lg;
this.lg = lg;
d = new T[](2*this.sz);
d.each!((ref x) => x = eT);
lz = new L[](2*this.sz);
lz.each!((ref x) => x = eL);
}
private void lzAdd(int k, L x) {
d[k] = opTL(d[k], x);
lz[k] = opLL(lz[k], x);
}
private void push(int k) {
if (lz[k] == eL) return;
lzAdd(2*k, lz[k]);
lzAdd(2*k+1, lz[k]);
lz[k] = eL;
}
T sum(int a, int b, int l, int r, int k) {
if (b <= l || r <= a) return eT;
if (a <= l && r <= b) return d[k];
push(k);
int md = (l+r)/2;
return opTT(sum(a, b, l, md, 2*k),
sum(a, b, md, r, 2*k+1));
}
T single(int k) {
k += sz;
foreach_reverse (int i; 1..lg+1) {
push(k>>i);
}
return d[k];
}
T sum(int a, int b) {
assert(0 <= a && a <= b && b <= n);
return sum(a, b, 0, sz, 1);
}
void add(int a, int b, L x, int l, int r, int k) {
if (b <= l || r <= a) return;
if (a <= l && r <= b) {
lzAdd(k, x);
return;
}
push(k);
int md = (l+r)/2;
add(a, b, x, l, md, 2*k);
add(a, b, x, md, r, 2*k+1);
d[k] = opTT(d[2*k], d[2*k+1]);
}
void add(int a, int b, L x) {
assert(0 <= a && a <= b && b <= n);
add(a, b, x, 0, sz, 1);
}
@property int opDollar() const {return sz;}
struct Range {
LazySeg* seg;
int start, end;
@property T sum() {
return seg.sum(start, end);
}
}
int[2] opSlice(size_t dim)(int start, int end) {
assert(0 <= start && start <= end && end <= sz);
return [start, end];
}
Range opIndex(int[2] rng) {
return Range(&this, rng[0], rng[1]);
}
void opIndexOpAssign(string op : "+")(L x, int[2] rng) {
add(rng[0], rng[1], x);
}
}
|
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 t = RD!int;
auto ans = new long[](t);
foreach (i; 0..t)
{
auto n = RD!int;
auto a = RDA(-1);
auto pos = new size_t[](n);
pos[] = -1;
ans[i] = long.max;
foreach (j, e; a)
{
if (pos[e] != -1)
{
ans[i] = min(ans[i], j - pos[e] + 1);
}
pos[e] = j;
}
if (ans[i] == long.max)
ans[i] = -1;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
void main()
{
string s, t;
ipElems(s, t);
long a, b;
ipElems(a, b);
string u = rdStr;
if (u == s) --a;
if (u == t) --b;
writeln(a, " ", b);
}
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
|
// 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, C;
scan(A, B, C);
if (B < A)
swap(A, B);
writeln(A <= C && C <= B ? "Yes" : "No");
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
int N = to!int(readln.chomp);
int[char] hash;
for (int i = 0; i < N; i++) {
string s = readln.chomp;
if ("MARCH".count(s[0])) {
hash[s[0]]++;
}
}
int[] a = hash.values ~ [0, 0, 0, 0, 0];
long ans = 0;
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
for (int k = j + 1; k < 5; k++) {
ans += 1L * a[i] * a[j] * a[k];
}
}
}
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!(long[]);
auto A = abc[0];
auto B = abc[1];
auto C = abc[2];
writeln((A+B >= C ? C : A+B+1) + B);
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] p = readln.split.to!(int[]);
int cnt;
foreach (i; 1 .. n-1) {
if (p[i-1] < p[i] && p[i] < p[i+1]) ++cnt;
if (p[i-1] > p[i] && p[i] > p[i+1]) ++cnt;
}
cnt.writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
/*
ビットDP
x[m]: ビットマップmで表されるうさぎたちがなすグループの得点
m = (1<<i) | m' であるとき
x[m] = x[m'] + sum(j; (1<<i) & j)(a[i, j])
y[m]: ビットマップmで表されるうさぎたちを最適にグループ分けしたときの得点
y[m] = max(x[m], max(m'; m' subsetof m)(y[m'] + y[m - m']))
※subsetof は、 m - m' == m ^ m' として実装できる
これは(2^16)×(2^16)なのでオーダー的にはTLEだが、がんばれば通るだろう
定数倍をがんばる
*/
long tmp, dt; // 高速化
void main(){
int n = read.to!int;
long[][] as = new long[][](16, 16);
foreach(i; 0 .. n) foreach(j; 0 .. n) as[i][j] = read.to!long;
debug StopWatch sw;
debug sw.start();
long[] xs = new long[](1<<n);
foreach(i; 0 .. n){
foreach(m; 0 .. 1<<i){
xs[1<<i | m] = xs[m];
foreach(j; 0 .. i){
if(1<<j & m) xs[1 << i | m] += as[i][j];
}
}
}
debug writeln("xs:", xs[0 .. 1<<n]);
long[] ys = new long[](1<<n);
foreach(m; 0 .. 1<<n){
ys[m] = xs[m];
int t = 1, u = m - 1; // u は m - t のこと
while(t < u){
if((m ^ t) == u){ // t が m のサブセットであるとき
tmp = ys[t] + ys[u];
if(tmp > ys[m]) ys[m] = tmp;
t += 1, u -= 1;
}
else{
// t の最後の桁と同じだけ上げる(例:10110100 がだめだったら、100の位がだめということなので、+100する)
dt = (t ^ (t - 1)) & t;
t += dt, u -= dt;
}
}
}
debug writeln("ys:", ys[0 .. 1<<n]);
ys[(1<<n) - 1].writeln;
debug writeln(sw.peek().msecs);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!size_t;
auto a = readln.split.to!(int[]);
writeln(a.reduce!max - a.reduce!min);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, A, B;
scan(N, A, B);
writeln((MOD * 2 + powmod(2, N) - 1 - c(N, A) - c(N, B)) % MOD);
}
long c(long n, long k)
{
long a = 1, b = 1;
foreach (i; 0 .. k)
{
a = a * (n - i) % MOD;
b = b * (i + 1) % MOD;
}
return a * powmod(b, MOD - 2) % MOD;
}
/// x^^n % m
T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7)
{
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1, m) % m;
}
T tmp = powmod(x, n / 2, m);
return tmp * tmp % m;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
long i = 1;
long B = long.max;
while (i * i <= N)
{
if (N % i == 0)
{
B = B.min(N / i);
}
i++;
}
writeln(B.to!string().length);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto lr = readln.split.to!(long[]);
auto L = lr[0];
auto R = lr[1]+1;
if (R-L >= 2019L) {
writeln(0);
return;
}
long x = long.max;
foreach (l; L..R) {
foreach (r; l+1..R) {
x = min(x, (l*r)%2019L);
}
}
writeln(x);
}
|
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;
}
int calc(string s) {
int ans = 0;
int lo = 0;
int hi = cast(int) s.length - 1;
while (lo < hi) {
if (s[lo] == s[hi]) {
lo++;
hi--;
}
else if (s[lo] == 'x') {
lo++;
ans++;
}
else if (s[hi] == 'x') {
hi--;
ans++;
}
else {
return -1;
}
}
return ans;
}
void main() {
auto s = readln.chomp;
writeln(calc(s));
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.