code stringlengths 4 1.01M | language stringclasses 2
values |
|---|---|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
int n, m;
scan(n, m);
auto cnt = new int[](n);
auto red = new bool[](n);
cnt[] = 1;
red[0] = 1;
foreach (i ; 0 .. m) {
int xi, yi;
scan(xi, yi);
xi--, yi--;
cnt[xi]--;
cnt[yi]++;
if (red[xi]) red[yi] = 1;
if (cnt[xi] == 0) red[xi] = 0;
}
auto ans = iota(n).count!(i => red[i]);
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.algorithm;
import std.range;
import std.stdio;
import std.string;
immutable string vowels = "aeiou";
void main ()
{
string s;
while ((s = readln.strip) != "")
{
string [] res;
main_loop:
while (!s.empty)
{
foreach (i; 2..s.length)
{
if (!vowels.canFind (s[i - 2]) &&
!vowels.canFind (s[i - 1]) &&
!vowels.canFind (s[i - 0]) &&
(s[i - 2] != s[i - 1] ||
s[i - 1] != s[i - 0]))
{
res ~= s[0..i];
s = s[i..$];
continue main_loop;
}
}
res ~= s;
s = "";
}
res.join (" ").writeln;
}
}
| D |
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
// 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;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
string s;
sc.scan(s);
int res = 0;
foreach (i; 0 .. 3) {
if (s[i] == '1')
res++;
}
writeln(res);
}
| 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();
string S = sread();
long[] T = new long[N + 1];
foreach (i; 0 .. N)
T[i + 1] = T[i] + (S[i] == '#');
// writeln(S);
// writeln(T);
long ans = long.max;
foreach (i; 0 .. N)
{
long a = (T[i + 1]);
long b = ((N - i - 1) - (T[$ - 1] - T[i]));
// writefln("%d %d %d", i, a, b);
ans = ans.min(a + b);
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read.to!int;
if(n % 2){
0.writeln;
return;
}
long ans = 1;
foreach(i; 0 .. n / 2){
ans *= 2;
}
ans.writeln;
}
| D |
void main() {
problem();
}
void problem() {
auto X = scan!char;
string solve() {
return 'A' <= X && X <= 'Z' ? "A" : "a";
}
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "a", long, "b");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n, m;
scan(n, m);
auto from = new bool[](n);
auto to = new bool[](n);
foreach (_; iota(m))
{
long a, b;
scan(a, b);
if (a == 1)
to[b - 1] = true;
if (b == n)
from[a - 1] = true;
}
long ans;
foreach (i; iota(n))
{
ans |= (from[i] && to[i]);
}
if (ans)
writeln("POSSIBLE");
else
writeln("IMPOSSIBLE");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
| D |
// import 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;
}
}
// }}}
void main() {
auto cin = new Scanner;
long n, k;
cin.scan(n, k);
long w = n / k;
writeln(min(n - w * k, abs(n - (w + 1) * k)));
}
| D |
import std.stdio, std.string, std.conv, std.algorithm;
void main() {
int[] tmp = readln.split.to!(int[]);
int p = tmp[0], q = tmp[1], r = tmp[2];
writeln(min(p+q, q+r, r+p));
} | D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
int solve (string s)
{
s = s.strip ('1');
if (s.empty)
{
return 0;
}
if (s.all !(q{a == '0'}))
{
return 1;
}
return 2;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
readln.strip.solve.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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto s = RD!int;
auto cnt = n / 2 + 1;
ans[ti] = s / cnt;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k1 = RD!int;
auto k2 = RD!int;
auto w = RD!int;
auto b = RD!int;
auto x = min(k1, k2);
auto y = abs(k1 - k2);
auto z = n - max(k1, k2);
w -= x + y / 2;
b -= z + y / 2;
ans[ti] = w <= 0 && b <= 0;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
} | D |
import std.stdio, std.conv, std.string, std.algorithm;
void main(){
auto a = readln.split.map!(to!int);
writeln(a[0]*a[1], " ", (a[0]+a[1])*2);
} | D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1],C=Q[2],D=Q[3];
if(A*B>C*D)writeln(A*B);
else writeln(C*D);
} | D |
import std.stdio;
import std.math;
import std.conv;
import std.string;
int solve(int x) {
return pow(x,3);
}
void main() {
writeln(solve(to!int(chomp(readln))));
} | D |
import std.stdio,
std.string,
std.range,
std.algorithm,
std.conv;
void main(){
readln;
auto S = readln.chomp.split.map!(to!int).array;
readln;
auto T = readln.chomp.split.map!(to!int).array;
int c = 0;
foreach(i; T){
if(!S.find(i).empty)
c++;
}
c.writeln();
// filter!("a")(chain(S,T)).writeln();
} | D |
import std.stdio, std.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable i = readln.split.to!(ulong[]);
immutable a = i[0], b = i[1];
auto ans = a >= 13 ? b : (a >= 6 ? b/2 : 0);
writeln(ans);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
auto cs = new long[](N+1);
foreach (long i; 0..N+1) {
cs[i] = i;
if (i) cs[i] += cs[i-1];
}
long r;
foreach (k; K..N+1) {
auto d = (cs[N] - cs[N-k] - cs[k-1] + 1) % P;
r += d %= P;
}
writeln((r+1)%P);
} | D |
void main()
{
long a = readln.chomp.to!long - 1;
long b = readln.chomp.to!long - 1;
bool[3] ok;
ok[] = true;
ok[a] = ok[b] = false;
foreach (i, x; ok)
{
if (x) writeln(i + 1);
}
}
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 core.stdc.stdio;
import std.algorithm;
void main(){
int n;
scanf("%d",&n);
int[] a = new int[n];
foreach(ref v;a)
scanf("%d",&v);
long[][] dp = new long[][](n,n);
int l = n%2;
if(l)
foreach(i;0..n)
dp[0][i]=a[i];
foreach(s;1..n){
l^=1;
foreach(i;0..n)
if(l)
dp[s][i]=max(dp[s-1][i]+a[(i+s)%n],dp[s-1][(i+1)%n]+a[i]);
else
dp[s][i]=a[(i+s)%n]>a[i]?dp[s-1][i]:dp[s-1][(i+1)%n];
}
long ans;
foreach(d;dp[n-1])
ans=max(d,ans);
printf("%lld\n",ans);
} | D |
class UF{
int[] par;
this(int n){
par.length=n;
foreach(i; 0..n) par[i]=i;
}
int find(int x){
if(x==par[x]) return par[x];
else return par[x]=find(par[x]);
}
void unite(int x, int y){
if((x=find(x))!=(y=find(y))) par[x]=y;
}
bool same(int x, int y){
return find(x)==find(y);
}
}
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.typecons;
int n, m; rd(n, m);
alias Edge=Tuple!(int, int);
auto edge=new Edge[](0);
foreach(_; 0..m){
int a, b; rd(a, b);
edge~=Edge(a-1, b-1);
}
int cnt=0;
foreach(i; 0..m){
auto uf=new UF(n);
foreach(j; 0..m){
if(i==j) continue;
uf.unite(edge[j][0], edge[j][1]);
}
if(!uf.same(edge[i][0], edge[i][1])) cnt++;
}
writeln(cnt);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
} | D |
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 = 10^^9 + 7;
void main() {
int n, w;
scan(n, w);
auto dp = new long[](w + 1);
foreach (i ; 0 .. n) {
int wi, vi;
scan(wi, vi);
foreach_reverse (j ; 0 .. w - wi + 1) {
dp[j + wi] = max(dp[j + wi], dp[j] + vi);
}
}
writeln(dp[w]);
}
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, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int h, w, k;
scan(h, w, k);
auto c = new int[][](w, w);
foreach (s ; 0 .. (1 << (w - 1))) {
if (s & (s >> 1)) continue;
auto p = iota(0, w).array;
foreach (i ; 0 .. w - 1) {
if (s & (1 << i)) {
swap(p[i], p[i + 1]);
}
}
foreach (i ; 0 .. w) {
c[i][p[i]]++;
}
}
debug {
writefln("%(%s\n%)", c);
}
auto dp = new long[][](h + 1, w);
dp[0][0] = 1;
foreach (i ; 1 .. h + 1) {
foreach (j ; 0 .. w) {
foreach (s ; 0 .. w) {
dp[i][j] += dp[i - 1][s] * c[s][j] % mod;
dp[i][j] %= mod;
}
}
}
debug {
writefln("%(%s\n%)", dp);
}
auto ans = dp[h][k - 1];
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.string, std.algorithm, std.range, std.conv, std.typecons, std.math;
void main(){
auto x = readln.chomp.to!ulong;
auto n = x / 11 * 2;
auto m = x % 11;
if (1 <= m && m <= 6) {
n++;
} else if (7 <= m) {
n += 2;
}
writeln(n);
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.15f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
int binarySearch(alias pred, T)(in T[] arr, bool isLower = true)
{
int ok, ng;
if (isLower) { ok = cast(int)arr.length; ng = -1; }
else { ok = -1; ng = cast(int)arr.length; }
while (abs(ok-ng) > 1)
{
auto mid = (ok+ng) / 2;
if (unaryFun!pred(arr[mid]))
ok = mid;
else ng = mid;
}
return ok;
}
void main()
{
auto N = RD;
auto S = RD!string;
auto pos = new long[][](26);
foreach (i; 0..N)
{
auto num = S[i] - 'a';
pos[num] ~= i;
}
long ans;
foreach (i; 0..N-1)
{
auto num = S[i] - 'a';
auto pi = pos[num].binarySearch!((long a) => a > i)();
foreach (j; pos[num][pi..$])
{
auto len1 = j - i;
auto len2 = N - j;
auto len = min(len1, len2);
if (len <= ans) continue;
foreach (k; 0..len)
{
if (S[i+k] != S[j+k]) break;
ans = max(ans, k+1);
}
}
}
writeln(ans);
stdout.flush();
debug readln();
} | D |
import std.array, std.stdio, std.conv, std.string, std.math, std.random, std.range,std.functional, std.typecons;
import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.comparison;
void main()
{
int Q = readln().strip.to!int;
foreach(i;0..Q)
{
auto aList = readln().strip;
auto bList = readln().strip;
writeln(lcs(aList, bList));
}
}
int lcs(string aList, string bList)
{
int[][] memo;
memo.length = aList.length+1;
memo[0].length = bList.length+1;
foreach(i; 1..aList.length+1)
{
memo[i].length = bList.length+1;
char a = aList[i-1];
foreach(j; 1..bList.length+1)
{
char b = bList[j-1];
if(a==b)
{
memo[i][j] = memo[i-1][j-1]+1;
}else{
memo[i][j] = max(
memo[i-1][j],
memo[i][j-1],
);
}
}
}
return memo[aList.length][bList.length];
}
| 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 R = scanElem;
auto G = scanElem;
writeln(G*2-R);
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n = lread();
string s = sread();
long k = lread();
char c = s[k - 1];
foreach(i;s)
{
if(i == c)
{
write(i);
}
else
{
write("*");
}
}
writeln();
} | D |
import std.stdio, std.array, std.conv, std.string, std.algorithm, std.math;
void main() {
while (true) {
auto input = readln.chomp.split;
int a = input[0].to!int;
int b = input[2].to!int;
char op = input[1].to!char;
if (op == '?') { break; }
switch (op) {
case '+':
writeln(a + b);
break;
case '-':
writeln(a - b);
break;
case '*':
writeln(a * b);
break;
case '/':
writeln(a / b);
break;
default:
break;
}
}
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int x,a,b;
scan(x);
scan(a);
scan(b);
auto ans = a + (x - a) / b * b;
ans = x - ans;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
| D |
void main() {
int n = readln.chomp.to!int;
int k = readln.chomp.to!int;
int x = readln.chomp.to!int;
int y = readln.chomp.to!int;
writeln(n > k ? x * k + (n - k) * y : x * n);
}
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 solve(){
}
void main(){
int n = inelm();
int a = inelm();
(n*n-a).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
alias instr = () => readln().chomp();
T inelm(T= int)(){ return to!(T)( readln().chomp() ); }
T[] inary(T = int)(){ return readln().chomp().split().to!(T[])(); }
| 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 ceildiv (in int x, in int y) {
return (x + y - 1) / y;
}
bool test (const int n, const int d) {
if (n >= d) return true;
for (int x = 1; x < 200_000 ; ++x) {
int y = x + ceildiv (d, x + 1);
if (y <= n) return true;
}
return false;
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
int n = r.next!int;
int d = r.next!int;
writeln (test (n, d) ? "YES" : "NO");
}
}
| D |
import std.stdio,std.string,std.conv,std.array,std.algorithm;
void main(){
for(;;){
auto rcs = readln().chomp().split();
auto scorelist = [to!int(rcs[0]),to!int(rcs[1]),to!int(rcs[2])];
if( scorelist[0]==-1 && scorelist[1]==-1 && scorelist[2]==-1 ){
break;
}
if( scorelist[0]==-1 || scorelist[1]==-1 ){
writeln("F");
} else if( 80 <= scorelist[0]+scorelist[1] ){
writeln("A");
} else if ( 65 <= scorelist[0]+scorelist[1] ) {
writeln("B");
} else if ( 50 <= scorelist[0]+scorelist[1] ) {
writeln("C");
} else if ( 30 <= scorelist[0]+scorelist[1] ) {
if( 50 <= scorelist[2] ){
writeln("C");
} else{
writeln("D");
}
} else {
writeln("F");
}
}
} | D |
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
void main() {
auto data = readln.split;
auto N = data[0].to!long, X = data[1].to!long;
long height(long level) { return 2^^(level+2) - 3; }
long p_num(long level) { return 2^^(level+1) - 1; }
long solve(long level, long x) {
if (level == 0 || x == 0) return x;
auto h = height(level-1);
if (x <= h+1) return solve(level-1, x-1);
else if (x <= 2*h+2) return p_num(level-1) + 1 + solve(level-1, x-h-2);
else return p_num(level);
}
writeln(solve(N, X));
}
| D |
import std.stdio;
import std.ascii;
import std.algorithm;
import core.stdc.stdio;
int main()
{
int t = readInt!int;
foreach(ti; 0 .. t)
{
string str = readString;
auto a = new int[](str.length);
foreach(i, ref ai; a)
switch(str[i])
{
case '0': ai = 0 ^ (i & 1); continue;
case '1': ai = 1 ^ (i & 1); continue;
default: ai = -1; continue;
}
debug writeln(a);
int c0 = 0;
int c1 = 0;
long result = 0;
foreach(i, ai; a)
{
if (ai == 0) c0++, c1 = 0;
if (ai == 1) c1++, c0 = 0;
if (ai == -1) c0++, c1++;
if (ai == 0) result += c0;
if (ai == 1) result += c1;
if (ai == -1) result += max(c0, c1);
}
result.writeln;
}
return 0;
}
/* INPUT ROUTINES */
int currChar;
static this()
{
currChar = getchar();
}
char topChar()
{
return cast(char) currChar;
}
void popChar()
{
currChar = getchar();
}
auto readInt(T)()
{
T num = 0;
int sgn = 0;
while (topChar.isWhite) popChar;
while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
if (sgn) return -num; return num;
}
string readString()
{
string res = "";
while (topChar.isWhite) popChar;
while (!topChar.isWhite) { res ~= topChar; popChar; }
return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
long rep;
this(long num)
{
rep = num;
}
Z!m opBinary(string operator)(Z!m rhs)
{
static if (operator == "+")
{
long result = rhs.rep + this.rep;
if (result >= m) result -= m;
return Z!m(result);
}
else static if (operator == "-")
{
long result = this.rep - rhs.rep;
if (result < 0) result += m;
return Z!m(result);
}
else static if (operator == "*")
{
long result = this.rep * rhs.rep;
if (result >= m) result %= m;
return Z!m(result);
} else static assert(text("Operator ", operator, " not supported"));
}
Z!m opBinary(string operator)(long exponent) if (operator == "^^")
{
assert(exponent >= 0);
Z!m base = this;
Z!m result = 1;
while (exponent)
{
if (exponent & 1)
result = result * base;
base = base * base;
exponent >>= 1;
}
return result;
}
invariant
{
assert(rep >= 0 && rep < m);
}
}
| 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;
// dfmt on
void main()
{
long H, W;
scan(H, W);
long cnt;
foreach (i; 0 .. H)
{
auto A = sread();
foreach (c; A)
{
cnt += c == '#';
}
}
writeln((cnt == H + W - 1) ? "Possible" : "Impossible");
}
| D |
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto data = readln.split;
auto A = data[0].to!long, B = data[1].to!long;
long ans;
foreach (i; 1 .. 41) {
immutable power = 2L^^i;
if (B < power) break;
auto A_i = A - (A/power) * power, B_i = B - (B/power) * power;
auto a = (A & power) != 0 ? 1 : 0, b = (B & power) != 0 ? 1 : 0;
//writeln(A_i, B_i);
if (A - A_i + power <= B - B_i) {
//writeln("here1 ", ans);
( (power-A_i)*a + (B_i+1)*b ) % 2 == 0 ? 0 : (ans += power);
//writeln(ans);
}
else {
//writeln("here2 ", ans);
if ((A & power) != 0) (B-A+1) % 2 == 0 ? 0 : (ans += power);
//writeln(ans);
}
}
B = B - A/2 * 2, A = A - A/2 * 2;
//writeln(A, ", , ", B);
if (A == 1) {
if (B%2 == 0) {
if (B/2 % 2 == 1) ans++;
}
else if ((B-1)%4 != 0) {}
else { ans++; }
}
else if (A == 0) {
if (B % 2 == 1 ) {
if ((B-1)%4 == 0) ans++;
}
else if (B%4 == 0) {}
else { ans++; }
}
writeln(ans);
}
| D |
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
import std.typecons;
void main() {
auto a = read.to!int;
auto b = read.to!int;
if (a + b == 15) writeln("+");
else if (a * b == 15) writeln("*");
else writeln("x");
}
string read() {
static string[] ss;
while (!ss.length) ss = readln.chomp.split;
auto res = ss[0];
ss.popFront;
return res;
} | D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
int ans = a * b - a - b + 1;
writeln(ans);
}
| D |
// Vicfred
// https://atcoder.jp/contests/abc160/tasks/abc160_b
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
long ans = (n/500)*1000;
n -= (n/500)*500;
ans += (n/5)*5;
ans.writeln;
}
| D |
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto a=scanElem;
auto o=scanString;
auto b=scanElem;
if(o=="+")end(a+b);end(a-b);
}
| D |
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
readln();
string s = sread();
long[] l = s.map!(c => c == 'I' ? 1 : -1)
.array
.to!(long[]);
l = 0 ~ l;
long m;
foreach (i; 1 .. l.length)
{
l[i] += l[i - 1];
m = max(m, l[i]);
}
writeln(m);
}
| D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!double;
writeln(sqrt(N).to!int^^2);
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new char[][](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
foreach (i; 0..k)
ans[ti] ~= 'a';
while (ans[ti].length < n)
{
ans[ti] ~= "cba";
}
ans[ti].length = n;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
} | D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 long[](T);
foreach (i; 0..T)
{
auto s = RD;
auto t = RD;
auto e = RD;
auto a = (s + t + e) / 2 + 1;
a = max(a, s);
ans[i] = (s+e) < a ? 0 : s + e - a + 1;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
} | 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;
if(n == 1) "-1".writeln;
else{
if(n % 9 == 1){
foreach(i; 0 .. n - 2) "5".write;
"99".writeln;
}
else{
foreach(i; 0 .. n - 1) "5".write;
"9".writeln;
}
}
}
}
| D |
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n, m;
char[] s;
scan(n, m);
scan(s);
foreach (i ; 0 .. m) {
int l, r;
char c1, c2;
scan(l, r, c1, c2);
l--, r--;
foreach (j ; l .. r + 1) {
if (s[j] == c1) s[j] = c2;
}
}
writeln(s);
}
long revnum(long n) {
long res;
while (n > 0) {
res *= 10;
res += (n % 10);
n /= 10;
}
return res;
}
unittest {
assert(revnum(0) == 0);
assert(revnum(8) == 8);
assert(revnum(10) == 1);
assert(revnum(123) == 321);
assert(revnum(10100) == 101);
writeln("ok");
}
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.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto x = readln.strip.to !(int);
int step = 0;
int pos = 0;
while (pos < x)
{
step += 1;
pos += step;
}
writeln (step + (pos - x == 1));
}
}
| D |
void main() {
problem();
}
void problem() {
const N = scan!long;
long solve() {
long ans;
foreach(i; 1..N+1) {
if (i % 3 != 0 && i % 5 != 0) ans += i;
}
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;
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 |
/+ 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() {
Scanner sc = new Scanner(stdin);
int n, m, x;
sc.read(n, m, x);
foreach (i; 0..n+1) {
foreach (j; 0..m+1) {
int k = n-i;
int l = m-j;
if (i*l + k*j == x) {
writeln("Yes");
return 0;
}
}
}
writeln("No");
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.stdio,
std.conv,
std.algorithm,
std.range,
std.string,
std.numeric;
void main() {
size_t a,b;
auto n = readln.chomp.to!size_t - 1;
auto ary = readln.chomp.split.to!(size_t[]);
a = ary[0];
b = ary[1];
foreach(cnt;0..n){
auto tmp = readln.chomp.split.to!(size_t[]);
auto inp_x = tmp[0];
auto inp_y = tmp[1];
immutable ratio = max((a-1)/inp_x+1,(b-1)/inp_y+1);
a = inp_x * ratio;
b = inp_y * ratio;
}
writeln(a+b);
}
| 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(){
string s = readln.chomp;
int ans;
char x = 'a';
foreach(c; s){
if(x != 'a') if(x != c) ans += 1;
x = c;
}
ans.writeln;
} | D |
import std.stdio, std.string, std.conv, std.array, std.algorithm;
void main() {
int count;
while (true) {
auto x = readln.chomp;
if (x == "0") break;
writeln("Case " ~ to!string(++count) ~ ": " ~ x);
}
} | D |
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
auto s = readln.chomp.to!(char[]);
auto n = s.length;
auto a = new long[](n + 1), c = new long[](n + 1), q = new long[](n + 1);
foreach (i; 0 .. n) {
a[i + 1] += a[i];
c[i + 1] += c[i];
q[i + 1] += q[i];
if (s[i] == 'A')
a[i + 1]++;
if (s[i] == 'C')
c[i + 1]++;
if (s[i] == '?')
q[i + 1]++;
}
long mod = 10 ^^ 9 + 7;
long powmod(long b, long ex) {
if (ex == 0)
return 1L;
if (ex & 1)
return b * powmod(b, ex - 1) % mod;
else
return powmod(b * b % mod, ex / 2);
}
// (A, C), (?, C), (C, ?), (?, ?)
long tot = 0;
foreach (i; 0 .. n) {
if (s[i] == 'B' || s[i] == '?') {
if (i > 0 && i + 1 < n) {
(tot += a[i] * (c[n] - c[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]))) %= mod;
if (q[i] > 0) {
(tot += q[i] * (c[n] - c[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]) - 1)) %= mod;
}
if (q[n] - q[i + 1] > 0) {
(tot += a[i] * (q[n] - q[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]) - 1)) %= mod;
}
if (q[i] > 0 && (q[n] - q[i + 1]) > 0) {
(tot += q[i] * (q[n] - q[i + 1]) % mod * powmod(3, q[i] + (q[n] - q[i + 1]) - 2)) %= mod;
}
}
}
}
writeln(tot);
}
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;
import std.array;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void readInto(T...)(ref T ts) {
auto ss = readln.split;
foreach(ref t; ts) {
t = ss.front.to!(typeof(t));
ss.popFront;
}
}
// 冪乗をmod取りつつ計算
@nogc @safe pure ulong modPow(ulong a, ulong n, ulong m) {
ulong r = 1;
while (n > 0) {
if(n % 2 != 0) r = r * a % m;
a = a * a % m;
n /= 2;
}
return r;
}
// フェルマーの小定理から乗法逆元を計算
// 定理の要請により法は素数
@nogc @safe pure ulong modInv(ulong a, ulong m) {
return modPow(a, m-2, m);
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modPerm(ulong n, ulong k, ulong m) {
if (n < k) return 0;
ulong r = 1;
for (ulong i = n-k+1; i <= n; i++) {
r *= i;
r %= m;
}
return r;
}
// mod取りつつ順列を計算
@nogc @safe pure ulong modFact(ulong n, ulong m) {
return modPerm(n, n, m);
}
// mod取りつつ組み合わせを計算
// modInvを使っているので法は素数
@nogc @safe pure ulong modComb(ulong n, ulong r, ulong m) {
return modPerm(n, r, m)*modInv(modFact(r, m), m) % m;
}
immutable ulong MOD = 1000000007;
void main() {
ulong r;
readInto(r);
writeln(3*r*r);
}
| D |
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
auto B = aryread();
if (A == B)
{
writeln("Yes");
return;
}
long s;
foreach (i; 0 .. N)
s += max(0, A[i] - B[i]);
long t;
foreach (i; 0 .. N)
{
long x = max(0, B[i] - A[i]);
t += x / 2;
}
writeln(s <= t ? "Yes" : "No");
}
| D |
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int x = read.to!int;
int a = read.to!int;
int ans;
if(x < a) ans = 0;
else ans = 10;
ans.writeln;
}
| D |
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
writeln((S[0] == S[1] && S[1] == S[2]) ? "No" : "Yes");
}
| 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 readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n; readV(n);
string[] w; readC(n, w);
auto h = [w[0]: true];
foreach (i; 1..n) {
if (w[i][0] != w[i-1][$-1] || w[i] in h) {
writeln("No");
return;
}
h[w[i]] = true;
}
writeln("Yes");
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;
void main() {
readln.chomp.count!"a == '1'".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.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto t = RD!int;
auto ans = new int[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
auto s = RD!string;
int len = n / k;
int l, r = k-1;
while (l <= r)
{
auto cnt = new int[](26);
foreach (i; 0..len)
{
auto c1 = s[i*k+l] - 'a';
auto c2 = s[i*k+r] - 'a';
++cnt[c1];
++cnt[c2];
}
int x;
foreach (i; 0..26)
{
x.chmax(cnt[i]);
}
if (l == r)
ans[ti] += len - x / 2;
else
ans[ti] += len * 2 - x;
debug writeln("ans:", ans[ti]);
++l;
--r;
}
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
| D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a, p;
scan(a, p);
auto ans = (3*a + p) / 2;
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
| D |
import std.stdio, std.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 ans1 = A + B;
auto ans2 = A - B;
auto ans3 = A * B;
writeln(max(ans1, max(ans2, ans3)));
stdout.flush();
debug readln();
}
| D |
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto M = RD;
writeln(N*(N-1)/2 + M*(M-1)/2);
stdout.flush;
debug readln;
} | D |
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
int d;
scan(d);
write("Christmas ");
foreach (i ; 0 .. 25 - d) {
write("Eve ");
}
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
string s = readln.strip;
foreach (i; 0 .. 26) {
auto c = (i + 97).to!(char);
if (s.find (c).empty) {
writeln (s, c);
return;
}
}
int l = s.length.to!(int);
auto a = s.map! (c => c.to!(int) - 97).array;
string res = "~";
foreach (i; 0 .. l) {
foreach (j; 0 .. 26) {
if (a[0 .. i].find (j).empty && j > a[i]) {
auto t = s[0 .. i] ~ (j + 97).to!(char);
if (res > t) {
res = t;
}
}
}
}
if (res == "~") {
res = "-1";
}
writeln (res);
}
| 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 N = lread();
writeln((N + 1) / 2);
}
| 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 minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
auto S = sread();
long m = long.max;
auto sum = new long[][](2, N + 1);
foreach (i; 0 .. N)
sum[0][i + 1] = sum[0][i] + (0 ^^ (S[i] != 'W'));
foreach (i; 0 .. N)
sum[1][i + 1] = sum[1][i] + (0 ^^ (S[i] != 'E'));
iota(N).map!(x => (sum[0][x] - sum[0][0]) + (sum[1][N] - sum[1][x + 1]))
.reduce!min().writeln();
}
| D |
void main()
{
long n = readln.chomp.to!long;
string digits = "0357";
long cnt;
foreach (i; 0 .. 1000000)
{
long x = i;
long len;
string nums;
while (x)
{
++len;
nums ~= digits[x%4];
x /= 4;
}
long num;
foreach_reverse (j, c; nums)
{
num += (c - '0') * 10 ^^ j;
}
if (num > n) break;
if (!nums.canFind('0')
&& nums.canFind('3')
&& nums.canFind('5')
&& nums.canFind('7'))
++cnt;
}
cnt.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(){
string s = readln().chomp();
string outs;
foreach(i; 0..s.length){
outs ~= "x";
}
outs.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
| D |
import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
import std.functional;
void main()
{
auto n = readln.chomp.to!int;
long answer;
foreach (a; 1..n+1)
foreach (b; 1..n+1)
{
immutable g = gcd(a, b);
foreach (c; 1..n+1)
answer += gcd(g, c);
}
answer.writeln;
}
alias gcd = memoize!_gcd;
long _gcd(in long a, in long b)
{
if (a == 0) return b;
if (b == 0) return a;
if (a & b & 1) return gcd(max(a, b) - min(a, b), min(a, b));
if (!((a | b) & 1)) return gcd(a >> 1, b >> 1) << 1;
if (a & 1) return gcd(a, b >> 1);
if (b & 1) return gcd(a >> 1, b);
assert (false);
}
unittest
{
assert (gcd(0, 0) == 0);
assert (gcd(0, 1) == 1);
assert (gcd(0, 2) == 2);
assert (gcd(1, 0) == 1);
assert (gcd(1, 1) == 1);
assert (gcd(1, 2) == 1);
assert (gcd(2, 0) == 2);
assert (gcd(2, 1) == 1);
assert (gcd(2, 2) == 2);
assert (gcd(20, 30) == 10);
assert (gcd(16, 24) == 8);
} | D |
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;
import core.bitop;
import std.digest.sha;
void main()
{
int n = readln.chomp.to!int;
bool[] done = new bool[](n + 1);
done[0] = true;
foreach (d; [4, 7]) {
foreach (i; 0..n - d + 1) {
done[i + d] |= done[i];
}
}
writeln = done[n] ? "Yes" : "No";
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.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;
}
}
bool test (in ulong[] a, uint k) {
debug stderr.writeln (a);
bool[int] m;
foreach (x; a) {
ulong y = x;
int o;
debug stderr.writeln ("x = ", x);
while (y > 0) {
uint t = (y % k).to!uint;
debug stderr.writeln (y, ' ', t);
if (t > 1) return false;
if (t == 1) {
if (o in m) return false;
m[o] = true;
}
y /= k;
++o;
}
}
return true;
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
const n = r.next!uint;
const k = r.next!uint;
auto a = r.nextA!ulong (n);
writeln (test (a, k) ? "YES" : "NO");
}
}
| D |
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// ModInt {{{
struct ModInt(ulong modulus) {
import std.traits : isIntegral, isBoolean;
import std.exception : enforce;
private {
ulong val;
}
this(const ulong n) {
val = n % modulus;
}
this(const ModInt n) {
val = n.value;
}
@property {
ref inout(ulong) value() inout {
return val;
}
}
T opCast(T)() {
static if (isIntegral!T) {
return cast(T)(val);
} else if (isBoolean!T) {
return val != 0;
} else {
enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ ".");
}
}
ModInt opAssign(const ulong n) {
val = n % modulus;
return this;
}
ModInt opOpAssign(string op)(ModInt rhs) {
static if (op == "+") {
val += rhs.value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs.value) {
val += modulus;
}
val -= rhs.value;
} else if (op == "*") {
val = val * rhs.value % modulus;
} else if (op == "/") {
this *= rhs.inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs.value > 0) {
if (rhs.value % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opOpAssign(string op)(ulong rhs) {
static if (op == "+") {
val += rhs;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs) {
val += modulus;
}
val -= rhs;
} else if (op == "*") {
val = val * rhs % modulus;
} else if (op == "/") {
this *= ModInt(rhs).inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs > 0) {
if (rhs % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opUnary(string op)() {
static if (op == "++") {
this += 1;
} else if (op == "--") {
this -= 1;
} else {
enforce(false, op ~ " is not implemented.");
}
return this;
}
ModInt opBinary(string op)(const ulong rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(ref const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinaryRight(string op)(const ulong lhs) const {
mixin("return ModInt(this) " ~ op ~ "= lhs;");
}
long opCmp(ref const ModInt rhs) const {
return cast(long)value - cast(long)rhs.value;
}
bool opEquals(const ulong rhs) const {
return value == ModInt(rhs).value;
}
ModInt inv() const {
ModInt ret = this;
ret ^^= modulus - 2;
return ret;
}
string toString() const {
import std.format : format;
return format("%s", val);
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int h, w;
cin.scan(h, w);
string[] s = cin.nextArray!string(h);
bool[] rmh = new bool[h], rmw = new bool[w];
foreach (i; 0 .. h) {
bool f = true;
foreach (j; 0 .. w) {
if (s[i][j] == '#') {
f = false;
break;
}
}
rmh[i] = f;
}
foreach (i; 0 .. w) {
bool f = true;
foreach (j; 0 .. h) {
if (s[j][i] == '#') {
f = false;
break;
}
}
rmw[i] = f;
}
foreach (i; 0 .. h) {
if (rmh[i]) continue;
foreach (j; 0 .. w) {
if (rmw[j]) continue;
write(s[i][j]);
}
writeln;
}
}
| D |
import std.stdio, std.conv, std.string, std.math;
void main(){
auto ip = readln.split.to!(string[]);
auto A = ip[0];
auto B = ip[1];
auto C = ip[2];
if(A[$-1] == B[0] && B[$-1] == C[0]) "YES".writeln;
else "NO".writeln;
} | D |
void main()
{
long a, b, c, d;
rdVals(a, b, c, d);
foreach (i; 0 .. 1000)
{
if (i & 1)
{
a -= d;
if (a <= 0)
{
"No".writeln;
return;
}
}
else
{
c -= b;
if (c <= 0)
{
"Yes".writeln;
return;
}
}
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop; | D |
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
auto ret = 1;
foreach (_; 0..N)
ret = min(ret * 2, ret + K);
writeln(ret);
} | D |
/+ dub.sdl:
name "E"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n;
sc.read(n);
int[] wpos = new int[n], bpos = new int[n];
foreach (i; 0..2*n) {
char c; int a;
sc.read(c, a); a--;
if (c == 'W') {
wpos[a] = i;
} else {
bpos[a] = i;
}
}
int base = 0;
foreach (i; 0..n) {
foreach (j; i+1..n) {
if (wpos[i] > wpos[j]) base++;
if (bpos[i] > bpos[j]) base++;
}
}
debug writeln("BASE ", base);
int[][] wdp = new int[][](n, n+1), bdp = new int[][](n, n+1);
foreach (i; 0..n) {
foreach (j; 1..n+1) {
wdp[i][j] = wdp[i][j-1];
if (wpos[i] < bpos[j-1]) wdp[i][j]++;
bdp[i][j] = bdp[i][j-1];
if (bpos[i] < wpos[j-1]) bdp[i][j]++;
}
}
debug writeln(wdp);
debug writeln(bdp);
int[][] dp = new int[][](2*n+1, n+1);
foreach (i; 1..2*n+1) {
foreach (w; max(0, i-n)..min(n+1, i+1)) {
int b = i-w;
dp[i][w] = 10^^9;
if (w) {
dp[i][w] = min(dp[i][w], dp[i-1][w-1] + wdp[w-1][b]);
}
if (b) {
dp[i][w] = min(dp[i][w], dp[i-1][w] + bdp[b-1][w]);
}
}
}
debug {
foreach (v; dp) {
writeln(v);
}
}
writeln(base + dp[2*n][n]);
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
| D |
import std.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);
long k; readV(k);
auto i = s.countUntil!(si => si != '1');
if (i == -1 || i >= k)
writeln(1);
else
writeln(s[i]);
}
| D |
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
long ask (int lo, int hi)
{
writeln ("? ", lo, " ", hi);
stdout.flush ();
return readln.strip.to !(long);
}
void solve (int n)
{
auto total = ask (1, n);
int lo = 1;
int hi = n;
while (lo < hi)
{
int me = (lo + hi) / 2;
auto cur = ask (1, me);
if (cur < total)
{
lo = me + 1;
}
else
{
hi = me;
}
}
int k = hi;
auto all1 = ask (1, k);
auto cut1 = ask (1, k - 1);
auto delta1 = cast (int) (all1 - cut1);
int j = k - delta1;
auto all2 = ask (1, j - 1);
auto cut2 = ask (1, j - 2);
auto delta2 = cast (int) (all2 - cut2);
int i = j - 1 - delta2;
writeln ("! ", i, " ", j, " ", k);
stdout.flush ();
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
solve (n);
}
}
| D |
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int read() { return readln.chomp.to!int; }
int[] reads() { return readln.split.to!(int[]); }
void solve() {
auto a = reads();
writeln(abs(a[0] - a[1]) > 1 ? ":(" : "Yay!");
}
void main() {
solve();
readln;
} | D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
enum mod = 1_000_000_007L;
struct Mint {
long x;
this(long a) {
x = a % mod;
if (x < 0) x += mod;
}
this(Mint a) {
x = a.x;
}
ref Mint opAssign(long a) {
this = Mint(a);
return this;
}
ref Mint opOpAssign(string op)(Mint rhs) {
static if (op == "+") {
x += rhs.x;
if (x >= mod) x -= mod;
}
static if (op == "-") {
x -= rhs.x;
if (x < 0) x += mod;
}
static if (op == "*") {
(x *= rhs.x) %= mod;
}
static if (op == "/") {
this *= rhs.inv();
}
return this;
}
ref Mint opOpAssign(string op)(long rhs) {
static if (op == "^^") {
this = powmod(this, rhs);
return this;
}
else {
return mixin("this " ~ op ~ "= Mint(rhs)");
}
}
const Mint powmod(Mint a, long b) {
Mint res = 1, p = a;
while (b > 0) {
if (b & 1) res *= p;
p *= p;
b /= 2;
}
return res;
}
const Mint inv() {
return powmod(this, mod - 2);
}
Mint opUnary(string op)() if (s == "-") {
return Mint(-x);
}
Mint opBinary(string op, T)(const T rhs) {
return mixin("Mint(this) " ~ op ~ "= rhs");
}
Mint opBinaryRight(string op)(const long rhs) {
return mixin("Mint(rhs) " ~ op ~ "= this");
}
bool opEquals(Mint a, Mint b) {
return a.x == b.x;
}
bool opEquals(long rhs) {
long y = rhs % mod;
if (y < 0) y += mod;
return x == y;
}
string toString() {
import std.conv : to;
return x.to!string;
}
}
unittest {
long powmod(long a, long b) {
return b > 0 ? powmod(a, b / 2)^^2 % mod * a^^(b & 1) % mod : 1L;
}
auto a = Mint(2), b = Mint(3);
assert(a + b == 5);
assert(a + 5 == 7);
assert(1 + b == 4);
assert(a - b == mod - 1);
assert(a * b == 6);
assert(a / b == 2 * powmod(3, mod - 2));
assert(a^^10 == 1024);
Mint z;
assert(z == 0);
(z += 2) *= 3;
assert(z == 6);
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
string s;
scan(s);
auto p = ["dream", "dreamer", "erase", "eraser"];
while (!s.empty) {
bool e;
foreach (str ; p) {
if (str.length > s.length) continue;
if (str == s[$ - str.length .. $]) {
s = s[0 .. $ - str.length];
e = true;
break;
}
}
if (!e) {
writeln("NO");
return;
}
}
writeln("YES");
}
| D |
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
string s = read!string;
writeln(s.count!(c => c == 'x') <= 7 ? "YES" : "NO");
}
| D |
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.math;
void main(string[] args) {
auto input = readln().chomp.split.map!(to!int).array;
if(input[1] == 100){input[1]++;}
auto ans = input[1] * pow(100,input[0]);
ans.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, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
alias Pair = Tuple!(int, "a", int, "b");
void main() {
int N;
scan(N);
if (N == 1) {
writeln("Hello World");
}
else {
int a, b;
scan(a);
scan(b);
writeln(a + b);
}
}
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 |
module Main;
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.math;
import std.algorithm;
void main() {
string input;
immutable limitList = 50022;
bool[] listPrimeNumber = new bool[](limitList);
int number, primeNumberSmaller, primeNumberBigger;
listPrimeNumber.fill(true);
foreach (i; 2..limitList.to!double.sqrt.to!int) {
if (listPrimeNumber[i]) {
for (int j = i*2; j < limitList; j += i) listPrimeNumber[j] = false;
}
}
while ((input = readln.chomp).length != 0) {
number = input.to!int;
primeNumberSmaller = getPrimeNumber(listPrimeNumber, number, false);
primeNumberBigger = getPrimeNumber(listPrimeNumber, number, true);
writeln(primeNumberSmaller, " ", primeNumberBigger);
}
}
int getPrimeNumber(in bool[] list, in int input, in bool flagBigger) {
int temp;
if (flagBigger) {
temp = input + 1;
} else {
temp = input - 1;
}
if (list[temp]) {
return temp;
} else {
return getPrimeNumber(list, temp, flagBigger);
}
} | 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
7
10
---+/
/+---test
14
12
112
---+/
void main(string[] args) {
const H = readln.chomp.to!long;
const W = readln.chomp.to!long;
const N = readln.chomp.to!long;
ceil(N*1. / max(H, W)).to!int.writeln;
}
| D |
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
void main() {
int ans;
foreach (dchar[] line; stdin.lines) {
line = line.chomp;
if (line == line.retro.array) ans++;
}
ans.writeln;
} | D |
import std.stdio, std.conv, std.array,std.string,std.algorithm;
void main()
{
auto input1=readln.chomp;
writeln(input1[0],input1.length-2,input1[input1.length-1]);
} | D |
import std.stdio,std.algorithm;
long n,no,ne,mino,so,se,temp;
void main()
{
scanf(" %d",&n);
mino=long.max;
for(int i=0;i<n;++i)
{
scanf(" %d",&temp);
if(temp%2)
{
mino=min(mino,temp);
so+=temp;
++no;
}else{
se+=temp;
++ne;
}
}
writeln(se+so-(no%2?mino:0));
}
| D |
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
long x;
scan(x);
long t;
while (t * (t + 1) / 2 < x) t++;
writeln(t);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
| D |
void main(){
int m = _scan();
writeln(48-m);
}
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.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
int[] primes = [2,3,5];
for(int i=7;primes.length <= 10000;i++){
bool flg = true;
for(int j=0;primes[j]*primes[j]<=i;j++){
if(i%primes[j]==0){
flg = false;
break;
}
}
if(flg) primes ~= i;
}
while(true){
int n = to!int(chomp(readln()));
if(n == 0) break;
int sum = 0;
for(int i=0;i<n;i++)
sum += primes[i];
writeln(sum);
}
} | D |
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
string str = readln.chomp;
writeln = str.indexOf("9") == -1 ? "No" : "Yes";
}
| D |
void main() {
problem();
}
void problem() {
auto S = scan;
auto N = S.length;
bool solve() {
auto head = S[0..(N-1)/2];
auto tail = S[(N+3)/2-1..$];
S.deb;
head.deb;
tail.deb;
auto s = S;
for(long i = 0; i < s.length/2; i++) {
if (s[i] != s[$-i-1]) return false;
}
s = head;
for(long i = 0; i < s.length/2; i++) {
if (s[i] != s[$-i-1]) return false;
}
s = tail;
for(long i = 0; i < s.length/2; i++) {
if (s[i] != s[$-i-1]) return false;
}
return true;
}
writeln(solve() ? "Yes" : "No");
}
// ----------------------------------------------
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); }
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, 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()
{
int a; readV(a);
int b; readV(b);
int h; readV(h);
writeln((a+b)*h/2);
}
| D |
import std.stdio;
import std.conv;
import std.string;
void main()
{
int n = readln.chomp.to!int - 1;
int[] a = readln.split.to!(int[]);
int ans = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == a[i + 1])
{
ans++;
i++;
}
}
writeln(ans);
}
| D |
immutable long mod=998244353;
immutable int M=1_000_00*4;
void main(){
import std.stdio, std.algorithm;
long n, a, b, k; rd(n, a, b, k);
auto fact=genFact(M, mod);
auto invFact=genInv(fact, mod);
long comb(long nn, long rr){
if(nn<rr) return 0;
long ret=fact[nn]%mod;
(ret*=invFact[rr])%=mod;
(ret*=invFact[nn-rr])%=mod;
return ret;
}
long tot=0;
for(long x=0; x<=n; x++)if(k-a*x>=0){
if((k-a*x)%b==0){
long y=(k-a*x)/b;
if(y>n) continue;
(tot+=comb(n, x)*comb(n, y)%mod)%=mod;
}
}
writeln(tot);
}
long[] genFact(int n, long mod){
auto fact=new long[](n);
fact[0]=fact[1]=1;
foreach(i; 2..n) fact[i]=i*fact[i-1]%mod;
return fact;
}
long[] genInv(long[] arr, long mod){
auto inv=new long[](arr.length);
inv[0]=inv[1]=1;
foreach(i, elem; arr[1..$]) inv[i]=powmod(arr[i], mod-2, mod);
return inv;
}
long powmod(long a, long x, long mod){
if(x==0) return 1;
else if(x==1) return a;
else if(x&1) return a*powmod(a, x-1, mod)%mod;
else return powmod(a*a%mod, x/2, mod);
}
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;
enum conv = [
'I':1,
'V':5,
'X':10,
'L':50,
'C':100,
'D':500,
'M':1000
];
void main()
{
foreach(input; stdin.byLine())
{
int arabia = 0;
if(input.length == 1) arabia = conv[input[0]];
else
{
foreach(i; 1 .. input.length)
{
if(conv[input[i - 1]] < conv[input[i]])
{
arabia -= conv[input[i - 1]];
}
else
{
arabia += conv[input[i - 1]];
}
}
arabia += conv[input[$ - 1]];
}
arabia.writeln;
}
} | D |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.