code
stringlengths 4
1.01M
| language
stringclasses 2
values |
---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
bool[][] G;
G.length = N;
foreach (i; 0..N) {
G[i].length = N;
foreach (j, x; readln.chomp) {
if (x == '1') G[i][j] = true;
}
}
auto S = new int[](N);
bool check(int i, int p, int s) {
if (S[i]) return S[i] == s;
S[i] = s;
foreach (j, n; G[i]) if (n && i != j && j != p) {
if (check(j.to!int, i, s%2+1) == false) return false;
}
return true;
}
if (check(0, -1, 1) == false) {
writeln(-1);
return;
}
auto M = new int[][](N, N);
foreach (i; 0..N) {
foreach (j, c; G[i]) {
if (i == j) continue;
if (c) {
M[i][j] = 1;
M[j][i] = 1;
} else {
M[i][j] = int.max / 3;
M[j][i] = int.max / 3;
}
}
}
foreach (k; 0..N) {
foreach (i; 0..N) {
foreach (j; 0..N) {
if (M[i][j] > M[i][k] + M[k][j]) {
M[i][j] = M[i][k] + M[k][j];
}
}
}
}
int r;
foreach (i; 0..N) foreach (j; 0..N) r = max(r, M[i][j]);
writeln(r+1);
} | D |
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
auto input = readln.split.to!(int[]);
auto N = input[0], D = input[1];
auto r = 2*D + 1;
((N + r -1) / r).writeln;
}
| D |
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
3
())
---+/
/+---test
6
)))())
---+/
/+---test
8
))))((((
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
const S = readln.chomp;
long dep, lcnt;
foreach (c; S) {
if (c == '(') ++dep;
if (c == ')') {
if (dep > 0) {
--dep;
} else {
++lcnt;
}
}
}
(cycle("(").take(lcnt).array.to!string~S~cycle(")").take(dep).array.to!string).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()
{
writeln("2018" ~ sread()[4 .. $]);
}
| 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[]);
if (tmp[0] == tmp[1]) writeln(tmp[2]);
else if (tmp[1] == tmp[2]) writeln(tmp[0]);
else writeln(tmp[1]);
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib, core.bitop;
void main(){
auto S = scanString;
string NS;
bool B;
for(int i = 0;i < S.length-1;i++)
{
if(S[i] == 'B' && S[i+1] == 'C'){
NS ~= 'D';
i++;
}else{
NS ~= S[i];
}
}
long aCount;
long res;
foreach(c; NS)
{
if(c=='A')
{
aCount++;
}else if(c=='D')
{
res+=aCount;
}else{
aCount=0;
}
}
writeln(res);
}
long gcd(long a, long b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
string scanString()
{
return readln.strip;
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(T[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
//素因数分解
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
//約数をすべて列挙
long[] divisors(long n)
{
long[] list;
void func(Factor[] fs, long n)
{
if(fs.empty){
list ~= n;
return;
}
foreach(c; 0..fs[0].c+1)
{
func(fs[1..$], n * (fs[0].n ^^ c));
}
}
func(factors(n), 1);
sort(list);
return list;
}
//nまでの素数のリスト
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
//素数判定
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
} | D |
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long a = read.to!long;
long b = read.to!long;
long c = read.to!long;
long k = read.to!long;
long ans;
if(k % 2) ans = b - a;
else ans = a - b;
if(ans > 1_000_000_000_000_000_000) writeln("Unfair");
else ans.writeln;
} | D |
// Vicfred
// https://atcoder.jp/contests/abc135/tasks/abc135_c
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
long n = readln.chomp.to!long;
long[] as = readln.split.map!(to!long).array;
long[] bs = readln.split.map!(to!long).array;
long monsters = 0L;
for(long i = n-1; i >= 0L; i--) {
long bugs = min(bs[i], as[i+1]);
//writefln("mate %d bichos", bugs);
monsters += bugs;
as[i+1] -= bugs;
bs[i] -= bugs;
bugs = min(as[i],bs[i]);
//writefln("mate %d bichos", bugs);
monsters += bugs;
bs[i] -= bugs;
as[i] -= bugs;
//as.writeln;
//bs.writeln;
}
monsters.writeln;
}
| 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 = 1L << 50;
enum mod = 10L^^9 + 7;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n-1) {
int x, y;
scan(x, y);
x--, y--;
adj[x] ~= y;
adj[y] ~= x;
}
auto dp = new long[][](n, 2);
fillAll(dp, -1);
long dfs(int v, int p, int col) {
if (dp[v][col] != -1) {
return dp[v][col];
}
dp[v][col] = 1;
foreach (u ; adj[v]) {
if (u == p) continue;
long t = 0;
t += dfs(u, v, 0);
t %= mod;
if (col == 0) {
t += dfs(u, v, 1);
t %= mod;
}
dp[v][col] *= t;
dp[v][col] %= mod;
}
return dp[v][col];
}
auto ans = dfs(0, -1, 0) + dfs(0, -1, 1);
ans %= mod;
debug {
writefln("%(%s\n%)", dp);
}
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 |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == 0) break;
foreach (i; 0..x[0]) {
foreach (j; 0..x[1]) {
if ((i+j)%2) write(".");
else write("#");
}
writeln("");
}
writeln("");
}
} | D |
void main()
{
long n = rdElem;
long[][] lists = new long[][](10, 10);
foreach (i; 1 .. n+1)
{
long f = i;
long b = f % 10;
while (f > 9) f /= 10;
++lists[f][b];
}
long result;
foreach (i; 0 .. 10)
{
foreach (j; 0 .. 10)
{
result += lists[i][j] * lists[j][i];
}
}
result.writeln;
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
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.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.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 x = RD!int;
auto a = RD!int-1;
auto b = RD!int-1;
auto aa = min(a, b);
auto bb = max(a, b);
auto cnt1 = min(aa, x);
x -= cnt1;
aa -= cnt1;
auto cnt2 = min(n-bb-1, x);
bb += cnt2;
ans[i] = bb - aa;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
} | D |
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.range, std.algorithm, std.conv;
// import dcomp.scanner;
// import dcomp.algorithm;
int main() {
auto sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
int[][] g = new int[][](n);
foreach (i; 0..m) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b;
g[b] ~= a;
}
bool[] used = new bool[n];
void visit(int p) {
if (used[p]) return;
used[p] = true;
foreach (d; g[p]) {
visit(d);
}
}
int[] col = new int[n]; col[] = -1;
bool dfs(int p, int c) {
if (col[p] != -1 && col[p] != c) return false;
if (col[p] != -1) return true;
col[p] = c;
foreach (d; g[p]) {
if (!dfs(d, 1-c)) return false;
}
return true;
}
long zc, oc, tc;
foreach (i; 0..n) {
if (g[i].length == 0) {
zc++;
continue;
}
if (used[i]) continue;
visit(i);
if (dfs(i, 0)) {
tc++;
} else {
oc++;
}
}
// writeln(zc, oc, tc);
long ans = 0;
ans += zc * n + n * zc - zc*zc;
ans += (oc + tc) * (oc + tc) + tc * tc;
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
string[] buf;
private bool succ() {
while (!buf.length) {
if (f.eof) return false;
buf = f.readln.split;
}
return true;
}
private bool readSingle(T)(ref T x) {
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
x = buf.front;
buf.popFront;
} else {
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf.map!(to!E).array;
buf.length = 0;
}
} else {
x = buf.front.to!T;
buf.popFront;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
//[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
unittest {
assert(minimum([2, 1, 3]) == 1);
assert(minimum!"a > b"([2, 1, 3]) == 3);
assert(minimum([2, 1, 3], -1) == -1);
assert(minimum!"a > b"([2, 1, 3], 100) == 100);
assert(maximum([2, 1, 3]) == 3);
assert(maximum!"a > b"([2, 1, 3]) == 1);
assert(maximum([2, 1, 3], 100) == 100);
assert(maximum!"a > b"([2, 1, 3], -1) == -1);
}
bool[ElementType!Range] toMap(Range)(Range r) {
import std.algorithm : each;
bool[ElementType!Range] res;
r.each!(a => res[a] = true);
return res;
}
| D |
import std.stdio, std.conv, std.array,std.string;
void main()
{
string[] input=readln.split(",");
writeln(input[0]~" "~input[1]~" "~input[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 n = readln.chomp.to!int;
auto h = readln.chomp.to!int;
auto w = readln.chomp.to!int;
writeln((n-h+1)*(n-w+1));
}
| D |
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto s=readln().chomp().split().map!(to!int);
int a,b,c,d;
a=s[0];
b=s[1];
c=s[2];
d=s[3];
if(a+b>c+d) writeln("Left");
else if(a+b==c+d) writeln("Balanced");
else writeln("Right");
} | 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.to!(char[]).array;
int i, res;
i = res = 0;
while (i != s.length / 2) {
//writeln(i);
if (s[i] != s[$-i-1]) {
if (s[i] == 'x') {
s.insertInPlace(s.length-i, 'x');
} else if (s[$-i-1] == 'x') {
s.insertInPlace(i, 'x');
} else {
res = -1;
break;
}
res++;
}
/*s.writeln;
readln();*/
i++;
}
res.writeln;
}
| D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln(min(readln.chomp.to!int, readln.chomp.to!int) + min(readln.chomp.to!int, readln.chomp.to!int));
}
| D |
import std.stdio;
import std.conv;
import std.string;
void main() {
int S = to!int(chomp(readln()));
int s = S % 60;
S /= 60;
int m = S % 60;
S /= 60;
int h = S;
writeln(to!string(h) ~ ":" ~ to!string(m) ~ ":" ~ to!string(s));
} | D |
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
immutable long mod = 10^^9 + 7;
immutable long inf = mod;
void main(){
int n = readln.chomp.to!int;
int ans;
foreach(i ; 0 .. n){
int x = readln.chomp.to!int;
ans += is_prime(x);
}
writeln(ans);
}
bool is_prime(int x){
for(int i = 2; i*i <= x; ++i){
if (x % i == 0) {
return false;
}
}
return true;
}
T modpow(T)(T x, T y, T mod){
return y ? modpow(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1;
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
} | D |
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 r1, c1, r2, c2;
scan(r1, c1, r2, c2);
auto mc = ModComb(2 * 10^^6 + 100);
long ans;
foreach (i ; c1 .. c2 + 1) {
ans += mc.c(r2 + i + 1, r2) - mc.c(r1 + i, r1 - 1) + mod;
ans %= mod;
}
writeln(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) {
if (k < 0 || k > n) return 0;
return f(n) * finv(n - k) % _mod * finv(k) % _mod;
}
long p(int n, int r) {
if (r < 0 || r > n) return 0;
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;
}
}
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;
// }}}
// 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
*/
// }}}
void main() {
auto cin = new Scanner;
int n, k;
cin.scan(n, k);
int[] x = cin.nextArray!int(n);
int res;
foreach (i; 0 .. n) {
res += min(x[i] * 2, abs(x[i] - k) * 2);
}
writeln(res);
}
| D |
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
3.iota.map!(i => readln.chomp[i]).array.writeln;
}
| D |
void main() {
string s = readln.chomp;
foreach (x; lowercase) {
if (!s.canFind(x)) {
x.writeln;
break;
}
if (x == 'z') "None".writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni; | D |
void main() {
problem();
}
void problem() {
const S = scan;
auto N = S.length;
long solve() {
if (N < 4) return 0;
const P = 2019;
long ans;
auto cs = new long[](P);
cs[0] = 1;
long x, t = 1;
foreach_reverse (c; S) {
x = ((c-'0').to!long * t + x) % P;
t = (t * 10) % P;
ans += cs[x];
++cs[x];
}
return ans;
}
long solve2() {
const MOD = 2019;
auto countsPerMod = new long[MOD];
long answer;
countsPerMod[0] = 1;
long acc;
foreach(i, c; S) {
const number = c - '0';
auto newCountsPerMod = new long[MOD];
foreach(m; 0..MOD) {
newCountsPerMod[(10 * m) % MOD] += countsPerMod[m];
}
acc = (acc * 10 + number) % MOD;
answer += newCountsPerMod[acc];
newCountsPerMod[acc]++;
countsPerMod = newCountsPerMod;
}
return answer;
}
solve2().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");
import std.bigint, std.functional;
// -----------------------------------------------
| D |
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
void main()
{
long n, m;
scan(n, m);
//writeln(n, m);
auto color = new bool[](n);
color[0] = true;
auto cup = new long[](n);
cup[] = 1;
foreach (i; 0 .. m)
{
long x, y;
scan(x, y);
//writeln(x, y);
cup[x - 1] -= 1;
cup[y - 1] += 1;
if (color[x - 1])
{
color[y - 1] = true;
}
if (cup[x - 1] == 0)
{
color[x - 1] = false;
}
}
// writeln(cup);
// writeln(color);
auto cnt = 0L;
foreach (i; 0 .. n)
{
if (cup[i] != 0 && color[i])
{
cnt += 1;
}
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
| D |
import std.stdio;
void main()
{
for (int i = 0; i < 1000; i++) {
writeln("Hello World");
}
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
int d, x;
scan(d, x);
foreach (i ; 0 .. n) {
int ai;
scan(ai);
x += (d + ai - 1) / ai;
}
writeln(x);
}
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.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.replace(" ", "").to!int % 4 ? "NO" : "YES");
}
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;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
char[] s = readln.chomp.to!(char[]);
long k = readln.chomp.to!long;
foreach(i, c; s){
int x = 'z' + 1 - c;
if(s[i] > 'a' && x <= k){
s[i] = 'a';
k -= x;
}
}
s[$ - 1] = 'a' + (cast(long)(s[$ - 1] - 'a') + k) % 26;
s.writeln;
} | D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
int a, b, c;
scan(a, b, c);
int k;
scan(k);
int cnt;
while (a >= b) {
b *= 2;
cnt++;
}
while (b >= c) {
c *= 2;
cnt++;
}
yes(cnt <= k);
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
| D |
import std.stdio;
import std.string;
void main() {
string ans = "WA";
string s = readln.strip;
if (s[0] == 'A') {
ulong c = 0;
foreach(i, e; s[0..$ - 1]) {
if (e == 'C' && i > 1) {
c = i;
break;
}
}
if (c > 1) {
ans = "AC";
foreach(i, e; s) {
if (i == 0 || i == c)
continue;
if (e < 97 || e > 122) {
ans = "WA";
break;
}
}
}
}
writeln(ans);
} | D |
class UnionFind{
int[] root;
this(int n){
root.length=n;
foreach(i; 0..n) root[i]=i;
}
int find(int x){
if(x==root[x]) return x;
else return root[x]=find(root[x]);
}
void merge(int x, int y){
x=find(x); y=find(y);
if(x==y) return;
root[x]=y;
}
}
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, k, l; rd(n, k, l);
auto s1=new UnionFind(n), s2=new UnionFind(n);
foreach(_; 0..k){
int a, b; rd(a, b);
s1.merge(a-1, b-1);
}
foreach(_; 0..l){
int a, b; rd(a, b);
s2.merge(a-1, b-1);
}
struct Pair{int fi, se;}
int[Pair] freq;
foreach(i; 0..n){
auto pair=Pair(s1.find(i), s2.find(i));
if(pair in freq) freq[pair]++;
else freq[pair]=1;
}
foreach(i; 0..n){
writeln(freq[Pair(s1.find(i), s2.find(i))]);
}
}
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 core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a, b, c, d;
scan(a, b, c, d);
long e = gcd(c, d);
e = min(c, d) * (max(c, d) / e);
long f = b / c - a / c + !(a % c);
long g = b / d - a / d + !(a % c);
long h = b / e - a / e + !(a % c);
long i = f + g - h;
(b - a - i + 1).max(0).writeln();
}
| D |
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container;
int n, k, c;
bool[] solve() {
scanf("%d %d %d\n", &n, &k, &c);
string s = readln.chomp;
bool[] banned = s.map!(c => c=='x').array;
int[] l = new int[k];
int k_cnt = 0;
for(int i=0; i<n; i++) {
if(!banned[i]) {
l[k_cnt] = i;
i += c;
if(++k_cnt == k) break;
}
}
if(k_cnt < k) return new bool[n];
int[] r = new int[k];
k_cnt = k;
for(int i=n-1; i>=0; i--) {
if(!banned[i]) {
r[--k_cnt] = i;
i -= c;
if(k_cnt == 0) break;
}
}
bool[] result = new bool[n];
for(int i=0; i<l.length; i++) {
result[l[i]] = l[i]==r[i];
}
return result;
}
void main() {
auto res = solve();
for(int i=0; i<res.length; i++) {
if(res[i]) writeln(i+1);
}
}
| D |
import std.stdio;
//import std.string;
//import std.conv;
import core.stdc.stdio;
void main(){
//int a = to!int(chomp(readln()));
//int b = to!int(chomp(readln()));
int a;
int b;
scanf("%d",&a);
scanf("%d",&b);
if(a == b)
writeln("a == b");
else
writeln((a < b) ? "a < b" : "a > b");
} | D |
import std.stdio;
import std.string;
import std.conv;
int main()
{
while (true) {
string[] str = readln().split();
if (str[0] == "0" && str[1] == "0") break;
for (int i = 0; i < str[0].to!int(); i++) {
for (int j = 0; j < str[1].to!int(); j++) {
write("#");
}
writeln();
}
writeln();
}
return 0;
} | D |
void main() {
(700 + readln.chomp.count!"a == 'o'" * 100).writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
| D |
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.math;
void main()
{
string w = chomp(readln());
int[26] alphabet_list;
for(int i = 0;i < w.length;i ++)
{
alphabet_list[to!int(w[i])-97] ++;
}
int odd_num_count = 0;
for(int i=0;i < 26;i ++)
{
if(alphabet_list[i]%2 != 0)
{
odd_num_count ++;
}
}
if(odd_num_count == 0)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string;
int[(10^^5)*2] AS;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) AS[i] = readln.chomp.to!int;
if (AS[0]) {
writeln("-1");
return;
}
long cnt;
foreach (i; 1..N) {
if (AS[i] > AS[i-1]+1) {
writeln("-1");
return;
}
if (AS[i] == AS[i-1]+1) {
++cnt;
} else {
cnt += AS[i];
}
}
writeln(cnt);
} | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int a;
rd(a);
int digit_sum(int x) {
int ret = 0;
while (x) {
ret += x % 10;
x /= 10;
}
return ret;
}
for (int n = a;; n++) {
if (digit_sum(n) % 4 == 0) {
writeln(n);
break;
}
}
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
| 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];
writeln(a * b, " ", (a + b) * 2);
} | D |
import std.stdio;
import std.string;
import std.array;
import std.range;
import std.algorithm;
import std.conv;
void main(string[] args) {
readln();
string input = readln().chomp;
int[2][char] range;
foreach(int i, char c; input) {
if(c in range){
range[c][1] = i;
}else{
range[c] = [i,0];
}
}
int[100] res;
foreach(char c, int[2] a; range){
for(int i = a[0]; i < a[1]; i++){
res[i]++;
}
}
res.reduce!max.writeln;
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import std.math;
void main(){
auto z=readln.split.to!(int[]),a=z[0],b=z[1];
if(a<=b)writeln(a);
else writeln(a-1);
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (i; 0..t)
{
auto x = RD;
auto y = RD;
auto d = x - y;
if (d == 1)
ans[i] = false;
else
ans[i] = true;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush();
debug readln();
} | D |
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
//END OF TEMPLATE
void main(){
long a,b,c,k;
scan(a,b,c,k);
if(k<=a){
k.writeln;
}else if(k<=a+b){
a.writeln;
}else{
(a-(k-a-b)).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 = lread();
auto A = lreads(N);
writeln(A.any!"(a&1)!=0" ? "first" : "second");
}
| 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;
bool calc(string s) {
if (s.startsWith("keyence")) return true;
if (s.startsWith("k") && s.endsWith("eyence")) return true;
if (s.startsWith("ke") && s.endsWith("yence")) return true;
if (s.startsWith("key") && s.endsWith("ence")) return true;
if (s.startsWith("keye") && s.endsWith("nce")) return true;
if (s.startsWith("keyen") && s.endsWith("ce")) return true;
if (s.startsWith("keyenc") && s.endsWith("e")) return true;
if (s.endsWith("keyence")) return true;
return false;
}
void main() {
string s = read!string;
writeln(calc(s) ? "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; }
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;
long[] arr;
arr ~= RD;
arr ~= RD;
arr ~= RD;
arr ~= RD;
arr ~= RD;
long ans;
long cnt;
foreach (i; 0..5)
{
if (i != 0)
{
arr[i] = min(arr[i], arr[i-1]);
}
if (i == 4)
{
cnt += N / arr[i];
cnt += N % arr[i] != 0 ? 1 : 0;
}
}
writeln(cnt + 4);
stdout.flush();
debug readln();
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
void main()
{
auto b = iota(42, 133, 10);
auto ptr = new int[](10);
auto str = readln.chomp;
int pos;
foreach (c; str) {
int cur;
foreach (e; b) {
if (c <= e) break;
cur++;
}
while (cur < pos) {
write("<");
pos--;
}
while (cur > pos) {
write(">");
pos++;
}
while (ptr[pos] < c) {
write("+");
ptr[pos]++;
}
while (ptr[pos] > c) {
write("-");
ptr[pos]--;
}
write(".");
}
} | 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;
string ans;
foreach (c; S)
{
if (c == 'B')
{
if (ans.length != 0)
ans.popBack;
}
else
ans ~= c;
}
writeln(ans);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n-1) {
int ai,bi;
scan(ai,bi);
adj[ai] ~= bi;
adj[bi] ~= ai;
}
int dfs(int v, int p) {
if (adj[v].length == 1) {
return 0;
}
int res, dmax;
bool zero;
foreach (u ; adj[v]) {
if (u == p) continue;
int d = dfs(u, v);
if (!d) {
zero = 1;
d++;
}
res += d;
}
if (zero) res--;
return res;
}
int ans = 1;
foreach (i ; 0 .. n) {
if (adj[i].length > 2) {
ans = dfs(i, -1);
break;
}
}
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 |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
readln;
auto AS = readln.split.to!(int[]);
long ret;
foreach (a; AS) {
while (!(a&1)) {
a >>= 1;
++ret;
}
}
writeln(ret);
} | 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;
void main(){
auto ip = readln.split, s = ip[1];
writeln("A", s[0], "C");
} | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto xy = readln.split.to!(int[]);
auto X = xy[0];
auto Y = xy[1];
writeln(X + Y / 2);
} | D |
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto p = readln.chomp;
foreach (i; 0..s.length)
if ((s[i..$] ~ s[0..i]).canFind(p)) {
writeln("Yes");
return;
}
writeln("No");
} | 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;
bool calc(string s) {
for (int i = 0; i < s.length; i++) {
if (i % 2 == 0) {
if (s[i] == 'L') return false;
}
else if (s[i] == 'R') return false;
}
return true;
}
void main() {
string s = read!string;
writeln(calc(s) ? "Yes" : "No");
}
| D |
void main(){
int k,a,b;
k = _scan();
scanf("%d %d", &a, &b);
for(int i=a; i<=b; i++){
if( i%k==0 ){
writeln("OK");
return;
}
}
writeln("NG");
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
import std.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() {
int[] as = readln.chomp.map!(a => a=='1'?1:0).array;
if (as[0]==1) {
foreach(i, a; as) {
as[i] = a==1?0:1;
}
}
int N = as.length.to!int;
bool f(int c) {
int[] bs = as.dup;
int k = 0;
foreach(i; 0..c) {
if ((bs[i]+k)%2==1 && i+c<=N) {
k++;
}
if ((bs[i]+k)%2==0) {
} else {
return false;
}
}
return true;
}
int l = 0;
int r = N+1;
while(r-l>1) {
int c = (r + l)/2;
if (f(c)) {
l = c;
} else {
r = c;
}
}
l.writeln;
}
// ----------------------------------------------
void scanln(Args...)(ref Args args) {
foreach(i, ref v; args) {
"%d".readf(&v);
(i==args.length-1 ? "\n" : " ").readf;
}
// ("%d".repeat(args.length).join(" ") ~ "\n").readf(args);
}
void times(alias fun)(int n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(int n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
auto minElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto minimum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b < minimum) {
element = a;
minimum = b;
}
}
return element;
}
auto maxElement(alias map, Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
alias mapFun = unaryFun!map;
auto element = r.front;
auto maximum = mapFun(element);
r.popFront;
foreach(a; r) {
auto b = mapFun(a);
if (b > maximum) {
element = a;
maximum = b;
}
}
return element;
}
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int n;
scan(n);
int pmax = 0;
int ps = 0;
foreach (i ; 0 .. n) {
int pi;
scan(pi);
ps += pi;
pmax = max(pmax, pi);
}
ps -= pmax / 2;
writeln(ps);
}
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.array;
import std.math;
void main(){
double r = readln.split[0].to!double;
r = 2 * PI * r;
printf("%lf\n",r);
} | D |
void main() {
dchar[] n = readln.chomp.to!(dchar[]);
auto g = n.group.array;
writeln(g.any!(t => t[1] >= 3) ? "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;
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()
{
int sum, tmp;
foreach (i; 0..5) {
auto o = readln.chomp.to!int;
if (o % 10 == 0) {
sum += o;
} else {
auto t = 10 - o % 10;
tmp = max(t, tmp);
sum += o + t;
}
}
writeln(sum - tmp);
}
| D |
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long a, b;
scan(a, b);
writeln(a * b);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
| 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 S = new string[](N);
foreach (i; 0 .. N)
S[i] = sread();
long ans;
outer: foreach (i; 0 .. N)
{
foreach (j; 0 .. N)
foreach (k; 0 .. N)
{
if (S[(j + i) % N][k] != S[(k + i) % N][j])
{
// writefln("%s %s %s %s %s", i, j, k, S[(j + i) % N][k], S[k][(j + i) % N]);
continue outer;
}
}
ans += N;
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto S = readln.chomp;
auto L = S.length;
int distinct_count;
foreach(i; 0..L/2) {
if (S[i] != S[L-i-1]) distinct_count++;
}
writeln(distinct_count);
}
| D |
void main() {
auto S = rs;
if(S[2] == S[3] && S[4] == S[5]) writeln("Yes");
else writeln("No");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
} | D |
import std.stdio,std.array;
void main(){
string[] n = split(readln());
int sevenCount = 0;
for(int i=0;i<3;i++){
if("7" == n[i]) sevenCount++;
}
int fiveCount =0;
for(int i=0;i<3;i++){
if("5" == n[i]) fiveCount++;
}
if(fiveCount == 2 && sevenCount == 1){
writeln("YES");
}else{
writeln("NO");
}
}
| D |
void main() {
auto N = ri;
auto S = rs;
auto K = ri;
auto c = S[K-1];
foreach(v; S) {
if(v == c) c.write;
else '*'.write;
}
writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
} | D |
void main() {
int n = readln.chomp.to!int;
string s = readln.chomp;
string t1 = s[0..n/2], t2 = s[n/2..$];
writeln(n % 2 == 0 && t1 == t2 ? "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.string,std.conv;
int main()
{
string s;
while((s = readln.chomp).length != 0)
{
double d = s.to!double;
if(91.00 < d) "heavy".writeln;
if(81.00 < d && d <= 91.00) "light heavy".writeln;
if(75.00 < d && d <= 81.00) "middle".writeln;
if(69.00 < d && d <= 75.00) "light middle".writeln;
if(64.00 < d && d <= 69.00) "welter".writeln;
if(60.00 < d && d <= 64.00) "light welter".writeln;
if(57.00 < d && d <= 60.00) "light".writeln;
if(54.00 < d && d <= 57.00) "feather".writeln;
if(51.00 < d && d <= 54.00) "bantam".writeln;
if(48.00 < d && d <= 51.00) "fly".writeln;
if(d <= 48.00) "light fly".writeln;
}
return 0;
} | D |
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
auto n=readln().chomp.to!int;
auto k=readln().chomp.to!int;
auto x=readln().chomp.to!int;
auto y=readln().chomp.to!int;
writeln(min(n,k)*x+max(0,n-k)*y);
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container;
// 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;
// dfmt on
void main()
{
long H, W;
scan(H, W);
long[2][] black;
auto A = new bool[][](H, W);
foreach (i; 0 .. H)
{
auto s = sread();
foreach (j, c; s)
if (c == '#')
{
black ~= [i, j];
A[i][j] = true;
}
}
long cnt;
while (true)
{
long[2][] tmp;
foreach (b; black)
{
long y = b[0];
long x = b[1];
if (0 < y && !A[y - 1][x])
{
A[y - 1][x] = true;
tmp ~= [y - 1, x];
}
if (0 < x && !A[y][x - 1])
{
A[y][x - 1] = true;
tmp ~= [y, x - 1];
}
if (y + 1 < H && !A[y + 1][x])
{
A[y + 1][x] = true;
tmp ~= [y + 1, x];
}
if (x + 1 < W && !A[y][x + 1])
{
A[y][x + 1] = true;
tmp ~= [y, x + 1];
}
}
if (tmp.empty)
{
writeln(cnt);
return;
}
else
{
cnt++;
black = tmp;
}
}
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
iota(2).map!(i => readln.chomp.to!int).reduce!((a, b) => a^^2 - b).writeln;
}
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.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
foreach (i; 0..S.length) {
if (S[i..$] ~ S[0..i] == T) {
writeln("Yes");
return;
}
}
writeln("No");
}
| D |
import std.stdio;
import std.math;
import std.conv;
void main(){
int n;
scanf("%d", &n);
int digit = log10(n.to!float).floor.to!int;
if (digit == 0) {
write(n);
}
else if (digit == 1) {
write(9);
}
else if (digit == 2) {
write(9 + (n-99));
}
else if (digit == 3) {
write(9 + 900);
}
else if (digit == 4) {
write(9 + 900 + (n-9999));
}
else {
write(9 + 900 + 90000);
}
}
| D |
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
void main()
{
int A, B, r, K;
auto input = readln.split.to!(int[]);
A = input[0];
B = input[1];
K = input[2];
while(A%B)
{
r = A%B;
A = B;
B = r;
}
int[] ans = new int[100];
ans[0] = B;
int j = 1;
for(int i = 2; i <= B; i++) {
if(B%i == 0)
{
ans[j] = B / i;
j++;
}
}
writeln(ans[K-1]);
} | D |
import std.stdio;
void main() {
foreach (i; 1..10) {
foreach (j; 1..10) {
write(i); write("x"); write(j); write("="); writeln(i*j);
}
}
} | D |
import std.stdio;
void main(){
foreach(x;1..10)foreach(y;1..10)writeln(x,"x",y,"=",x*y);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.algorithm;
immutable MAX_SCORE = 20;
immutable MAX_CARDS = 10;
void main() {
string[] input;
while ((input = readln.chomp.split(" ")).length != 0) {
auto cards = new bool[](10); //未出現カード
auto openedCards = input.map!(to!int); //0,1:自分の手札, 2:相手の公開札
auto deck = MAX_CARDS - (openedCards.length); //山札の残り枚数
auto myScore = openedCards[0] + openedCards[1]; //現在の手札の合計
auto count = 0;
cards.fill(true);
foreach (i; openedCards) {
cards[i - 1] = false;
}
foreach (i; 0..(MAX_SCORE - myScore)) {
if (i >= 10) break;
if (cards[i]) count++;
}
if ((count.to!double / deck) >= 0.50) {
writeln("YES");
} else {
writeln("NO");
}
}
} | D |
import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
int correct_count = 0;
for(int i=0; i<S.length; i++) {
if (S[i] == T[i]) correct_count++;
}
writeln(correct_count);
}
| 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 R, G, B, N;
scan(R, G, B, N);
long ans;
foreach (r; 0 .. N / R + 1)
{
foreach (g; 0 .. (N - r * R) / G + 1)
{
if ((N - r * R - g * G) % B == 0)
{
// writefln("%s %s %s", r, g, (N - r * R - g * G) / B);
ans++;
}
}
}
writeln(ans);
}
| D |
// Vicfred
// https://atcoder.jp/contests/abc152/tasks/abc152_c
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
int[] p = readln.split.map!(to!int).array;
int minima = 1<<30;
int answer = 0;
foreach(item; p) {
if(item <= minima) {
minima = item;
answer += 1;
}
}
answer.writeln;
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto M = s[0].to!long;
auto N = s[1];
auto P = new int[](N);
long x;
int[] Q;
int debug_cnt = 0;
debug {
x = readln.chomp.to!long;
Q = N.iota.map!(_ => readln.chomp.to!int).array;
}
int ask(long y) {
writeln(y);
stdout.flush;
debug {
if (x < y) return -1 * Q[debug_cnt];
else if (x > y) return 1 * Q[debug_cnt];
else return 0;
++debug_cnt;
}
return readln.chomp.to!int;
}
foreach (i; 0..N) {
P[i] = ask(1);
if (P[i] == 0)
return;
}
long hi = M+1;
long lo = 0;
int cnt = 0;
while (hi - lo > 1) {
long mid = (hi + lo) / 2;
int ret = ask(mid) * P[cnt%N];
if (ret == 1) {
lo = mid;
} else if (ret == -1) {
hi = mid;
} else {
return;
}
++cnt;
}
}
| D |
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;
void main() {
immutable n = readln.strip.to!int;
auto s = readln.take(n).array;
int[] st;
auto x = new int[n];
auto ans = new char[n];
foreach (i; 0 .. n) {
if (s[i] == '(') {
st ~= i;
ans[i] = (st.length & 1) ? '0' : '1';
} else {
int j = st.back;
st.popBack ();
x[i] = j;
x[j] = i;
ans[i] = ans[j];
}
}
writeln (ans);
}
| D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
string f0 = "What are you doing at the end of the world? Are you busy? Will you save us?";
string str1 = "What are you doing while sending \"";
string str2 = "\"? Are you busy? Will you send \"";
string str3 = "\"?";
long[] flen;
unittest {
assert(str1.length == 34);
}
void main() {
int q;
scan(q);
int n;
long k;
auto ans = new char[](q);
flen = new long[](53 + 1);
flen[0] = 75;
foreach (i ; 1 .. 54) {
flen[i] = 68 + 2*flen[i-1];
}
debug {
writefln("%(%s\n%)", flen);
}
foreach (i ; 0 .. q) {
scan(n, k);
bool b;
while (n > 53) {
n--;
k -= 34;
if (k <= 0) {
k += 34;
b = 1;
break;
}
}
if (b) {
ans[i] = str1[(k-1).to!uint];
}
else {
ans[i] = ask(n, k);
}
}
writeln(ans);
}
char ask(int n, long k) {
if (n == 0) {
if (k > f0.length) {
return '.';
}
else {
return f0[(k - 1).to!uint];
}
}
else {
int d1 = str1.length.to!int;
int d2 = str2.length.to!int;
int d3 = str3.length.to!int;
if (1 <= k && k <= d1) {
return str1[(k-1).to!uint];
}
else if (d1 < k && k <= d1 + flen[n-1]) {
return ask(n-1, k-d1);
}
else if (d1 + flen[n-1] < k && k <= d1 + d2 + flen[n-1]) {
return str2[(k - (d1 + flen[n-1])).to!uint - 1];
}
else if (d1 + d2 + flen[n-1] < k && k <= d1 + d2 + 2*flen[n-1]) {
return ask(n-1, k-(d1 + d2 +flen[n-1]));
}
else if (d1 + d2 + 2*flen[n-1] < k && k <= d1 + d2 + d3 + 2*flen[n-1]){
return str3[(k - (d1 + d2 + 2*flen[n-1])).to!uint - 1];
}
else {
return '.';
}
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
} | D |
import std.stdio;
import std.algorithm;
void main()
{
long x1, x2, y1, y2;
scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2);
long dx = (x2 - x1) / 2;
long dy = (y2 - y1) / 2;
printf("%lld\n", (dx + 1) * (dy + 1) + dx * dy);
} | D |
module main;
import core.stdc.stdio;
int main(string[] argv)
{
int n;
scanf("%d", &n);
if(n%2==1){
printf("7");
}else{
printf("1");
}
for(int i = 0; i<n/2-1; i++)
printf("1");
return 0;
} | D |
import std.stdio;
void main()
{
int n;
int res = 0;
char[200 + 1] s;
scanf("%d%s", &n, &s[0]);
for (int i = 0; i < n; i++) {
int vertical = 0;
int horizontal = 0;
for (int j = i; j < n; j++) {
if (s[j] == 'U')
vertical--;
if (s[j] == 'D')
vertical++;
if (s[j] == 'L')
horizontal--;
if (s[j] == 'R')
horizontal++;
if (vertical == 0 && horizontal == 0)
res++;
}
}
printf("%d\n", res);
}
| D |
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0];
auto A = s[1];
auto B = s[2];
auto C = s[3];
auto D = s[4];
long lb = A - (N - 1) * D;
long ub = A - (N - 1) * C;
foreach (i; 0..N) {
if (lb <= B && B <= ub) {
"YES".writeln;
return;
}
lb += C + D;
ub += C + D;
//writeln(i, " " , lb, " ", ub);
}
"NO".writeln;
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
long ans;
foreach (i; 1..N+1)
{
if (i % 5 && i % 3)
ans += i;
}
writeln(ans);
stdout.flush;
debug readln;
} | D |
import std.stdio;
long div = 1_000_000_007;
void main() {
long n, m;
scanf("%ld %ld\n", &n, &m);
auto dp = new long[n+2];
dp[0] = 1;
auto check = new bool[1_0000];
long before = -1;
foreach(_; 0..m) {
long a;
scanf("%ld\n", &a);
assert(before < a);
before = a;
check[a] = true;
}
foreach(i; 0..n) {
if (check[i]) continue;
dp[i+1] = (dp[i+1] + dp[i]) % div;
dp[i+2] = (dp[i+2] + dp[i]) % div;
}
dp[n].write;
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
auto X = RD;
long ans;
foreach (i; 0..A+1)
{
foreach (j; 0..B+1)
{
foreach (k; 0..C+1)
{
if (i * 500 + j * 100 + k * 50 == X)
++ans;
}
}
}
writeln(ans);
stdout.flush();
debug readln();
} | D |
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto I = readln.split.to!(long[]);
auto W = I[0];
auto H = I[1];
long solve() {
if (W == 1 || H == 1) return 1;
long ans;
ans += (1 + (W - 1)/2) * (1 + (H - 1)/2);
ans += (W / 2) * (H / 2);
return ans;
}
solve().writeln();
}
| D |
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
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() {
Scanner sc = new Scanner;
int A, P;
sc.scan(A, P);
P += (A * 3);
writeln(P / 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;
void main() {
int n;
scan(n);
int ans = n;
foreach (a ; 0 .. n + 1) {
int tmp;
int b = n - a;
while (a > 0) {
tmp += a % 6;
a /= 6;
}
while (b > 0) {
tmp += b % 9;
b /= 9;
}
ans = min(ans, tmp);
}
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 |
import core.stdc.stdio;
import std.algorithm;
void main(){
int n,m;
scanf("%d%d",&n,&m);
int[] p = new int[m];
foreach(ref v;p)
scanf("%d",&v);
int[] a=new int[n-1],b=new int[n-1],c=new int[n-1];
foreach(i;0..n-1)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
int[] sum=new int[n-1];
foreach(i;0..m-1){
int s=p[i]-1,t=p[i+1]-1;
if(s>t)
swap(s,t);
sum[s]++;
if(t<n-1)
sum[t]--;
}
foreach(i;1..n-1)
sum[i]+=sum[i-1];
long ans;
foreach(i;0..n-1)
ans+=min(cast(long)a[i]*sum[i],cast(long)b[i]*sum[i]+c[i]);
printf("%lld\n",ans);
} | D |
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;
void main()
{
auto n = map!(to!int)(readln.strip.split);
while(n[0]!=-1 || n[1] != -1 || n[2] != -1) {
string grade;
int m=n[0],f=n[1],r=n[2],score=0;
score = m+f;
if(m==-1 || f ==-1)
{
grade = "F";
}
else if(score >= 80)
{
grade = "A";
}
else if(score < 80 && score >= 65)
{
grade = "B";
}
else if(score < 65 && score >= 50)
{
grade = "C";
}
else if(score < 50 && score >= 30)
{
if(r>=50)
grade = "C";
else
grade = "D";
}
else
grade = "F";
writeln(grade);
n = map!(to!int)(readln.strip.split);
}
} | D |
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.range;
void main() {
string s = readln.strip;
long cnt = 0;
char[] last = new char[](10000);
long p = -1;
for (long i = 0; i < s.length; i++) {
if (i+1 < s.length && s[i] != s[i+1]) {
cnt += 2;
i++;
} else {
if (p >= 0 && last[p] != s[i]) {
cnt += 2;
p--;
} else {
p++;
last[p] = s[i];
}
}
}
writeln(cnt);
}
| 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[long] D;
foreach (n; 1 .. N + 1)
{
long i = 2;
while (n != 1)
{
// writeln(n);
if (n < i * i)
{
D[n]++;
n = 1;
}
while (n % i == 0)
{
n /= i;
D[i]++;
}
i++;
}
}
long ans = 1;
// writeln(D);
foreach (v; D.values)
{
ans = ans * (v + 1) % MOD;
}
writeln(ans);
}
| D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.