code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
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;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.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 gen(){}
void jury(){}
void solve(){
int n = scan!int;
int ans;
foreach(a; 1 .. n + 1){
ans += (n - 1) / a;
}
ans.print;
}
|
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 S = RD!string;
auto num = [S[$-1]].to!long;
writeln(num % 2);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto t = readln.split.map!(to!int);
auto H = t[0];
auto W = t[1];
auto A = readln.split.map!(to!long).array;
auto B = readln.split.map!(to!long).array;
auto X = readln.chomp.to!long;
auto minA = new long[](H+1);
auto minB = new long[](W+1);
foreach (len; 1..H+1) {
long s = 0;
foreach (i; 0..len) s += A[i];
minA[len] = s;
foreach (i; len..H) {
s = s - A[i-len] + A[i];
minA[len] = min(minA[len], s);
}
}
foreach (len; 1..W+1) {
long s = 0;
foreach (i; 0..len) s += B[i];
minB[len] = s;
foreach (i; len..W) {
s = s - B[i-len] + B[i];
minB[len] = min(minB[len], s);
}
}
long ans = 0;
foreach (i; 0..H+1) foreach (j; 0..W+1) if (minA[i] * minB[j] <= X) ans = max(ans, i * j);
ans.writeln;
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void solve(){
auto n = scan!int;
auto arr = iota(1L, n+1L).array;
long a = scan;
long b = scan;
if(abs(a - b) > 1 || a + b > n - 2){
writeln(-1);
return;
}
long swp = max(a, b);
if(a > b){
arr.reverse;
}
for(int i = 0; i+1 < n; i += 2){
if(!swp){
break;
}
swap(arr[i], arr[i+1]);
--swp;
}
long ac = 0, bc = 0;
for(int i = 1; i < n-1; ++i){
if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
++ac;
}else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
++bc;
}
}
if(ac == a && bc == b){
for(int i = 0; i < n; ++i){
write(arr[i], " ");
}
writeln;
return;
}else if (ac == a || bc == b){
swap(arr[n-1], arr[n-2]);
ac = 0; bc = 0;
for(int i = 1; i < n-1; ++i){
if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
++ac;
}else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
++bc;
}
}
}
if(ac == a && bc == b){
for(int i = 0; i < n; ++i){
write(arr[i], " ");
}
writeln;
}else{
show(arr);
writeln(-1);
}
}
void main(){
long tests = scan; // Toggle!
while(tests--) solve;
}
/************** ***** That's All Folks! ***** **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 long[][](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto used = new bool[](n);
foreach (i; 0..n)
{
auto k = RD;
bool ok;
foreach (j; 0..k)
{
auto e = RD!int-1;
if (ok) continue;
if (!used[e])
{
used[e] = true;
ok = true;
}
}
if (!ok)
{
ans[ti] = [i+1];
}
}
if (!ans[ti].empty)
{
foreach (i; 0..n)
{
if (!used[i])
{
ans[ti] ~= i+1;
break;
}
}
}
}
foreach (e; ans)
{
if (e.empty)
{
writeln("OPTIMAL");
}
else
{
writeln("IMPROVE");
writeln(e[0], " ", e[1]);
}
}
stdout.flush;
debug readln;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.numeric;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
}
void main() {
auto r = new InputReader;
immutable n = r.next!int;
immutable m = r.next!int;
auto x = uninitializedArray!(long[]) (n);
foreach (i; 0 .. n) x[i] = r.next!long;
immutable x0 = x[0];
long g;
foreach (i; 1 .. n) {
immutable dx = x[i] - x0;
if (i == 1) {
g = dx;
} else {
g = gcd (g, dx);
}
}
auto p = uninitializedArray!(long[]) (n);
foreach (i; 0 .. m) {
p[i] = r.next!long;
if (!(g % p[i])) {
writeln ("YES");
writeln (x[0], ' ', i + 1);
return;
}
}
writeln ("NO");
}
|
D
|
import std.stdio;
import std.ascii;
import core.stdc.stdio;
int main()
{
int t = readInt!int;
while (t--)
{
long x = readInt!long;
long m111 = 0;
bool can = false;
int maxrep = 20;
int i = 0;
while (m111 <= x && i < maxrep)
{
if ((x - m111) % 11 == 0)
{
can = true;
break;
}
m111 += 111;
i++;
}
if (can) writeln("YES"); else writeln("NO");
}
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;
}
|
D
|
import std.stdio;
int l,r;
void main()
{
scanf("%d %d", &l, &r);
long low = 1;
int ans = 0;
while(low<=r){
long x=low;
while(x<=r){
if(x>=l){
ans++;
}
x*=2;
}
low*=3;
}
printf("%d",ans);
}
|
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 r = new long[][](n);
foreach (i; 0..n)
{
r[i] = RDA;
}
int x;
foreach (int i; 1..n)
{
long cnt;
foreach (j; 0..5)
{
if (r[x][j] < r[i][j])
++cnt;
}
if (cnt >= 3) continue;
x = i;
}
auto cnt = new long[](n);
foreach (i; 0..n)
{
if (i == x) continue;
foreach (j; 0..5)
{
if (r[x][j] < r[i][j])
++cnt[i];
}
}
ans[ti] = x+1;
foreach (i; 0..n)
{
if (i == x) continue;
if (cnt[i] < 3)
{
ans[ti] = -1;
break;
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
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 a,b,c,d;
scan(a,b,c,d);
foreach (i ; a .. 2*a + 1) {
foreach (j ; b .. min(i, 2*b + 1)) {
foreach (k ; c .. min(j, 2*c + 1)) {
if (d <= k && k <= 2*d && j > 2*d) {
writeln(i);
writeln(j);
writeln(k);
return;
}
}
}
}
writeln(-1);
}
void solve(int a, int L) {
auto as = new int[](21);
as[0] = a;
foreach (i ; 1 .. 21) {
as[i] = calmax(as[i-1], L) - calmin(as[i-1], L);
foreach (j ; 0 .. i) {
if (as[i] == as[j]) {
writeln(j, " ", as[i], " ", i-j);
return;
}
}
}
}
int calmax(int x, int L) {
auto cnt = new int[](10);
while (x > 0) {
cnt[x % 10]++;
x /= 10;
}
cnt[0] += max(0, L - cnt.sum);
int res;
foreach_reverse (i ; 0 .. 10) {
foreach (j ; 0 .. cnt[i]) {
res *= 10;
res += i;
}
}
debug {
writeln(res);
}
return res;
}
int calmin(int x, int L) {
auto cnt = new int[](10);
while (x > 0) {
cnt[x % 10]++;
x /= 10;
}
cnt[0] += max(0, L - cnt.sum);
int res;
foreach (i ; 0 .. 10) {
foreach (j ; 0 .. cnt[i]) {
res *= 10;
res += i;
}
}
debug {
writeln(res);
}
return res;
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s1 = readln.chomp;
auto s2 = readln.chomp;
writeln(levenshteinDistance(s1, s2));
}
|
D
|
void main(){
real num = readln().chomp().replace(" ","").to!int();
writeln(num%sqrt(num)==0?"Yes":"No");
}
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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.range;
enum PS = [11, 31, 41, 61, 71, 101, 131, 151, 181, 191, 211, 241, 251, 271, 281, 311, 331, 401, 421, 431, 461, 491, 521, 541, 571, 601, 631, 641, 661, 691, 701, 751, 761, 811, 821, 881, 911, 941, 971,991, 1021, 1031, 1051, 1061, 1091, 1151, 1171, 1181, 1201, 1231, 1291, 1301, 1321, 1361, 1381].to!(string[]);
void main()
{
writeln(PS[0..readln.chomp.to!int].join(" "));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
alias Plan = Tuple!(int, "t", int, "x", int, "y");
bool calc(Plan[] plans) {
int t = 0;
int x = 0;
int y = 0;
foreach (p; plans) {
int dt = p.t - t;
int dx = abs(x - p.x);
int dy = abs(y - p.y);
if (dt < dx + dy) return false;
if ((dt - (dx + dy)) % 2 != 0) return false;
t = p.t;
x = p.x;
y = p.y;
}
return true;
}
void main() {
int n = readint;
Plan[] plans;
for (int i = 0; i < n; i++) {
auto txy = readints;
plans ~= Plan(txy[0], txy[1], txy[2]);
}
writeln(calc(plans) ? "Yes" : "No");
}
|
D
|
import std.conv, std.stdio;
import std.algorithm, std.range, std.string;
void main()
{
auto i = readln.chomp.split.to!(int[]);
only(i[0]+i[1], i[0]-i[1], i[0]*i[1]).reduce!max.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10^^9L+7;
long[10^^5*2+1] SS;
bool[10^^5*2+1] BS;
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
foreach (_; 0..M) {
auto a = readln.chomp.to!int;
BS[a] = true;
}
SS[0] = 1;
foreach (i; 0..N) {
if (BS[i]) continue;
SS[i+1] = (SS[i+1] + SS[i]) % P;
SS[i+2] = (SS[i+2] + SS[i]) % P;
}
writeln(SS[N]);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
const alpha = 26, a = 'a';
void main()
{
auto n = readln.chomp.to!size_t;
auto b = new int[](alpha), bs = new int[](alpha);
b[] = int.max;
foreach (_; 0..n) {
auto s = readln.chomp;
bs[] = 0;
foreach (c; s) ++bs[c-a];
foreach (i; 0..alpha) b[i] = min(b[i], bs[i]);
}
foreach (i; 0..alpha)
foreach (j; 0..b[i])
write(cast(char)(a+i));
writeln;
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
import core.bitop;
void main()
{
long N = lread();
auto X = sread();
ulong m = X.map!"a == '1'"().sum();
ulong plain0 = () {
if (m == 1)
return -1;
long ret;
foreach (i, c; X)
{
if (c == '1')
{
ret += powmod(2, X.length - i - 1, m - 1);
ret %= m - 1;
}
}
return ret;
}();
ulong plain1 = () {
long ret;
foreach (i, c; X)
{
if (c == '1')
{
ret += powmod(2, X.length - i - 1, m + 1);
ret %= m + 1;
}
}
return ret;
}();
foreach (i; 0 .. X.length)
{
if (X[i] == '1' && m == 1)
{
writeln(0);
continue;
}
ulong y = () {
if (X[i] == '1')
{
return ((m - 1) + plain0 - powmod(2, X.length - i - 1, m - 1)) % (m - 1);
}
return (plain1 + powmod(2, X.length - i - 1, m + 1)) % (m + 1);
}();
// dprint(i, X[i] == '1', y, plain0, plain1);
long ans = 1;
while (y != 0)
{
y = g(y);
ans++;
}
writeln(ans);
}
}
ulong g(ulong x)
{
return x % popcnt(x);
}
/// x^^n % m
T powmod(T = long)(T x, T n, T m = 10 ^^ 9 + 7)
{
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1, m) % m;
}
T tmp = powmod(x, n / 2, m);
return tmp * tmp % m;
}
|
D
|
import std.stdio,std.string,std.conv,std.array;
void main(){
int[26] arr;
for(;;){
auto rc = readln().chomp();
if(!rc){ break; }
foreach( c ; rc ){
if( 'a' <= c && c <= 'z' ){
arr[c-'a']++;
} else if ( 'A' <= c && c <= 'Z' ){
arr[c-'A']++;
}
}
}
foreach( key,i ; arr ){
writeln( to!char(key+'a')," : ",i );
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int a,b,c,x;
scan(a);
scan(b);
scan(c);
scan(x);
int ans;
foreach(i;0..a+1){
foreach(j;0..b+1){
foreach(k;0..c+1){
if(500*i+100*j+50*k == 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() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, m;
rd(n, m);
auto a = readln.split.to!(long[]);
long[long] map;
long tot = 0, mulm = 0;
for (int i = 0; i < n; i++) {
if (i > 0) {
a[i] += a[i - 1];
}
if (a[i] % m == 0) {
tot += (++mulm);
} else {
if (a[i] % m in map)
tot += map[a[i] % m];
if (a[i] % m in map)
map[a[i] % m]++;
else
map[a[i] % m] = 1;
}
}
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.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv,
std.format,
std.math;
void main()
{
auto ip = readln.chomp;
if(ip[0] == ip[2]) writeln("=");
else if(ip[0] < ip[2]) writeln("<");
else writeln(">");
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int h, w;
rd(h, w);
int a, b;
rd(a, b);
auto p = h / a, q = w / b;
writeln((h - p * a) * w + h * (w - q * b) - ((h - p * a) * (w - q * b)));
}
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto X = aryread();
long ans = long.max;
foreach (x; 1 .. 100 + 1)
{
long s;
foreach (i; 0 .. N)
{
s += (X[i] - x) ^^ 2;
}
ans = ans.min(s);
}
writeln(ans);
}
|
D
|
import std.algorithm;
import std.range;
import std.ascii;
import std.array;
import std.container;
import std.conv;
import std.numeric;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) {
stderr.writeln(arg);
}
int size(T)(in T s) {
return cast(int)s.length;
}
long modPow(long mod)(long x, long n) {
if (n == 0) return 1;
if (n % 2 == 0) return modPow!mod(x * x % mod, n / 2);
return x * modPow!mod(x % mod, n - 1) % mod;
}
class RollingHash(long[] Ps, long mod) {
string s;
int N;
long[][] H;
this(in string s) {
this.s = s;
this.N = s.size;
this.H = new long[][](Ps.length, N + 1); // H[k]: s[0]*Ps[k]^(n-1) + s[1]*Ps[k]^(n-2) + ... + s[n-1]*Ps[k]^0
foreach (k, P; Ps) {
foreach (int i, c; s) {
H[k][i + 1] = (H[k][i] * P + cast(int)(c - 'a' + 1)) % mod;
}
}
}
long[] hash(int l, int r) { // [l, r)
auto hs = iota(0, Ps.length, 1).map!(delegate(k) {
return (H[k][r] - H[k][l] * modPow!mod(Ps[k], r - l) % mod + mod) % mod;
});
return hs.array;
}
}
void main() {
int N, M; scanf("%d %d\n", &N, &M);
auto s = readln.chomp;
auto hasher = new RollingHash!([991, 9999991], cast(long)(1e9+7))(s);
int l = 0, r = 1;
bool[long[]] appeared;
foreach (_; 0 .. M) {
auto q = readln.chomp;
switch (q) {
case "L++": l++; break;
case "L--": l--; break;
case "R++": r++; break;
case "R--": r--; break;
default: assert(false);
}
appeared[hasher.hash(l, r).idup] = true;
}
writeln(appeared.keys.size);
}
|
D
|
import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum MO = 10L^^9 + 7;
int N;
string S;
void main() {
try {
for (; ; ) {
N = readInt();
S = readToken();
int[char] cnt;
foreach (k; S) {
++cnt[k];
}
long ans = 1;
foreach (k, v; cnt) {
ans = ans * (v + 1) % MO;
}
ans -= 1;
ans = (ans % MO + MO) % MO;
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
long a = read.to!long;
long b = read.to!long;
long ab = read.to!long;
long ans;
if(a == b) ans = ab * 2 + a + b;
else if(a == b + 1 || b == a + 1) ans = ab * 2 + a + b;
else ans = ab * 2 + min(a, b) * 2 + 1;
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int N, M;
scan(N, M);
auto adj = new int[][](N, 0);
foreach (i ; 0 .. M) {
int xi, yi;
scan(xi, yi);
xi--, yi--;
adj[xi] ~= yi;
}
auto ord = topSort(adj);
auto dp = new int[](N);
int ans;
foreach (v ; ord) {
ans = max(ans, dp[v]);
foreach (u ; adj[v]) {
chmax(dp[u], dp[v] + 1);
}
}
writeln(ans);
}
int[] topSort(int[][] adj) {
auto N = adj.length.to!int;
auto deg = new int[](N);
foreach (v ; 0 .. N) {
foreach (u ; adj[v]) {
deg[u]++;
}
}
int[] res;
int[] next;
foreach (v ; 0 .. N) {
if (!deg[v]) next ~= v;
}
while (!next.empty) {
int[] nn;
foreach (v ; next) {
res ~= v;
foreach (u ; adj[v]) {
deg[u]--;
if (!deg[u]) nn ~= u;
}
}
next = nn;
}
return res;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main() {
string s = readln.chomp;
writeln(2 * min(s.count('0'), s.count('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 std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math, std.typecons;
void main() {
int n; scan(n);
auto ban = new int[][](1010, 1010);
foreach (i ; 0 .. n) {
int x1, y1, x2, y2;
scan(x1, y1, x2, y2);
ban[x1][y1]++;
ban[x1][y2]--;
ban[x2][y1]--;
ban[x2][y2]++;
}
foreach (x ; 0 .. 1010) {
foreach (y ; 0 .. 1009) {
ban[x][y+1] += ban[x][y];
}
}
foreach (y ; 0 .. 1010) {
foreach (x ; 0 .. 1009) {
ban[x+1][y] += ban[x][y];
}
}
int ans;
foreach (x ; 0 .. 1010) {
foreach (y ; 0 .. 1010) {
ans = max(ans, ban[x][y]);
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main()
{
int n = readln().chomp().to!int();
while (n--) {
int times = 3;
int Hit = 0;
int result = 0;
while (times) {
string str = readln().chomp();
if (str == "HIT") {
if (Hit == 3) result++;
else Hit++;
} else if (str == "OUT") {
times--;
} else {
result += (Hit + 1);
Hit = 0;
}
}
writeln(result);
}
return 0;
}
|
D
|
void main() {
auto S = rs;
if(S == "ABC") {
writeln("ARC");
} else writeln("ABC");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int m, d; scan(m,d);
int ans;
foreach (i; 1..m+1) {
foreach (j; 1..d+1) {
int d1 = j%10, d2 = (j/10) % 10;
if (d1>=2 && d2>=2 && d1*d2 == i) ans++;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
int ans = 0;
for (int i = 0; i < N; i++) {
if (A[i] == i + 1) {
ans += 1;
i += 1;
}
}
ans.writeln;
}
|
D
|
void main() {
while(true) {
auto S = rs.to!(dchar[]);
if(S == "-") break;
auto m = ri;
foreach(i; 0..m) {
auto h = ri;
S = S[h..$] ~ S[0..h];
}
S.writeln;
}
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int count2(int n) {
int ret = 0;
while (n > 0 && n % 2 == 0) {
n /= 2;
ret++;
}
return ret;
}
void main() {
readint;
auto a = readints;
int ans = a.map!(count2).reduce!((a, b) => min(a, b));
writeln(ans);
}
|
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;
struct Deque(T) {
private {
int N, head, tail;
T[] deq;
}
this(int n) {
N = n + 1;
deq = new T[](N + 1);
}
bool empty() {
return head == tail;
}
bool full() {
return head == ((tail + 1) % N);
}
int length() {
return (tail - head + N) % N;
}
T front() {
assert(!empty);
return deq[head];
}
T back() {
assert(!empty);
return deq[(tail - 1 + N) % N];
}
void pushFront(T x) {
assert(!full);
head = (head - 1 + N) % N;
deq[head] = x;
}
void pushBack(T x) {
assert(!full);
deq[tail++] = x;
tail %= N;
}
void popFront() {
assert(!empty);
head = (head + 1) % N;
}
void popBack() {
assert(!empty);
tail = (tail - 1 + N) % N;
}
alias insert = pushBack;
alias removeFront = popFront;
alias removeBack = popBack;
T opIndex(size_t index)
in {
assert(0 <= index && index < length);
}
body {
return deq[(head + index) % N];
}
alias opDollar = length;
}
unittest {
import std.stdio;
auto q = Deque!(int)(8);
q.insert(3);
q.insert(1);
q.insert(4);
q.insert(1);
assert(q[3] == 1);
assert(q[0] == 3);
q.removeFront;
q.removeFront;
// q = [4, 1]
q.insert(8);
q.pushFront(7);
q.insert(11);
q.pushFront(0);
// q = [0, 7, 4, 1, 8, 11]
assert(q[0] == 0);
assert(q[1] == 7);
assert(q[4] == 8);
assert(q[$-1] == 11);
}
unittest {
import std.stdio;
int n = 5;
auto deq = Deque!(int)(n);
deq.pushFront(3);
deq.pushBack(1);
deq.pushBack(4);
deq.pushFront(5);
assert(deq.front == 5);
assert(deq.back == 4);
assert(deq.length == 4);
deq.pushFront(0);
assert(deq.full);
assert(deq.front == 0);
assert(deq.length == 5);
deq.popFront();
deq.popFront();
deq.popBack();
assert(deq.front == 3);
assert(deq.back == 1);
assert(deq.length == 2);
deq.popBack();
deq.popFront();
assert(deq.empty);
assert(deq.length == 0);
}
void main() {
string s;
scan(s);
auto deq = Deque!(char)(4 * 10^^5);
foreach (ch ; s) {
deq.pushBack(ch);
}
int Q;
scan(Q);
bool rev;
foreach (_ ; 0 .. Q) {
auto ln = readln.split;
if (ln.front == "1") {
rev ^= 1;
}
else {
auto f = ln[1].to!int;
auto c = ln[2].to!char;
f--;
if ((f ^ rev) == 0) {
deq.pushFront(c);
}
else {
deq.pushBack(c);
}
}
}
string ans;
if (!rev) {
while (!deq.empty) {
ans ~= deq.front;
deq.removeFront();
}
}
else {
while (!deq.empty) {
ans ~= deq.back;
deq.removeBack();
}
}
writeln(ans);
}
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;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string input = chomp(readln());
switch(input) {
case "a", "i", "u", "e", "o":
"vowel".writeln;
break;
default:
"consonant".writeln;
break;
}
}
|
D
|
import std.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
void main()
{
auto x = readln.chomp.to!int;
writeln(x^^3);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
auto a = aryread();
long x = 3 ^^ n;
long y = 1;
foreach (i; 0 .. n)
{
if (a[i] % 2 == 0)
{
y *= 2;
}
}
writeln(x - y);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio, std.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 = RDR.ARR;
writeln(a[2] >= a[0] && a[2] <= a[1] ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
void main() {
auto tmp = readln.split.to!(int[]);
auto W = tmp[1], H = tmp[0];
int[][] cost;
foreach (i; 0..10) {
tmp = readln.split.to!(int[]);
cost ~= tmp;
}
foreach (k; 0..10) {
foreach (i; 0..10) {
foreach (j; 0..10) {
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}
}
}
ulong res = 0;
foreach (i; 0..H) {
tmp = readln.split.to!(int[]);
foreach (j; 0..W) {
auto a = tmp[j];
if (a == -1) continue;
res += cost[a][1];
}
}
writeln(res);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
auto n = readln.chomp.to!uint;
writeln((n + 1) / 2);
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(T[], "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n, m;
cin.scan(n, m);
writeln((1900 * m + 100 * (n - m)) * 2 ^^ m);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(size_t[]), n = rd[0], m = rd[1];
auto a = new string[](n), b = new string[](m);
foreach (i; 0..n) a[i] = readln.chomp;
foreach (i; 0..m) b[i] = readln.chomp;
auto isSame(size_t i, size_t j)
{
foreach (k; 0..m)
if (a[i+k][j..j+m] != b[k]) return false;
return true;
}
foreach (i; 0..n-m+1)
foreach (j; 0..n-m+1)
if (isSame(i, j)) {
writeln("Yes");
return;
}
writeln("No");
}
|
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() {
int n = readint;
auto xs = readints;
auto deque = Deque!int(n);
for (int i = 0; i < xs.length; i++) {
if ((i & 1) == (n & 1)) {
deque.insertBack(xs[i]);
}
else {
deque.insertFront(xs[i]);
}
}
for (int i = 0; i < n; i++) {
if (i > 0) write(' ');
write(deque[i]);
}
writeln();
}
struct Deque(T) {
private T[] _buffer;
private int _front;
private int _back;
this(int size) {
_buffer = new int[size * 3];
_front = _back = (size * 3) / 2;
}
bool empty() @property { return _front == _back; }
int length() @property { return _back - _front; }
void insertFront(T value) {
if (empty) {
_buffer[_front] = value;
_back++;
return;
}
_buffer[--_front] = value;
}
void insertBack(T value) {
if (empty) {
_buffer[_front] = value;
_back++;
return;
}
_buffer[_back++] = value;
}
void removeFront() {
assert(!empty);
++_front;
}
void removeBack() {
assert(!empty);
--_back;
}
T opIndex(int index) {
return _buffer[_front + index];
}
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.algorithm;
import std.conv;
int[] g;
int insertionSort(ref int[] arr, int n, int g) {
int cnt = 0;
for (auto i = g; i < n; i++) {
int v = arr[i];
int j = i - g;
while (j >= 0 && arr[j] > v) {
arr[j+g] = arr[j];
j = j - g;
cnt++;
}
arr[j+g] = v;
}
return cnt;
}
int shellSort(ref int[] arr, int n) {
int cnt = 0;
int n_ = n;
do {
int g_ = n_ / 2;
g ~= g_;
n_ -= g_;
} while (n_ != 1);
for (auto i = 0; i < g.length; i++) {
cnt += insertionSort(arr, n, g[i]);
}
return cnt;
}
void main(string[] args)
{
int n = to!int(strip(readln()));
int[] arr;
for (int i = 0; i < n; i++) arr ~= to!int(strip(readln()));
int cnt = shellSort(arr, n);
writeln(g.length);
writeln(join(g.map!(text), " "));
writeln(cnt);
foreach(i; arr) { writeln(i); }
}
|
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 n, k;
scan(n, k);
auto s = readln.chomp;
auto pf = new int[](n + 1);
pf[1] = s[0] == '0';
foreach (i ; 2 .. n + 1) {
if (s[i - 1] == '0' && s[i - 2] == '1') {
pf[i] = pf[i - 1] + 1;
}
else {
pf[i] = pf[i - 1];
}
}
debug {
writeln(pf);
}
bool check(int x) {
foreach (i ; 0 .. n - x + 1) {
int d = pf[i + x] - pf[i];
if (i > 0 && s[i] == '0' && s[i - 1] == '0') d++;
if (d <= k) {
return true;
}
}
return false;
}
int ok = 0, ng = n + 1;
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
(check(mid) ? ok : ng) = mid;
}
writeln(ok);
}
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.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
bool chmin(T)(ref T a, T b) {
if (b < a) {
a = b;
return true;
} else {
return false;
}
}
void main() {
int N = readln.chomp.to!int;
string S = readln.chomp;
int[][] dp = new int[][](2, N);
foreach (i, ch; S) {
if (ch == 'W') {
if (i > 0) {
dp[0][i] = dp[0][i - 1] + 1;
} else {
dp[0][i] = 1;
}
} else {
if (i > 0) {
dp[0][i] = dp[0][i - 1];
} else {
dp[0][i] = 0;
}
}
}
foreach_reverse (i, ch; S) {
if (ch == 'E') {
if (i < N - 1) {
dp[1][i] = dp[1][i + 1] + 1;
} else {
dp[1][i] = 1;
}
} else {
if (i < N - 1) {
dp[1][i] = dp[1][i + 1];
} else {
dp[1][i] = 0;
}
}
}
int[] ans = new int[](N);
foreach (i; 0..N) {
ans[i] = dp[0][i] + dp[1][i];
}
int min = int.max;
foreach (a; ans) {
chmin(min, a);
}
writeln(min - 1);
}
|
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 N = readln.split[0].to!int;
// p = 10^^k <= N < 10^^(k+1)
int p = 1, k = 0;
foreach (i; 0 .. 10) {
if (p <= N && N < p*10) break;
p *= 10; k += 1;
}
int result;
foreach (num; all(k+1)) {
if (num > N) break;
if (has(num, 3) && has(num, 5) && has(num, 7)) result++;
}
result.writeln;
}
// does n has m in its digit
bool has(int n, int m) {
if (n < 10) return n == m;
int n_ = n/10;
return m == n - n_*10 || has(n_, m);
}
int[] generate(int n) {
if (n == 1) return [3, 5, 7];
auto next = generate(n-1);
int[] result;
foreach (num; next) { result ~= [num*10+3, num*10+5, num*10+7]; }
return result;
}
int[] all(int n) {
if (n == 1) return generate(1);
else return all(n-1) ~ generate(n);
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto a = readln.splitter.map !(to !(int)).array;
writeln (isSorted (a) ? "NO" : "YES");
}
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc154/tasks/abc154_b
// simulation
import std.stdio;
import std.string;
void main() {
string S = readln.chomp;
foreach(ch; S) {
'x'.write;
} writeln;
}
|
D
|
import std.stdio;
import std.string;
void main ()
{
int divs = 0;
foreach (p; 2..51)
{
bool ok = true;
for (int d = 2; d * d <= p; d++)
{
if (p % d == 0)
{
ok = false;
}
}
if (!ok)
{
continue;
}
writeln (p);
stdout.flush ();
bool cur = (readln.strip == "yes");
divs += cur;
if (cur && p * p <= 100)
{
writeln (p * p);
stdout.flush ();
bool next = (readln.strip == "yes");
divs += next;
}
}
writeln (divs > 1 ? "composite" : "prime");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] h; readA(n, h);
int[] a; readA(n, a);
auto st = new SegmentTree!(long, max)(n+1);
foreach (i; 0..n)
st[h[i]] = st[0..h[i]] + a[i];
writeln(st[0..$]);
}
class SegmentTree(T, alias pred = "a + b")
{
import core.bitop, std.functional;
alias predFun = binaryFun!pred;
const size_t n, an;
T[] buf;
T unit;
this(size_t n, T unit = T.init)
{
this.n = n;
this.unit = unit;
an = n == 1 ? 1 : (1 << ((n-1).bsr + 1));
buf = new T[](an*2);
if (T.init != unit) buf[] = unit;
}
this(T[] init, T unit = T.init)
{
this(init.length, unit);
buf[an..an+n][] = init[];
foreach_reverse (i; 1..an)
buf[i] = predFun(buf[i*2], buf[i*2+1]);
}
void opIndexAssign(T val, size_t i)
{
buf[i += an] = val;
while (i /= 2)
buf[i] = predFun(buf[i*2], buf[i*2+1]);
}
pure T opSlice(size_t l, size_t r)
{
l += an; r += an;
T r1 = unit, r2 = unit;
while (l != r) {
if (l % 2) r1 = predFun(r1, buf[l++]);
if (r % 2) r2 = predFun(buf[--r], r2);
l /= 2; r /= 2;
}
return predFun(r1, r2);
}
pure T opIndex(size_t i) { return buf[i+an]; }
pure size_t opDollar() { return n; }
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
char[] t;
foreach (i, c; s)
if (i % 2 == 0) t ~= c;
writeln(t);
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int h = tmp[0], w = tmp[1];
string[] a;
foreach (i; 0 .. h) {
string row = readln.chomp;
if (row.any!(c => c == '#')) a ~= row;
}
h = a.length.to!int;
int j;
while (j < w) {
int cnt;
foreach (i; 0 .. h) {
if (a[i][j] == '.') ++cnt;
}
if (cnt == h) {
foreach (i; 0 .. h) {
a[i] = a[i][0..j] ~ a[i][j+1..$];
}
--w;
} else {
++j;
}
}
foreach (x; a) {
x.writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n;
scanf("%d\n", &n);
string s = readln().chomp;
int[] score = new int[n];
{
int cur = 0;
foreach (i; 0..n) {
score[i] += cur;
if (s[i] == 'W') cur++;
}
}
{
int cur = 0;
foreach_reverse (i; 0..n) {
score[i] += cur;
if (s[i] == 'E') cur++;
}
}
int ans = n;
foreach (i; 0..n) {
ans = min(ans, score[i]);
}
writeln(ans);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split, a = rd[0], b = rd[1], c = rd[2];
writeln(a[$-1] == b[0] && b[$-1] == c[0] ? "YES" : "NO");
}
|
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;
void main() {
int N, i;
scan(N, i);
auto ans = N + 1 - 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);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
// import chie template :) {{{
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 += ModInt(rhs).value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
auto r = ModInt(rhs);
if (val < r.value) {
val += modulus;
}
val -= r.value;
} else if (op == "*") {
val = val * ModInt(rhs).value % 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 a, b, c, d;
cin.scan(a, b, c, d);
bool flg = true;
while (true) {
if (flg) {
c -= b;
if (c <= 0) {
writeln("Yes");
return;
}
} else {
a -= d;
if (a <= 0) {
writeln("No");
return;
}
}
flg = !flg;
}
}
|
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;
string s;
cin.scan(s);
foreach (e; s) {
if (e == '7') {
writeln("Yes");
return;
}
}
writeln("No");
}
|
D
|
import std.functional,
std.algorithm,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv,
std.math;
T read_num(T)() { return readln.chomp.to!T; }
T[] read_nums(T)() { return readln.chomp.split.to!(T[]); }
enum INF = 1L << 30;
T max(T)(T a, T b) { return a < b ? b : a; }
void main() {
long N = read_num!long;
long[] A = read_nums!long;
long[2][] dp;
dp.length = N + 1;
dp[0][0] = 0;
dp[0][1] = -INF;
for (size_t i = 0; i < N; i++) {
long a = A[i];
dp[i + 1][0] = max(dp[i][0] + a, dp[i][1] - a);
dp[i + 1][1] = max(dp[i][0] - a, dp[i][1] + a);
}
writeln(dp[N][0]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
import std.math;
import std.range;
void main() {
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
auto x = 1;
foreach (i; 0..N) {
if (x < K) x *= 2;
else x += K;
}
writeln(x);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long a1, a2, a3;
scan(a1, a2, a3);
if (a1 + a2 + a3 <= 21)
{
writeln("win");
}
else if (22 <= a1 + a2 + a3)
{
writeln("bust");
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio,std.string,std.conv;
int main()
{
string s;
while((s = readln.chomp).length != 0)
{
for(int i=0;i<s.length;i++)
{
if(s[i] == '@')
{
int count = s[i+1] - '0';
foreach(j;0..count)
{
write(s[i+2]);
}
i += 2;
}
else
{
write(s[i]);
}
}
writeln;
}
return 0;
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto s = sread();
auto k = lread();
// writeln(s);
// writeln(k);
foreach (i; 0 .. k)
{
if (s[i] != '1')
{
writeln(s[i]);
return;
}
}
writeln(1);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
void main(){
string s = readln().chomp();
int ans;
foreach(elm; s){
ans += (elm=='+')?1:-1;
}
ans.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
writeln(s.uniq.array.length-1);
}
|
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() {
alias Section = Tuple!(int, "l", int, "r");
auto data = readln.split, N = data[0].to!int, M = data[1].to!int, Q = data[2].to!int;
int[500][500] train;
int[500][500] row_sum; // row_sum[l][r] = t_1r + ... + t_lr
foreach (i; 0 .. M) {
data = readln.split, train[data[0].to!int-1][data[1].to!int-1]++;
}
foreach (r; 0 .. 500) {
int s;
foreach (l; 0 .. 500) {
s += train[l][r];
row_sum[l][r] = s;
}
}
int[] p, q;
foreach (j; 0 .. Q) {
data = readln.split;
p ~= data[0].to!int-1, q ~= data[1].to!int-1;
}
foreach (j; 0 .. Q) {
int ans;
foreach (r; p[j] .. q[j]+1) {
if (p[j]==0) { ans += row_sum[q[j]][r]; /*row_sum[q[j]][r].writeln;*/ }
else { ans += (row_sum[q[j]][r] - row_sum[p[j]-1][r]); /*(row_sum[q[j]][r] - row_sum[p[j]-1][r]).writeln;*/ }
}
writeln(ans);
}
}
|
D
|
unittest
{
assert( [ "pot", "top" ].parse.expand.solve == "YES" );
assert( [ "tab", "bet" ].parse.expand.solve == "NO" );
assert( [ "eye", "eel" ].parse.expand.solve == "NO" );
}
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto css = new string[ 2 ];
foreach( ref cs; css )
{
cs = input.front.strip;
input.popFront;
}
return tuple( css );
}
auto solve( string[] css )
{
return ( css[ 0 ][ 0 ] == css[ 1 ][ 2 ] &&
css[ 0 ][ 1 ] == css[ 1 ][ 1 ] &&
css[ 0 ][ 2 ] == css[ 1 ][ 0 ] ) ? "YES" : "NO";
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.string;
import std.array;
void main() {
auto l = readln.chomp.split.map!(to!int).array;
int a = l[0], b = l[1], x = l[2];
for (int i = 0; i <= b; ++i) {
if (a + i == x) {
writeln("YES");
return;
}
}
writeln("NO");
return;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
for(int n; 0!=(n=readln().chomp().to!int()); )
{
auto a=0,b=0;
foreach(i;1..n+1)
{
auto t=i;
while(t%2==0) t/=2,++a;
while(t%5==0) t/=5,++b;
}
writeln(min(a,b));
}
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto a = aryread();
long sum_a;
foreach (i; 0 .. a.length)
{
sum_a += a[i];
}
// writeln(sum_a);
// writeln(cast(double) sum_a / a.length);
auto tmp = cast(double) sum_a / a.length;
auto ans = new double[](a.length);
foreach (i; 0 .. a.length)
{
// writeln(abs(a[i] - tmp));
ans[i] = abs(a[i] - tmp);
}
// writeln(ans);
long ansans;
auto min_x = double.max;
foreach (i; 0 .. ans.length)
{
if (min_x > ans[i])
{
min_x = ans[i];
ansans = i;
}
}
writeln(ansans);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
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 main()
{
long x = lread();
long max_pow = 1;
foreach (e; 2 .. 40)
{
long i = 2,j;
while(1)
{
j = pow(e,i);
i++;
if(j > x)
{
break;
}
max_pow = max(j, max_pow);
}
}
writeln(max_pow);
}
|
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, n;
scan(d, n);
if (n == 100) {
writeln(100^^(d+1) + 100^^d);
return;
}
writeln(100^^d * n);
}
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;
import core.bitop;
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, "x", long, "c");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
long n, k;
scan(n, k);
auto a = aryread();
long ok = a.reduce!(max), ng = -1;
while (abs(ok - ng) > 1)
{
long cntCut(long len)
{
return len > 0 ? a.map!(x => (x + len - 1) / len - 1).sum() : INF;
}
auto mid = (ok + ng) / 2;
// writefln("mid = %s, counts = %s", mid, cntCut(mid));
if (cntCut(mid) <= k)
ok = mid;
else
ng = mid;
}
ok.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math, std.container, std.typecons;
import core.stdc.stdio;
// foreach, foreach_reverse, writeln
void main() {
int n, k;
scanf("%d%d", &n, &k);
int[] a = new int[n];
foreach (i ; 0..n) scanf("%d", &a[i]);
int s = 10^^9;
foreach (x ; a) s = min(s, x);
int ans = 0;
int i = 0;
while (i < n) {
if (a[i] == s) {
i++;
continue;
}
ans++;
int cnt = 0;
int ti = min(n, i+k);
foreach (j ; i..ti) {
if (a[j] == s) cnt++;
}
if (cnt == 0 && ti == i+k) {
i = ti-1;
} else {
i = ti;
}
}
writeln(ans);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
const int n=3;
auto c=new int[][](n, n);
foreach(i; 0..n) c[i]=readln.split.to!(int[]);
for(int a0=-105; a0<=105; a0++){
auto a=new int[](n), b=new int[](n);
a[0]=a0;
foreach(j; 0..n) b[j]=c[0][j]-a[0];
foreach(i; 1..n) a[i]=c[i][0]-b[0];
bool ok=true;
foreach(i; 1..n)foreach(j; 1..n){
if(c[i][j]!=a[i]+b[j]) ok=false;
}
if(ok){writeln("Yes"); return;}
}
writeln("No");
}
/+
a1+b1 a1+b2 a1+b3
a2+b1 a2+b2 a2+b3
a3+b1 a3+b2 a3+b3
+/
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
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int t; rd(t);
while(t--){
auto s=readln.chomp.to!(char[]);
int d=0, l=0, u=0;
foreach(c; s){
if('0'<=c && c<='9') d++;
if('a'<=c && c<='z') l++;
if('A'<=c && c<='Z') u++;
}
if(d>0 && l>0 && u>0){
writeln(s);
continue;
}
if(d==0){
foreach(i; 0..s.length){
auto c=s[i];
if('a'<=c && c<='z' && l>1){
s[i]='0';
break;
}
if('A'<=c && c<='Z' && u>1){
s[i]='0';
break;
}
}
}
if(l==0){
foreach(i; 0..s.length){
auto c=s[i];
if('0'<=c && c<='9' && d>1){
s[i]='a';
break;
}
if('A'<=c && c<='Z' && u>1){
s[i]='a';
break;
}
}
}
if(u==0){
foreach(i; 0..s.length){
auto c=s[i];
if('0'<=c && c<='9' && d>1){
s[i]='A';
break;
}
if('a'<=c && c<='z' && l>1){
s[i]='A';
break;
}
}
}
writeln(s);
}
}
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
|
void main()
{
long h, w, k;
rdVals(h, w, k);
string[] s = h.rdCol!string;
long result = inf;
outer: foreach (i; 0 .. 1L<<(h-1))
{
long cnt = i._popcnt;
long[][] list = new long[][](w, cnt+1);
foreach (j; 0 .. w)
{
long idx;
foreach (l; 0 .. h)
{
list[j][idx] += s[l][j] - '0';
if (list[j][idx] > k) continue outer;
if ((i>>l)&1) ++idx;
}
}
long[] num = new long[cnt+1];
foreach (j; 0 .. w)
{
num[] += list[j][];
if (num.any!(x => x > k))
{
num = list[j];
++cnt;
}
}
result = min(result, cnt);
}
result.writeln;
}
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.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 l = new long[](n);
auto r = new long[](n);
foreach (i; 0..n)
{
l[i] = RD;
r[i] = RD;
}
auto edges = new int[][](n);
foreach (i; 0..n-1)
{
auto u = RD!int-1;
auto v = RD!int-1;
edges[u] ~= v;
edges[v] ~= u;
}
long[][] dfs(int pos, int last)
{
long[][] res = [[l[pos], 0], [r[pos], 0]];
foreach (v; edges[pos])
{
if (v == last) continue;
auto tmp = dfs(v, pos);
auto ll = abs(tmp[0][0] - l[pos]) + tmp[0][1];
auto lr = abs(tmp[0][0] - r[pos]) + tmp[0][1];
auto rl = abs(tmp[1][0] - l[pos]) + tmp[1][1];
auto rr = abs(tmp[1][0] - r[pos]) + tmp[1][1];
res[0][1] += max(ll, rl);
res[1][1] += max(lr, rr);
}
return res;
}
auto res = dfs(0, -1);
ans[ti] = max(res[0][1], res[1][1]);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.math;
import std.stdio;
import std.string;
struct Input
{
ulong n;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
n = readln().strip().to!ulong;
}
}
struct Output
{
}
auto main2(Input* input)
{
struct PrimeInfo
{
ulong prime;
ulong count;
}
PrimeInfo[] primes;
PrimeInfo first = {2,0};
primes ~= first;
bool isPrime(ulong n)
{
auto r = floor(sqrt(cast(real)n));
foreach (p; primes)
{
if (p.prime > r)
return true;
if (n % p.prime == 0)
return false;
}
return true;
}
auto temp = input.n;
while (true)
{
if (temp % 2 == 0)
{
primes[$-1].count++;
temp /= 2;
}
else
{
break;
}
}
for (ulong n = 3; temp != 1; n+=2)
{
if (isPrime(n))
{
PrimeInfo pi = {n,0};
primes ~= pi;
if (n > sqrt(floor(cast(real)temp)))
break;
while (true)
{
if (temp % n == 0)
{
primes[$-1].count++;
temp /= n;
}
else
{
break;
}
}
}
}
if (temp != 1)
{
PrimeInfo pi = {temp, 1};
primes ~= pi;
}
ulong[] counts;
counts.length = primes.length;
ulong max = 1;
ulong r = cast(ulong)(sqrt(cast(real)input.n));
LOOP:
while (true)
{
foreach (i, ref c; counts)
{
if (c < primes[i].count)
{
c++;
break;
}
else
{
c = 0;
if (i == counts.length - 1)
break LOOP;
}
}
auto mul = 1;
foreach (i, c; counts)
{
mul *= primes[i].prime ^^ c;
}
if (mul <= r && mul > max)
max = mul;
}
return (max - 1) + (input.n / max - 1);
}
unittest { writeln("begin unittest"); }
unittest // example1
{
string example =
`10`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 5);
}
unittest // example2
{
string example =
`50`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 13);
}
unittest // example3
{
string example =
`10000000019`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 10000000018);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
void main()
{
Input input = void;
parseInput(input, stdin);
auto result = main2(&input);
writeln(result);
}
|
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 = 10L^^15;
enum mod = 10L ^^ 9 + 7;
void main() {
int h, w;
scan(h, w);
auto ban = iota(h).map!(i => readln.chomp).array;
auto dp = new long[][](h, w);
dp[0][0] = 1;
foreach (i ; 0 .. h) {
foreach (j ; 0 .. w) {
if (ban[i][j] == '#') {
continue;
}
if (i > 0) {
dp[i][j] += dp[i-1][j];
dp[i][j] %= mod;
}
if (j > 0) {
dp[i][j] += dp[i][j-1];
dp[i][j] %= mod;
}
}
}
writeln(dp[h-1][w-1]);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void calc(string[] ss) {
int rows = cast(int) ss.length;
int cols = cast(int) ss[0].length;
int[][] adj(int row, int col) { // 隣接 8 マス
auto dr = [-1, -1, -1, 0, 0, 1, 1, 1];
auto dc = [-1, 0, 1, -1, 1, -1, 0, 1];
int[][] ret;
for (int i = 0; i < dr.length; i++) {
int r = dr[i] + row;
int c = dc[i] + col;
if (0 <= r && r < rows && 0 <= c && c < cols)
ret ~= [r, c];
}
return ret;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (ss[i][j] == '#') {
write('#');
continue;
}
int bomb = 0;
foreach (e; adj(i, j)) {
if (ss[e[0]][e[1]] == '#')
bomb++;
}
write(bomb);
}
writeln;
}
}
void main() {
auto hw = readints;
int rows = hw[0];
string[] ss;
for (int i = 0; i < rows; i++) {
ss ~= readln.chomp;
}
calc(ss);
}
|
D
|
import std.stdio, std.string, std.conv, std.math;
void main() {
int n = readln.chomp.to!int;
long debt = 100000;
foreach(i;0..n) debt = (((debt+debt*0.05)/1000.0).ceil*1000).to!long;
debt.writeln;
}
|
D
|
import std.stdio;
import std.string;
void main() {
auto s = readln.chomp;
writeln(s[0..1], s.length - 2, s[$ - 1..$]);
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;
immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);
void main(){
int N, M, a, b;
readVars(N, M);
auto ncnt = new int[](N);
foreach(i ; 0 .. M){
readVars(a, b);
a--; b--;
ncnt[a]++;
ncnt[b]++;
}
foreach(i ; 0 .. N){
if (ncnt[i] & 1) {
writeln("NO");
return;
}
}
writeln("YES");
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() { return readln.chomp.to!int; }
int[] readints() { return readln.split.to!(int[]); }
void main() {
int n = readint;
int[] a;
int[string] db; // name -> int
for (int i = 0; i < n; i++) {
auto ax = readln.split;
string material = ax[0];
int price = ax[1].to!int;
db[material] = i;
a ~= price;
}
auto uf = new UnionFind(n);
int m = readint;
for (int i = 0; i < m; i++) {
auto ss = readln.split;
// s と t は自由に交換可能
int s = db[ss[0]];
int t = db[ss[1]];
// rs, rt の集合の最安値を更新
int rs = uf.root(s);
int rt = uf.root(t);
uf.unite(s, t);
a[uf.root(s)] = min(a[rs], a[rt]);
}
int ans = iota(n).map!(e => a[uf.root(e)]).sum;
writeln(ans);
}
class UnionFind {
private int[] _data;
private int[] _nexts; // 次の要素。なければ -1
private int[] _tails; // 末尾の要素
this(int n) {
_data = new int[](n + 1);
_nexts = new int[](n + 1);
_tails = new int[](n + 1);
_data[] = -1;
_nexts[] = -1;
for (int i = 0; i < _tails.length; i++)
_tails[i] = i;
}
int root(int a) {
if (_data[a] < 0) return a;
return _data[a] = root(_data[a]);
}
bool unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb) return false;
// ra に rb を繋げる
_data[ra] += _data[rb];
_data[rb] = ra;
// ra の末尾の後ろに rb を繋げる
_nexts[_tails[ra]] = rb;
// ra の末尾が rb の末尾になる
_tails[ra] = _tails[rb];
return true;
}
bool isSame(int a, int b) {
return root(a) == root(b);
}
/// a が属する集合のサイズ
int groupSize(int a) {
return -_data[root(a)];
}
/// a が属する集合の要素全て
void doEach(int a, void delegate(int) fn) {
auto node = root(a);
while (node != -1) {
fn(node);
node = _nexts[node];
}
}
}
|
D
|
import std.stdio, std.ascii;
void main() {
auto d = stdin.readln.dup;
foreach (c; d) {
if (c.isLower) c.toUpper.write;
else c.toLower.write;
}
}
|
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);
foreach (i; 0..s.length-1) {
if (s[i] == s[i+1]) {
writeln(i+1, " ", i+2);
return;
}
if (i < s.length-2 && s[i] == s[i+2]) {
writeln(i+1, " ", i+3);
return;
}
}
writeln(-1, " ", -1);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto m = readln.chomp.to!int;
writeln(48-m);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto s=readln.chomp.to!(char[]);
if(s.length>"AKIHABARA".length){
writeln("NO"); return;
}
auto cs=['K', 'I', 'H', 'B', 'R'];
bool[char] exist; exist['A']=true;
foreach(c; cs) exist[c]=true;
foreach(c; s){
if((c in exist)==null){writeln("NO"); return;}
}
auto cnt1=new int[](cs.length);
foreach(c; s)foreach(i, c1; cs){
if(c==c1) cnt1[i]++;
}
foreach(e; cnt1){
if(e!=1){writeln("NO"); return;}
}
long idx=-1;
foreach(c; cs){
auto jdx=indexOf(s, c);
if(jdx==-1){writeln("NO"); return;}
if(idx>=jdx){writeln("NO"); return;}
idx=jdx;
}
auto cnt=new long[](cs.length+1);
cnt[0]=indexOf(s, cs[0]);
foreach(i; 1..cs.length){
cnt[i]=indexOf(s, cs[i])-indexOf(s, cs[i-1])-1;
}
cnt[$-1]=s.length-indexOf(s, cs[$-1])-1;
auto ok=[1, 0, 0, 1, 1, 1]; assert(ok.length==cnt.length);
foreach(i; 0..cnt.length){
if(cnt[i]>ok[i]){writeln("NO"); return;}
}
writeln("YES");
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.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, K;
scan(N, K);
auto A = aryread();
long I;
foreach (i; 0 .. N)
if (A[i] == 1)
{
I = i;
break;
}
long a = ((N - 1) + (K - 2)) / (K - 1);
writeln(a);
}
|
D
|
// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio : readln, writeln;
import std.array : split;
import std.string : chomp;
import std.conv : to;
auto gets() { return readln.chomp; }
auto getVals(T)() { return gets.split.to!(T[]); }
void main() {
auto xs = getVals!long;
auto l = xs[0], r = xs[1], x = xs[2], y = xs[3], k = xs[4];
for (auto i = x; i <= y; i++) {
auto v = i * k;
if (l <= v && v <= r) {
writeln("YES");
return;
}
}
writeln("NO");
}
|
D
|
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;
alias M = Tuple!(int, "a", int, "b", int, "c");
void main()
{
immutable nm = readln.chomp.split(" ").map!(to!int).array;
immutable n = nm[0];
immutable ma = nm[1];
immutable mb = nm[2];
auto meds = new M[n];
foreach(ref m; meds) {
auto abc = readln.chomp.split(" ").map!(to!int).array;
m = M(abc[0], abc[1], abc[2]);
}
auto memo = new int[500][500][41];
int buy(int a, int b, size_t idx)
{
if (memo[idx][a][b])
return memo[idx][a][b];
if (idx >= n) {
immutable div = gcd(a, b);
if (div) {
a /= div;
b /= div;
}
return memo[idx][a][b] = ma == a && mb == b ? 1 : int.max;
}
auto head = meds[idx];
auto a_ = a + head.a;
auto b_ = b + head.b;
auto ret1 = buy(a, b, idx+1);
auto ret2 = buy(a_, b_, idx+1);
ret2 = ret2 == int.max ? ret2 : ret2 + head.c;
return memo[idx][a][b] = min(ret1, ret2);
}
immutable min_c = buy(0, 0, 0);
writeln(min_c == int.max ? -1 : min_c -1);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Clue = Tuple!(long, "x", long, "y", long, "h");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
long cnt;
void main()
{
auto s = sread();
auto before = (s.length - 1) / 2;
auto after = (s.length + 3) / 2 - 1;
// writeln(s[0 .. before]);
// writeln(s[after .. $]);
if(s.is_reverse() && is_reverse(s[0 .. before]) && is_reverse(s[after .. $]))
writeln("Yes");
else
writeln("No");
}
bool is_reverse(string s)
{
bool ret = true;
foreach(i; iota(s.length))
{
ret &= (s[i] == s[$ - 1 - i]);
}
return ret;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto a = new long[](N);
foreach (i; 0..N)
{
a[i] = RD-1;
}
auto used = new bool[](N);
long ans;
long pos;
used[0] = true;
while (pos != 1)
{
if (used[a[pos]])
{
ans = -1;
break;
}
else
{
pos = a[pos];
used[pos] = true;
++ans;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=rdsp;foreach(ref j;v.tupleof)pick(r,j);}}
void main()
{
struct R { int c, t; }
int n, ts; readV(n, ts);
R[] r; readS(n, r);
auto c = r.filter!(ri => ri.t <= ts).map!"a.c";
if (c.empty)
writeln("TLE");
else
writeln(c.reduce!min);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=readln.split.to!(int[]);
int cur=-1, len=1;
int need=0;
foreach(e; a){
if(cur==e) len++;
else{
need+=len/2;
len=1;
cur=e;
}
}
writeln(need+len/2);
}
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
|
void main(){
import std.stdio, std.algorithm;
long n, a, b, k; rd(n, a, b, k);
const long mod=998244353;
const int M=1_000_00*4;
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.