code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
void main()
{
long k, a, b;
rdVals(k, a, b);
long rate = 1;
if (k <= a)
{
rate.writeln;
return;
}
if (b >= a)
{
writeln(-1);
return;
}
rate += (k - b - 1) / (a - b) * 2;
rate.writeln;
}
enum long mod = 10L^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, 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 L, R;
scan(L, R);
auto D = R - L + 1;
if (D >= 2019) {
writeln(0);
return;
}
long ans = 2019;
foreach (i ; L .. R + 1) {
foreach (j ; i + 1 .. R + 1) {
ans = min(ans, (1L * i * j) % 2019);
}
}
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 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 a = readint;
int b = readint;
int c = readint;
int d = readint;
writeln(min(a, b) + min(c, d));
}
|
D
|
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
auto n = readln.chomp;
int[10] cnt;
foreach (i, ref e; cnt) {
e = cast(int) n.count!(c => c - '0' == i);
}
debug { writeln(cnt); }
long[20][20] binom;
binom[0][0] = 1;
foreach (i; 1..20) {
foreach (j; 0..20) {
binom[i][j] = binom[i-1][j] + (j > 0 ? binom[i-1][j-1] : 0);
}
}
debug { binom.each!writeln; }
bool next_variant(ref int[10] lim) {
foreach_reverse (i; 0..10) {
if (cnt[i] == 0) continue;
if (lim[i] < cnt[i]) {
++lim[i];
return true;
} else {
lim[i] = 1;
}
}
return false;
}
int[10] lim;
foreach (i; 0..10) if (cnt[i] > 0) lim[i] = 1;
long ans = 0;
do {
int left = sum(lim[0..$]);
long cur = binom[left - 1][lim[0]];
left -= lim[0];
foreach (e; lim[1..10]) {
cur *= binom[left][e];
left -= e;
}
ans += cur;
} while (next_variant(lim));
ans.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, core.bitop;
// dfmt on
void main()
{
long N, M, X;
scan(N, M, X);
auto CA = new long[][](N);
foreach (i; 0 .. N)
CA[i] = aryread();
long ans = long.max;
foreach (state; 0 .. (1 << N))
{
auto x = new long[](M + 1);
foreach (i; 0 .. N)
if (state & (1 << i))
{
x[] += CA[i][];
}
if (x[1 .. $].all!(a => X <= a))
{
ans = ans.min(x[0]);
}
}
writeln(ans == long.max ? -1 : ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto G = new int[][](N);
foreach (_; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= s[1]-1;
G[s[1]-1] ~= s[0]-1;
}
if (N == 2) {
writeln(A[0] == A[1] ? "YES" : "NO");
return;
}
int root;
foreach (i; 0..N) {
if (G[i].length >= 2) {
root = i;
break;
}
}
bool dfs(int n, int p) {
if (G[n].length == 1) {
return true;
}
long sm = 0;
long mx = 0;
foreach (m; G[n]) {
if (m != p) {
if (!dfs(m, n)) return false;
sm += A[m];
mx = max(mx, A[m]);
}
}
if (sm < A[n] || sm > A[n] * 2) return false;
long k = sm - A[n];
if (k > sm - mx) return false;
A[n] -= k;
return n == root ? A[n] == 0 : (A[n] >= 0);
}
writeln(dfs(root, -1) ? "YES" : "NO");
}
|
D
|
// unihernandez22
// https://atcoder.jp/contests/abc150/tasks/abc150_b
// implementation
import std.stdio: readln, writeln;
import std.string: chomp;
import std.algorithm: count;
void main() {
readln;
string s = readln.chomp;
s.count("ABC").writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto C = new long[](26);
foreach (i; 0..N) C[S[i] - 'a'] += 1;
long ans = N.to!long * (N - 1).to!long / 2;
foreach (i; 0..26) {
ans -= C[i] * (C[i] - 1) / 2;
}
writeln(ans + 1);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
int[int] NS;
void main()
{
auto N = readln.chomp.to!int;
foreach (_; 0..N) {
auto A = readln.chomp.to!int;
if (A in NS)
++NS[A];
else
NS[A] = 1;
}
int ret;
foreach (a; NS.byValue) if (a & 1) ++ret;
writeln(ret);
}
|
D
|
import std.stdio, std.math, std.conv, std.string;
bool is_prime(int n)
{
auto t = sqrt(n.to!double);
for (int i = 2; i <= t; ++i) {
if (n % i == 0) return false;
}
return true;
}
void main()
{
auto N = readln.chomp.to!int;
int cnt;
foreach (_; 0..N) {
if (is_prime(readln.chomp.to!int)) ++cnt;
}
writeln(cnt);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
string s;
scan(s);
int n = s.length.to!int;
auto a = new int[](n + 1);
foreach (i ; 0 .. n) {
a[i + 1] = a[i] ^ (1 << (s[i] - 'a'));
}
debug {
writeln(a);
}
auto opt = new int[](n + 1);
auto dp = new int[](1<<26);
dp[] = n;
dp[0] = 0;
foreach (i ; 1 .. n + 1) {
int mn = dp[a[i]];
foreach (j ; 0 .. 26) {
mn = min(mn, dp[a[i] ^ (1<<j)]);
}
opt[i] = mn + 1;
dp[a[i]] = min(dp[a[i]], opt[i]);
debug {
writeln(opt);
}
}
writeln(opt[n]);
}
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.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.numeric;
void main() {
const tmp = readln.split.to!(long[]);
const N = tmp[0], M = tmp[1], C = tmp[2];
const bs = readln.split.to!(long[]);
N.iota.map!(_ => readln.split.to!(long[]).dotProduct(bs) + C > 0 ? 1 : 0).sum.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range;
void main() {
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
auto xs = readln.split.to!(int[]);
xs.map!(x => 2 * min(x, K-x)).sum.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
auto a = readln.split.to!(int[]);
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n) {
if ((a[i] - a[j]) % (n - 1) == 0) {
writeln(a[i], " ", a[j]);
return;
}
}
}
}
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.regex;
void main(){
auto io = new IO();
auto S = io.line!string()[0];
if ( S.have("N") ^ S.have("S") || S.have("W") ^ S.have("E") ){
writeln("No");
}else{
writeln("Yes");
}
return;
}
import std.stdio,std.conv,std.string;
import std.algorithm,std.array,std.math;
class IO
{
T[] line( T = size_t , string token = " " )( size_t m = 1 ){
T[] arr = [];
foreach( i ; 0..m ){
arr ~= this.read!T();
}
return arr;
}
T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){
T[][] arr = new T[][](m);
foreach( i ; 0..m ){
arr[i] = this.read!T(token);
}
return arr;
}
private T[] read( T = size_t )( string token = " " ){
T[] arr;
foreach( elm ; readln().chomp().split(token) ){
arr ~= elm.to!T();
}
return arr;
}
}
// T -> T[]
pure T[] step( T = size_t )( in T begin , in T end , in T step = 1 ){
T[] ret;
for( T i = begin ; i < end ; i += step ){
ret ~= i;
}
return ret;
}
unittest{
assert( step(0,10,2) == [0,2,4,6,8] );
}
// T[] -> T[]
pure T[] fill( T )( T[] args , in T filling ){
foreach( ref arg ; args ){
arg = filling;
}
return args;
}
T[] map( T )( T[] args , T function(T) f ){
foreach( ref arg ; args ){
arg = f(arg);
}
return args;
}
unittest{
assert( [true,false,true,false].fill(true) == [true,true,true,true] );
assert( [1,-2,3,-4].map!int(x=>x*2) == [2,-4,6,-8] );
}
// T[] -> number
pure T sum( T )( in T[] args ){
T ret = 0;
foreach( i ; 0..args.length ){
ret += args[i];
}
return ret;
}
pure T ave(T)( in T[] args ){
return args.sum()/args.length;
}
pure real multi( in real[] args ){
real ret = 1.0;
foreach( i ; 0..args.length ){
ret *= args[i];
}
return ret;
}
// T[] -> bool
pure bool any( T )( in T[] args , in T target ){
foreach( arg ; args ){
if( arg == target ){return true;}
}
return false;
}
pure bool all( T )( in T[] args , in T cond ){
foreach( arg ; args ){
if( arg != cond ){return false;}
}
return true;
}
pure bool have( T )( in T[] args , in T[] target ... ){
bool[] found = new bool[](target.length);
foreach( arg ; args ){
foreach( i,t ; target ){
if( arg == t ){
found[i] = true;
}
}
}
foreach( f ; found ){
if( f == false ){return false;}
}
return true;
}
unittest{
assert( [1,2,3,4,5].sum() == 15 );
assert( [1,2,3,4,5].ave() == 3 );
assert( cmp( [0.3,0.3].multi() , 0.09 ) , "multi() test failed" );
assert( cmp( [0.3,0.3,0.3].multi() , 0.027 ) , "multi() test failed" );
assert( [1,1,1].all(1) == true );
assert( [1,1,2].all(1) == false );
assert( [1,1,2].any(2) == true );
assert( [1,1,2].any(3) == false );
assert( [1,2,3,4,5].have([1,3,5]) == true );
assert( [1,2,3,4,5].have([1,3,5,7]) == false );
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
string str = readln.chomp;
int[char] count;
for(int i=0; i<str.length; i++) {
count[str[i]]++;
}
int odd = 0;
char key;
foreach(char k, int v; count) {
if (v%2 != 0) {
odd++;
key = k;
}
}
if (odd > 1) {
writeln(0);
} else {
count[key]--;
long x = 0;
long y = 1;
foreach(int v; count.values) {
x += v/2;
y *= fact(v/2);
}
writeln(fact(x)/y);
}
}
long factorial(long n) {
if (n==0) return 1;
return n*factorial(n-1);
}
alias memoize!factorial fact;
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdlib;
immutable long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
auto K = N % 2 ? 2 : 1;
auto dp = new long[][](N + 5, K + 1);
foreach (ref d; dp) d[] = -(1L << 59);
dp[0][0] = 0;
dp[1][1] = 0;
if (K == 2) dp[2][2] = 0;
foreach (i; 0..N) foreach (k; 0..K+1) {
dp[i + 2][k] = max(dp[i + 2][k], dp[i][k] + A[i]);
if (k + 1 <= K) dp[i + 3][k + 1] = max(dp[i + 3][k + 1], dp[i][k] + A[i]);
if (k + 2 <= K) dp[i + 4][k + 2] = max(dp[i + 4][k + 2], dp[i][k] + A[i]);
}
long ans = -(1L << 59);
foreach (i; N..N+5) ans = max(ans, dp[i][K]);
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 x, y;
scan(x, y);
long ans;
auto mc = ModComb(x + 1);
foreach (a ; 0 .. x + 1) {
auto xr = x - a;
auto yr = y - 2*a;
if (xr < 0 || yr < 0) continue;
if (xr % 2 == 1 || xr / 2 != yr) continue;
auto b = xr / 2;
(ans += mc.c(a + b, a)) %= mod;
}
writeln(ans);
}
struct ModComb {
int _N;
long[] _fact, _factinv;
long _mod;
this(int n, long mod = 1_000_000_007L) {
_N = n;
_fact = new long[](_N + 1);
_factinv = new long[](_N + 1);
_mod = mod;
_fact[0] = 1;
foreach (i ; 1 .. _N + 1) {
_fact[i] = (_fact[i-1] * i) % mod;
}
_factinv[_N] = _powmod(_fact[_N], mod - 2);
foreach_reverse (i ; 0 .. _N) {
_factinv[i] = (_factinv[i+1] * (i+1)) % mod;
}
}
long c(int n, int k) {
if (k < 0 || k > n) return 0;
return f(n) * finv(n - k) % _mod * finv(k) % _mod;
}
long p(int n, int r) {
if (r < 0 || r > n) return 0;
return f(n) * finv(n - r) % _mod;
}
long f(int n) {
return _fact[n];
}
long finv(int n) {
return _factinv[n];
}
long _powmod(long x, long y) {
return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio, std.bitmanip;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0].to!int;
auto A = s[1];
auto B = s[2];
auto X = readln.split.map!(to!long);
long ans = 0;
foreach (i; 1..N) {
ans += min((X[i]-X[i-1])*A, B);
}
ans.writeln;
}
|
D
|
void main(){
string[] val = inln!string();
bool a = val[0]=="H";
bool b = val[1]=="H";
writeln( a^b?'D':'H');
}
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;
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;
long calc(long n, long m) {
if (n > m) return calc(m, n);
if (n == 1) {
if (m == 1) return 1;
return m - 2;
}
if (n == 2) return 0;
return (n - 2) * (m - 2);
}
void main() {
auto nm = readints;
int n = nm[0], m = nm[1];
auto ans = calc(n, m);
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
import std.uni;
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 s1, s2, s3; readV(s1, s2, s3);
writeln(s1[0].toUpper, s2[0].toUpper, s3[0].toUpper);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.string;
void main()
{
auto tokens = split(chomp(readln()));
int a = to!int(tokens[0]);
int b = to!int(tokens[1]);
if ((a * b) % 2 == 0)
writeln("No");
else
writeln("Yes");
stdout.flush();
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.typecons, std.math;
int h, w, d; rd(h, w, d);
auto c=new int[][](h, w);
foreach(i; 0..h) c[i]=readln.split.to!(int[]);
foreach(i; 0..h)foreach(j; 0..w) c[i][j]--;
alias P=Tuple!(int, "i", int, "j");
auto map=new P[](h*w);
foreach(int i; 0..h)foreach(int j; 0..w) map[c[i][j]]=P(i, j);
long cost(P a, P b){
return (a.i-b.i).abs+(a.j-b.j).abs;
}
auto dp=new long[](h*w);
foreach(i; d..(h*w)) dp[i]=dp[i-d]+cost(map[i-d], map[i]);
int q; rd(q);
while(q--){
int l, r; rd(l, r); l--; r--;
writeln(dp[r]-dp[l]);
}
}
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;
int calc(string s) {
int[int] d = [0: 1];
int x = 0;
int p = 1;
foreach_reverse (c; s) {
x = (x + (c - '0') * p) % 2019;
p = 10 * p % 2019;
d[x]++;
}
return d.values.map!(e => e * (e - 1) / 2).sum;
}
void main() {
string s = read!string;
writeln(calc(s));
}
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;
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
void fill(ref int[][] m, int x, int y) {
if(m[x][y] == 1) {
m[x][y] = 0;
for(int i = -1; i <= 1; ++i) {
for(int j = -1; j <= 1; ++j) {
fill(m, x+i, y+j);
}
}
}
}
void main(){
while(true){
auto wh = readInts();
auto w = wh[0];
auto h = wh[1];
if(w == 0) {
return;
}
int[][] m;
for(int i; i < h; ++i) {
m ~= [0] ~ readInts() ~ [0];
}
m = [new int[](w+2)] ~ m;
m ~= new int[](w+2);
int ans;
for(int i; i < h; ++i) {
for(int j; j < w; ++j) {
if(m[i+1][j+1] == 1) {
++ans;
fill(m, i+1, j+1);
}
}
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.conv, std.range, std.string, std.array;
void main(){
auto ip = readln.chomp;
writeln(ip.count('1'));
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], m = tmp[1];
long[] a = new long[n+2];
foreach (i; 0 .. m) {
tmp = readln.split.to!(int[]);
int l = tmp[0] - 1, r = tmp[1];
++a[l];
--a[r];
}
foreach (i; 0 .. n+1) {
a[i+1] += a[i];
}
a.count(m).writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.typecons;
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main()
{
int N = readln().chomp().to!int();
string[] num = readln().split();
for (int i = N - 1; i > 0; i--)
write(num[i], " ");
writeln(num[0]);
return 0;
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional,std.mathspecial;
void main(){
auto nm=readln.split.map!(to!int).array;
auto n=nm[0];
auto m=nm[1];
int[][] abs;
foreach(i;0..m)abs~=readln.split.map!(to!int).array;
int[][] routes;
routes~=[1];
foreach(i;0..n-1){
int[][] newRoutes;
foreach(x;1..n+1){
foreach(r;routes){
if(r.find(x).empty)newRoutes~=(r~x);
}
}
routes=newRoutes;
}
long res=0;
outer:foreach(r;routes){
foreach(i;0..n-1){
if(abs.find(r[i..i+2]).empty && abs.find([r[i+1],r[i]]).empty)continue outer;
}
++res;
}
writeln(res);
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.numeric;
// import dcomp.foundation, dcomp.scanner, dcomp.algorithm;
bool solve() {
auto sc = new Scanner(stdin);
int n; long k; long[] a;
sc.read(n, k, a);
long u = 0;
foreach (d; a) u = gcd(u, d);
if (k > a.maximum || k % u) {
return false;
}
return true;
}
int main() {
if (solve) {
writeln("POSSIBLE");
} else {
writeln("IMPOSSIBLE");
}
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
|
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) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto a = RD!char;
writeln(a-'a' >= 0 && a-'a' <= 26 ? 'a' : 'A');
stdout.flush;
debug readln;
}
|
D
|
void main() {
int x = readln.chomp.to!int;
writeln(x < 1200 ? "ABC" : "ARC");
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.ascii;
import std.algorithm;
void main()
{
foreach (_; 0..readln.chomp.to!int) {
auto script = readln;
auto keys = script.split.filter!(str => str.length == 4);
loop:
foreach (a; [0, 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]) {
foreach (b; 0..26) {
foreach (key; keys) {
string decoded = key.decrypt(a, b);
if (decoded == "this" || decoded == "that") {
script.decrypt(a, b).write;
break loop;
}
}
}
}
}
}
string decrypt(string input, int a, int b)
{
string output;
foreach (c; input) {
output ~= isAlpha(c) ? ((c - 'a') * a + b) % 26 + 'a' : c;
}
return output;
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int n = readln.chomp.to!int;
int[][] a = new int[][](n, n);
foreach (i; 0 .. n) {
int[] tmp = readln.chomp.split.to!(int[]);
foreach (j; 0 .. tmp[1]) {
a[tmp[0]-1][tmp[j+2]-1] = 1;
}
}
foreach (i; 0 .. n) {
foreach (j, x; a[i]) {
x.write;
if (j != n - 1) " ".write;
else writeln;
}
}
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
const N = readln.chomp.to!int;
void solve() {
void dfs(string s, char mx) {
if (s.length == N) {
s.writeln;
return;
}
for(char c = 'a'; c <= mx; c++) {
dfs(s ~ c, c == mx ? cast(char)(mx + 1) : mx);
}
}
dfs("", 'a');
}
solve();
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto N = S.length;
int r1, r2;
foreach (i; 0..N) {
auto c = i%2 == 0 ? '0' : '1';
if (S[i] == c) ++r1;
if (S[i] != c) ++r2;
}
writeln(min(r1, r2));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int p, q, r;
scan(p, q, r);
auto ans = min(p + q, p + r, q + r);
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 std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons;
import std.math, std.numeric;
void main() {
int n; scan(n);
auto a = readln.split.to!(long[]);
long[long] cnt;
cnt[0] = 1;
long s;
long ans;
foreach (i ; 0 .. n) {
s += a[i];
if (s in cnt) {
ans += cnt[s];
cnt[s]++;
}
else {
cnt[s] = 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
|
/+ dub.sdl:
name "A"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner, dkh.algorithm;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n;
sc.read(n);
writeln((n - 2) * 180);
return 0;
}
/* IMPORT /home/yosupo/Programs/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/Programs/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/Programs/dunkelheit/source/dkh/algorithm.d */
// module dkh.algorithm;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = l + (r-l) / 2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
import std.range.primitives;
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "minimum: range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "maximum: range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() const {
return now.empty;
}
@property auto front() const {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
/* IMPORT /home/yosupo/Programs/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;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
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 : enforce;
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.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
alias P = Tuple!(int, "to", int, "d");
void main()
{
auto ve = readln.split.to!(int[]);
auto V = ve[0];
auto E = ve[1];
P[][] ps;
ps.length = V;
foreach (_; 0..E) {
auto std = readln.split.to!(int[]);
auto s = std[0];
auto t = std[1];
auto d = std[2];
ps[s] ~= P(t, d);
}
auto DP = new int[][][](V, V, 1<<V);
foreach (ref dp; DP) foreach (ref dp2; dp) dp2[] = -1;
int solve(int i, int f, uint s) {
if (s == (1<<V)-1) {
foreach (n; ps[i]) if (n.to == f) return n.d;
return int.max/3;
}
if (DP[i][f][s] == -1) {
auto r = int.max/2;
foreach (n; ps[i]) if (!(s & (1<<n.to))) {
r = min(r, solve(n.to, f, s | (1<<n.to)) + n.d);
}
DP[i][f][s] = r;
}
return DP[i][f][s];
}
auto r = int.max;
foreach (i; 0..V) r = min(r, solve(i, i, 1<<i));
if (r > 15000) r = -1;
writeln(r);
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto A = scan!int(N);
long solve() {
long ans;
foreach(i, a; A) {
if (i % 2 == 0 && a % 2 == 1) ans++;
}
return ans;
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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 long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto r = RDA;
foreach (i; 0..n)
{
if (r[i] == 2) continue;
++ans[ti];
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main() {
string input;
while ((input = readln.chomp).length != 0) {
auto line = input.map!(a => a.to!int - '0').array;
while (line.length > 1) {
for (int i = 0; i < (line.length - 1); i++) {
line[i] = line[i] + line[i+1];
}
line.popBack;
}
writeln(line.front % 10);
}
}
|
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() {
string s = read!string;
char[] buf;
foreach (c; s) {
switch (c) {
case '0': buf ~= '0'; break;
case '1': buf ~= '1'; break;
case 'B':
if (buf.length > 0) {
buf = buf[0..$-1];
}
break;
default:
break;
}
}
writeln(buf);
}
|
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;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
int[] ra (int n) {
auto a = r.nextA!int (n);
sort (a);
return a;
}
foreach (tid; 0 .. nt) {
long res = long.max;
auto a = r.nextA!uint (3);
auto t = new int[][3];
foreach (k; 0 .. 3) t[k] = ra (a[k]);
void check (long u, long v, long w) {
long q = (u - v) * (u - v);
q += (u - w) * (u - w);
q += (v - w) * (v - w);
if (res > q) res = q;
}
void f (in int[] x, in int[] y, in int[] z) {
size_t j, k;
foreach (i; 0 .. x.length) {
int xv = x[i];
while (j < y.length && y[j] < xv) ++j;
while (k < z.length && z[k] < xv) ++k;
void go (size_t u, size_t v) {
if (u < y.length && v < z.length)
check (xv, y[u], z[v]);
}
go (j, k);
if (k > 0) {
go (j, k - 1);
}
if (j > 0) {
go (j - 1, k);
if (k > 0) {
go (j - 1, k - 1);
}
}
}
}
f (t[0], t[1], t[2]);
f (t[1], t[0], t[2]);
f (t[2], t[0], t[1]);
writeln (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() {
readint;
auto a = readints;
auto dp = new long[a.length];
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
for (int i = 2; i < a.length; i++) {
dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
dp[i - 2] + abs(a[i] - a[i - 2]));
}
writeln(dp[a.length - 1]);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto r = RD;
auto D = RD;
auto x2000 = RD;
long last = x2000;
foreach (i; 0..10)
{
auto x = last * r - D;
writeln(x);
last = x;
}
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 N = readln.chomp.to!int;
auto A = readln.chomp.map!(a => a == '1').array;
auto B = readln.chomp.map!(a => a == '1').array;
auto cnt = new long[](4);
foreach (i; 0..N) {
if (!A[i] && !B[i]) {
cnt[0] += 1;
} else if (A[i] && !B[i]) {
cnt[1] += 1;
} else if (!A[i] && B[i]) {
cnt[2] += 1;
} else {
cnt[3] += 1;
}
}
auto ans = cnt[0] * cnt[1] + cnt[0] * cnt[3] + cnt[1] * cnt[2];
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
int c;
foreach (a; AS) {
while (a && a%2 == 0) {
++c;
a /= 2;
}
}
writeln(c);
}
|
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;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
class Node{
Node[] adjnodes;
Node[] kids;
Node parent;
int id;
this(int id){
this.id = id;
}
void add(Node nd){
this.adjnodes ~= nd;
nd.adjnodes ~= this;
}
void setKids(){
foreach(nd; this.adjnodes){
if(!this.parent || nd.id != this.parent.id){
this.kids ~= nd;
nd.parent = this;
nd.setKids;
}
}
}
int getStatus(){ // 0: I am single; 1: I am paired; 2: Cannot pair
if(this.kids.length == 0){
return 0 /* single */;
}
else if(this.kids.length == 1){
int s = this.kids[0].getStatus;
if(s == 0 /* single */) return 1 /* paired */;
else if(s == 1) return 0 /* single */;
else return 2 /* error */;
}
else{
int s = 0 /* single */;
foreach(nd; this.kids){
int s1 = nd.getStatus;
if(s1 == 2 /* error */) return 2 /* error */;
if(s1 == 0 /* single */){
if(s == 0 /* single */) s = 1 /* paired */;
else return 2 /* error */;
}
}
return s;
}
}
}
void main(){
int n = read.to!int;
Node[int] nodes;
foreach(i; 1 .. n + 1){
nodes[i] = new Node(i);
}
foreach(i; 1 .. n){
int a = read.to!int;
int b = read.to!int;
nodes[a].add(nodes[b]);
}
nodes[1].setKids;
string ans;
if(nodes[1].getStatus == 1 /* paired */) ans = "Second";
else ans = "First";
ans.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto x = readln.chomp.to!int;
immutable m = 200_000 + 1;
auto f = new bool[](m);
auto res = 1;
foreach (i; 2..m) {
if (f[i]) continue;
else if (x <= i) {
res = i;
break;
}
foreach (j; iota(i, m, i)) {
f[j] = true;
}
}
res.writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.array, std.range;
void main() {
int t = readln.chomp.to!(int);
long ans = long.min;
long[][] arr;
long[][] dp = new long[][](t, t);
for(long i = 0; i < t; i++)
arr ~= (readln.chomp.split(" ").map!(to!(long)).array);
for(int i = 0; i < t; i++) {
for(int j = 0; j < t; j++) {
int sum = 0;
for(int k = j; k < t; k++) {
sum += arr[i][k];
dp[j][k] = max(dp[j][k], 0) + sum;
if (ans < dp[j][k]) ans = dp[j][k];
}
}
}
writeln(ans);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string _1, s, _2; readV(_1, s, _2);
writeln('A', s[0], 'C');
}
|
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;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long X, Y;
scan(X, Y);
writeln(X + Y / 2);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int rows, int cols, string[] g) {
auto t = new int[][](rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
t[i][j] = int.max;
}
}
int[][] q;
q ~= [0, 0, 0];
while (q.length > 0) {
auto s = q[0];
q = q[1..$];
int r = s[0];
int c = s[1];
int cost = s[2];
if (t[r][c] <= cost) continue;
t[r][c] = cost;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (abs(i) + abs(j) == 1) {
int row = r + i;
int col = c + j;
if (0 <= row && row < rows && 0 <= col && col < cols) {
if (g[row][col] == '#') continue;
q ~= [row, col, cost + 1];
}
}
}
}
}
assert(t[rows-1][cols-1] != int.max);
int last = t[rows-1][cols-1];
if (last == int.max) return -1;
int ans = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int cost = t[i][j];
if (cost == int.max && g[i][j] == '#') continue;
ans++;
}
}
return ans - last - 1;
}
void main() {
auto hw = readints;
int rows = hw[0], cols = hw[1];
string[] g;
for (int i = 0; i < rows; i++) {
g ~= readln.chomp;
}
int ans = calc(rows, cols, g);
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string S;
scan(S);
auto N = S.length;
bool ok = true;
foreach (i ; 0 .. N) {
if (i % 2 == 0) {
if (S[i] == 'L') ok = false;
}
else {
if (S[i] == 'R') ok = false;
}
}
writeln(ok ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.array;
import std.string;
import std.conv;
void main(){
int[] inputs = readln.chomp.split.map!(to!int).array;
auto n = inputs[0];
auto a = inputs[1];
auto b = inputs[2];
auto answer = min(n * a, b);
writeln(answer);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
string s;
scan(s);
int n = s.length.to!int;
int btm = 1, top = n + 1;
bool check(int k) {
if (2*k <= n) {
return true;
}
bool b = 1;
char ch = s[n-k];
foreach (i ; n-k .. k) {
b &= s[i] == ch;
}
return b;
}
while (top - btm > 1) {
int mid = (btm + top) / 2;
if (check(mid)) {
btm = mid;
}
else {
top = mid;
}
}
writeln(btm);
}
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.array, std.conv, std.algorithm.iteration, std.functional;
void main()
{
auto ip = readln.chomp.split(" ").map!(pipe!((c => c.to!int), (i => i == 1 ? 14 : i))).array;
auto a = ip[0];
auto b = ip[1];
writeln(
a > b ? "Alice"
: a < b ? "Bob"
: "Draw"
);
}
|
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 H, W;
scan(H, W);
auto S = new string[](H);
foreach (i; 0 .. H)
S[i] = sread();
foreach (y; 0 .. H)
foreach (x; 0 .. W)
if (S[y][x] == '#')
{
bool u = (0 < y) ? S[y - 1][x] == '#' : false;
bool d = (y + 1 < H) ? S[y + 1][x] == '#' : false;
bool l = (0 < x) ? S[y][x - 1] == '#' : false;
bool r = (x + 1 < W) ? S[y][x + 1] == '#' : false;
if (!(u || d || l || r))
{
writeln("No");
return;
}
}
writeln("Yes");
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.algorithm;
void main()
{
auto ab = readln.chomp.split(" ").map!(to!int);
auto a = ab[0];
auto b = ab[1];
auto ret = a + b;
while (ret > 23) ret -= 24;
writeln(ret);
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.functional;
Scanner sc;
static this() {
sc = new Scanner(stdin);
}
long N;
int main() {
long l = 10L^^10 - 1, r = 10L^^11;
while (r-l > 1) {
long md = (l+r)/2;
if (!query(md)) {
l = md;
} else {
r = md;
}
}
long ans = r;
while (true) {
if (ans < 10) break;
long na = ans/10 + 1;
if (query(na) == (na.to!string < ans.to!string)) {
break;
}
ans /= 10;
}
answer(ans);
return 0;
}
bool query(long n) {
writeln("? ", n);
stdout.flush;
char c;
sc.read(c);
return c == 'Y';
// if (n < N && n.to!string <= N.to!string) return true;
// if (n > N && n.to!string > N.to!string) return true;
// return false;
}
void answer(long n) {
writeln("! ", n);
stdout.flush;
writeln("answer ", n, " ", N);
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;
import std.traits;
T pow(T, U)(T x, U n) if (!isFloatingPoint!T && isIntegral!U) {
return pow(x, n, T(1));
}
T pow(T, U)(T x, U n, T e) if (isIntegral!U) {
while (n) {
if (n & 1) e *= x;
x *= x;
n /= 2;
}
return e;
}
T lcm(T)(in T a, in T b) {
import std.numeric : gcd;
return a / gcd(a,b) * b;
}
T[3] extGcd(T)(in T a, in T b)
if (!isIntegral!T || isSigned!T)
{
if (b==0) {
return [1, 0, a];
} else {
auto e = extGcd(b, a%b);
return [e[1], e[0]-a/b*e[1], e[2]];
}
}
/* 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;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/functional.d */
// module dcomp.functional;
struct memoCont(alias pred) {
import core.exception : RangeError;
import std.range, std.algorithm, std.conv;
import std.string : join;
import std.traits : ReturnType, ParameterTypeTuple, isIntegral;
import std.typecons : tuple, Tuple;
import std.meta;
alias R = ReturnType!pred;
alias Args = ParameterTypeTuple!pred;
static assert (allSatisfy!(isIntegral, Args));
static immutable N = Args.length;
int[2][N] rng;
int[N] len;
R[] dp;
bool[] used;
void init(int[2][N] rng) {
this.rng = rng;
len = rng[].map!(a => a[1]-a[0]+1).array;
int sz = len.reduce!"a*b";
dp = new R[sz];
used = new bool[sz];
}
R opCall(Args args) {
int idx, base = 1;
foreach (i, v; args) {
version(assert) {
if (v < rng[i][0] || rng[i][1] < v) {
throw new RangeError;
}
}
assert(rng[i][0] <= v && v <= rng[i][1]);
idx += base*(v - rng[i][0]);
base *= len[i];
}
if (used[idx]) return dp[idx];
used[idx] = true;
auto r = pred(args);
dp[idx] = r;
return r;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/modint.d */
// module dcomp.modint;
// import dcomp.numeric.primitive;
struct ModInt(uint MD) if (MD < int.max) {
import std.conv : to;
uint v;
this(int v) {this(long(v));}
this(long v) {this.v = (v%MD+MD)%MD;}
static auto normS(uint x) {return (x<MD)?x:x-MD;}
static auto make(uint x) {ModInt m; m.v = x; return m;}
auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}
auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}
auto opBinary(string op:"*")(ModInt r) const {return make((long(v)*r.v%MD).to!uint);}
auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);}
auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}
static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);}
string toString() {return v.to!string;}
}
struct DModInt(string name) {
import std.conv : to;
static uint MD;
uint v;
this(int v) {this(long(v));}
this(long v) {this.v = ((v%MD+MD)%MD).to!uint;}
static auto normS(uint x) {return (x<MD)?x:x-MD;}
static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;}
auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));}
auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));}
auto opBinary(string op:"*")(DModInt r) const {return make((long(v)*r.v%MD).to!uint);}
auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);}
auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");}
static DModInt inv(DModInt x) {
return DModInt(extGcd!int(x.v, MD)[0]);
}
string toString() {return v.to!string;}
}
template isModInt(T) {
const isModInt =
is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s);
}
T[] factTable(T)(size_t length) if (isModInt!T) {
import std.range : take, recurrence;
import std.array : array;
return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array;
}
T[] invFactTable(T)(size_t length) if (isModInt!T) {
import std.algorithm : map, reduce;
import std.range : take, recurrence, iota;
import std.array : array;
auto res = new T[length];
res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b";
foreach_reverse (i, v; res[0..$-1]) {
res[i] = res[i+1] * T(i+1);
}
return res;
}
T[] invTable(T)(size_t length) if (isModInt!T) {
auto f = factTable!T(length);
auto invf = invFactTable!T(length);
auto res = new T[length];
foreach (i; 1..length) {
res[i] = invf[i] * f[i-1];
}
return res;
}
|
D
|
void main()
{
long[] tmp = rdRow;
long h = tmp[0], w = tmp[1], a = tmp[2], b = tmp[3];
foreach (i; 0 .. h)
{
foreach (j; 0 .. w)
{
((i < b) ^ (j < a)).to!long.write;
}
writeln;
}
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int a, b;
void main() {
scan(a, b);
a = min(a, b);
long ans = 1L;
foreach (i ; 1 .. a + 1) {
ans *= i;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
string[] input = readln.split;
int N = input[0].to!int;
int Q = input[1].to!int;
string ans = "kogakubu10gokan";
foreach(i; 0..N) {
input = readln.split;
if (input[0].to!int <= Q) ans = input[1];
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
import std.format;
void main()
{
auto abbcca = readln.split.to!(int[]), ab = abbcca[0], bc = abbcca[1], ca = abbcca[2];
writeln(ab * bc / 2);
}
|
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() {
readint;
auto a = readints;
auto rates = new int[8];
int redOver = 0; // (>= 3200)
foreach (x; a) {
if (x < 3200) rates[x / 400]++;
else redOver++;
}
auto x = rates.count!(e => e > 0);
writeln(max(x, 1), " ", x + redOver);
}
|
D
|
import std.stdio, std.string, std.conv, std.array, std.algorithm;
void main()
{
auto a = readln.split.map!(to!int);
if(a[0]+a[1]==2||a[2]==1) writeln("Open");
else writeln("Close");
}
|
D
|
void main(){
long a;
long[2] bb;
scanf("%ld %ld.%ld", &a, &bb[0], &bb[1]);
long b = bb[0]*100.to!long + bb[1];
( a*b/100 ).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.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
int N;
int[] ary;
void main() {
while(true) {
N = readln.chomp.to!int;
if (N==0) break;
ary = readln.split.to!(int[]);
rec(0, 0).writeln;
}
}
int rec(int depth, int x) {
if (depth==N) return x.abs;
return min(rec(depth+1, x+ary[depth]), rec(depth+1, x-ary[depth]));
}
|
D
|
import std;
// 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 arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;}
void scan(TList...)(ref TList Args){auto line = readln.split;
foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}}
void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length);
static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}}
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, K;
scan(N, K);
auto b = new bool[](N);
foreach (_; 0 .. K)
{
long d = lread();
auto A = aryread();
foreach (a; A)
{
b[a - 1] = true;
}
}
b.map!"a?0:1"().sum().writeln();
}
|
D
|
import std.stdio, std.string, std.conv, std.array;
string mathed(char[] s, char[] t)
{
for (int i = cast(int)(s.length-t.length); i >= 0; i--) {
if (s[i] == t[0] || s[i] == '?') {
int ii = i;
bool flag = true;
for (int j = 0; j < t.length;) {
if (!(s[i] == t[j] || s[i] == '?')) {
flag = false;
break;
}
i++;
j++;
}
if (flag) {
for (int j = 0; j < t.length; j++) {
s[ii+j] = t[j];
}
return cast(string)s.replace("?", "a");
}
else {
i = ii;
}
}
}
return "UNRESTORABLE";
}
void main()
{
char[] s = readln.chomp.dup;
char[] t = readln.chomp.dup;
writeln(mathed(s, t));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int r, g;
scan(r);
scan(g);
auto ans = r + (g - r) * 2;
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;
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 abc = aryread();
// writeln(abc);
long cnt5;
long cnt7;
foreach (i; 0 .. (abc.length))
{
if (abc[i] == 5)
{
cnt5 += 1;
}
else if (abc[i] == 7)
{
cnt7 += 1;
}
}
if (cnt5 == 2 && cnt7 == 1)
{
writeln("YES");
}
else
{
writeln("NO");
}
}
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();
string t = readln().chomp();
int ans;
foreach(i; 0..s.length){
if(s[i] != t[i])ans++;
}
ans.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.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto s = readln.chomp;
int res;
auto prev = 'S';
foreach (e; s) {
if (prev != e) {
res++;
prev = e;
}
}
res.writeln;
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int s;
sc.scan(s);
int[] a = new int[10000];
a[0] = s;
int i = 1;
while (1) {
if (a[i - 1] % 2 == 0) {
a[i] = a[i - 1] / 2;
} else {
a[i] = (3 * a[i - 1]) + 1;
}
foreach_reverse (j; 0 .. i) {
if (a[i] == a[j]) {
writeln(i + 1);
return;
}
}
i++;
}
}
|
D
|
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()
{
int n = readln.chomp.to!int;
int a = readln.chomp.to!int;
writeln = n * n - a;
}
|
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 n, m; readV(n, m);
auto r = 1;
foreach (f; 1..m.nsqrt+1) {
if (m%f != 0) continue;
if (m/f >= n) r = max(r, f);
if (f >= n) r = max(r, m/f);
}
writeln(r);
}
pure T nsqrt(T)(T n)
{
import std.algorithm, std.conv, std.range, core.bitop;
if (n <= 1) return n;
T m = T(1) << (n.bsr/2+1);
return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
|
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 main() {
dchar[] s = readln.chomp.to!(dchar[]);
dchar[] t = readln.chomp.to!(dchar[]);
bool can() {
foreach (_; 0..s.length+1) {
bringToFront(s[0..1], s[1..$]);
if (s == t) return true;
}
return false;
}
writeln(can() ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.ascii;
import std.algorithm;
static import core.stdc.stdio;
long[300_000+2] a;
long[300_000+2] c;
int main()
{
int[][] parent = new int[][](300_000+2, 20);
int q = readInt!int;
a[0] = readInt!long;
c[0] = readInt!long;
parent[0][] = -1;
int getNthParent(int v, int n)
{
int i = 0;
while (n)
{
if (n & 1)
v = parent[v][i];
n >>= 1;
i++;
}
return v;
}
foreach(i; 1 .. q + 1)
{
int type = readInt!int;
if (type == 1)
{
int pi = readInt!int;
long ai = readInt!long;
long ci = readInt!long;
parent[i][0] = pi;
foreach(j; 1 .. 20)
{
if (parent[i][j - 1] > 0)
parent[i][j] = parent[parent[i][j - 1]][j - 1];
else parent[i][j] = -1;
}
a[i] = ai;
c[i] = ci;
}
else
{
int vi = readInt!int;
long wi = readInt!long;
int root = vi;
int no = 0;
long spent = 0;
long bought = 0;
foreach_reverse(j; 0 .. 20)
{
int possibleParent = parent[root][j];
if (possibleParent < 0) continue;
if (a[possibleParent] > 0)
{
root = possibleParent;
no += (long(1) << j);
}
}
debug writeln("for node ", vi, " root is ", root, " depth = ", no);
while (wi > 0 && no >= 0)
{
int p = getNthParent(vi, no);
debug writeln("Now is ", no, " ", p, " ap = ", a[p], " wi = ", wi);
long taken = min(wi, a[p]);
wi -= taken;
bought += taken;
spent += taken * c[p];
a[p] -= taken;
no--;
}
writeln(bought, " ", spent);
stdout.flush;
}
}
return 0;
}
/* INPUT ROUTINES */
int currChar;
static this()
{
currChar = core.stdc.stdio.getchar();
}
char topChar()
{
return cast(char) currChar;
}
void popChar()
{
currChar = core.stdc.stdio.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
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
auto sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
int[][] g = new int[][n];
int sa, sb;
foreach (i; 0..m) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
if (i == 0) {
sa = a; sb = b;
}
}
bool[] used = new bool[n];
used[sa] = used[sb] = true;
int[] pathl, pathr;
void dfsl(int p) {
pathl ~= p;
foreach (d; g[p]) {
if (used[d]) continue;
used[d] = true;
dfsl(d);
break;
}
}
void dfsr(int p) {
pathr ~= p;
foreach (d; g[p]) {
if (used[d]) continue;
used[p] = true;
dfsr(d);
break;
}
}
dfsl(sa);
dfsr(sb);
reverse(pathl);
auto path = pathl ~ pathr;
writeln(path.length);
writeln(path.map!(x => (x+1).to!string).join(" "));
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, false).make();
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (deq.length && deq.front == i-m) {
deq.removeFront();
}
if (deq.length) {
ndp[i] = dp[deq.front] + 1L * ph * a[i];
}
while (deq.length && dp[deq[deq.length-1]] <= dp[i]) {
deq.removeBack();
}
deq.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool hasNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (hasNull && !p) p = new Payload(); }
private void C() const {
version(assert) if (!p) throw new RangeError();
}
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() {
Deque que;
que.p = new Payload();
return que;
}
@property bool empty() const { return (!p || p.empty); }
@property size_t length() const { return (p ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
//todo optimize
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.stdio;
void main()
{
foreach(i; 1 .. 10)
{
foreach(j; 1 .. 10)
{
writeln(i, 'x', j, '=', i * j);
}
}
}
|
D
|
import std.stdio;
void main(){for(int i;i<81;i++){
writeln(i/9+1,"x",i%9+1,"=",(i/9+1)*(i%9+1));
}}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
void main(){
while(true){
auto s = readln();
if(stdin.eof()) break;
s = chomp(s);
char win = 'd';
for(int i=0;i<3;i++){
if(s[3*i] != 's' && s[3*i] == s[3*i+1] && s[3*i] == s[3*i+2]) win = s[3*i];
}
for(int i=0;i<3;i++){
if(s[i] != 's' && s[i] == s[i+3] && s[i+6] == s[i]) win = s[i];
}
if(s[4] != 's'){
if(s[0] == s[4] && s[0] == s[8]) win = s[0];
else if(s[2] == s[4] && s[2] == s[6]) win = s[2];
}
writeln(win);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
int calc(int k, int[] ws) {
bool match(int limit) {
int n = 0;
int sum = 0;
foreach (w; ws) {
if (sum + w <= limit) {
sum += w;
}
else {
sum = w;
n++;
if (n > k)
return false;
}
}
return n + 1 <= k;
}
int lo = ws.reduce!max;
int hi = 10 ^^ 9;
int ans = hi;
while (lo <= hi) {
int m = (lo + hi) / 2;
if (match(m)) {
ans = min(ans, m);
hi = m - 1;
}
else {
lo = m + 1;
}
}
return ans;
}
void main() {
auto nk = readints();
int n = nk[0], k = nk[1];
int[] ws;
for (int i = 0; i < n; i++) {
ws ~= readint();
}
writeln(calc(k, ws));
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
auto R = scanElem;
auto G = scanElem;
auto B = scanElem;
auto N = scanElem;
int c;
for(auto r = 0; r*R <= N; r++)
{
for(auto g = 0; r*R+g*G <= N; g++)
{
if((N-r*R-g*G)%B==0) c++;
}
}
writeln(c);
}
long gcd(long a, long b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio, std.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;
long k;
scan(n, k);
auto a = readln.split.to!(long[]);
long ans;
foreach (i ; 0 .. 50) {
if (k & (1L << i)) {
auto ks = k ^ (1L << i);
auto mask = (1L << i) - 1;
ks |= mask;
auto res = solve(n, a, ks);
debug {
writefln("%08b", k);
writefln("%08b", ks);
writefln("res: %d", res);
}
ans = max(ans, solve(n, a, ks));
}
}
ans = max(ans, solve(n, a, k));
writeln(ans);
}
long solve(int n, long[] a, long k) {
auto cnt = new int[](50);
foreach (i ; 0 .. 50) {
foreach (j ; 0 .. n) {
if (a[j] & (1L << i)) {
cnt[i]++;
}
}
}
long res;
foreach (i ; 0 .. 50) {
if (k & (1L << i)) {
res += (1L << i) * max(cnt[i], n - cnt[i]);
}
else {
res += (1L << i) * cnt[i];
}
}
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);
}
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto p = readln.chomp.split.to!(int[]);
int res;
auto m = p[0];
foreach (i; 0..n) {
if (m >= p[i]) {
res++;
}
m = min(m, p[i]);
}
res.writeln;
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const A = readInt();
int ans;
foreach (i; 0 .. 6) {
ans |= ((A >> i) & 1) << [4, 1, 3, 2, 0, 5][i];
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dunkelheit" version="1.0.1"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner, dkh.algorithm;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
string s;
sc.read(s);
int n = s.length.to!int;
long r = n;
long ans = 0;
foreach_reverse (l; 0..n) {
int k = 1;
while (l + 2 * k < r && (s[l + k] != s[l] || s[l + 2 * k] != s[l])) k++;
r = min(r, l + 2 * k);
ans += (n - r);
}
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Programs/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/Programs/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/Programs/dunkelheit/source/dkh/algorithm.d */
// module dkh.algorithm;
import std.traits : isFloatingPoint, isIntegral;
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = l + (r-l) / 2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
import std.range.primitives;
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "minimum: range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "maximum: range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() const {
return now.empty;
}
@property auto front() const {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
/* IMPORT /home/yosupo/Programs/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;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private 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) {
assert(succW());
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 : enforce;
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.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;
foreach (i; 0..N) {
foreach (j; i+1..N) {
if (A[i] > A[j]) {
ans ^= 1;
}
}
}
auto Q = readln.chomp.to!int;
while (Q--) {
auto lr = readln.split.map!(to!int);
auto d = lr[1] - lr[0] + 1;
ans ^= (d * (d - 1) / 2) % 2;
writeln(ans%2 ? "odd" : "even");
}
}
|
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 k = RD!int;
long x = n;
for (long i = 2; i*i <= n; ++i)
{
if (n % i == 0)
{
x = i;
break;
}
}
ans[ti] = n + x + 2 * (k-1);
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
long n, m;
rd(n, m);
auto mn = max(0, n - m * 2);
long mx = 0;
for (auto k = 0L; k <= n; k++) {
if (k * (k - 1) / 2 >= m) {
mx = max(mx, n - k);
}
}
writeln(mn, " ", mx);
}
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
|
// This file is a "Hello, world!" in D language by DMD for wandbox.
import std.stdio;
import std.bigint;
import std.conv;
import std.string;
import std.algorithm;
ulong dfs(int[] arr)
{
bool flag = true;
int prev = -1;
foreach (v; arr)
{
flag &= prev <= v;
prev = v;
}
if (flag)
{
return arr.length;
}
else
{
return max(arr[0..$/2].dfs, arr[$/2..$].dfs);
}
}
void main()
{
int n = readln.chomp.to!int;
int[] arr = readln.chomp.split.to!(int[]);
arr.dfs.writeln;
}
// DMD reference:
// https://dlang.org/dmd-linux.html
// D language references:
// https://dlang.org
// http://www.kmonos.net/alang/d/ ( Japanese p
|
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 long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto x = RD;
auto a = RDA;
bool ok = true;
foreach (i; 1..n)
{
if (a[i] < a[i-1])
{
ok = false;
}
}
if (ok) continue;
ok = true;
foreach (i; 0..n)
{
if (a[i] > x)
{
swap(a[i], x);
++ans[ti];
bool done = true;
foreach (j; i+1..n)
{
if (a[j] < a[j-1])
{
done = false;
break;
}
}
if (done) break;
}
else if (a[i] < a[i-1])
{
ok = false;
}
}
if (!ok)
ans[ti] = -1;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;
struct IO {
string[] tk;
string readString() {
while (tk.empty)
tk = readln.split;
auto tkt = tk.front;
tk.popFront;
return tkt;
}
int readInt() {
return readString.to!int;
}
double readDouble() {
return readString.to!double;
}
}
void main() {
IO cin;
int t = 1;
t = cin.readInt;
while (t--) {
int n = cin.readInt;
int k = cin.readInt;
if (k == 0) {
if (n % 2 == 0) writeln(0);
else writeln(1);
} else {
if (k > n) writeln(k - n);
else {
if (n % 2 == 0 && k % 2 == 1) writeln(1);
else if (n % 2 == 1 && k % 2 == 0) writeln(1);
else writeln(0);
}
}
}
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T[]readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
struct Queue(T){T[]e;auto enq(T t){e~=t;}auto enq(T[]ts){e~=ts;}T deq(){T tp=e[0];e=e.length>1?e[1..$]:[];return tp;}}
struct Stack(T){T[]e;auto push(T t){e~=t;}auto push(T[]ts){e~=ts;}T pop(){T tp=e[$-1];e=e.length>1?e[0..$-1]:[];return tp;}}
//END OF TEMPLATE
void main(){
long n;
n.scan;
iota(1,n+1).filter!(i=>i%3!=0&&i%5!=0).fold!"a+b".writeln;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.