code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
long ans = long.max;
// foreach (i; 1 .. (n + 1))
// {
// foreach (j; 1 .. (n + 1))
// {
// if (i * j == n)
// {
// ans = min(ans, ((i - 1) + (j - 1)));
// }
// }
// }
// writeln(ans);
foreach (i; 1 .. n)
{
if (n % i == 0)
{
ans = min(ans, ((i - 1) + (n / i) - 1));
// writeln(ans);
}
if (i > (n / i))
{
// writeln(i, n / i);
break;
}
}
writeln(ans);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
void main() {
int N = readln.chomp.to!(int);
int[] A = readln.chomp.split.to!(int[]);
if (N == 1) {
writeln(1);
return;
}
int ans;
for (int i = 1; i < N; ) {
i += max(plus(A, i), minus(A, i)) + 1;
ans++;
if (N == i) {
ans++;
}
}
writeln(ans);
}
int plus(int[] A, int index) {
int ret;
for (int i = index; i < A.length; i++) {
if (A[i - 1] <= A[i]) {
ret++;
}
else {
break;
}
}
return ret;
}
int minus(int[] A, int index) {
int ret;
for (int i = index; i < A.length; i++) {
if (A[i - 1] >= A[i]) {
ret++;
}
else {
break;
}
}
return ret;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
void main()
{
long h, w;
scan(h, w);
auto field = new string[](h);
foreach (ref e; field)
e = sread();
auto box = new long[][](h, w);
auto x_move = [-1, 0, 1];
auto y_move = [-1, 0, 1];
foreach (i; iota(h))
{
foreach (j; iota(w))
{
foreach (k; y_move)
{
if (i + k < 0 || i + k >= h)
continue;
foreach (l; x_move)
{
if (j + l < 0 || j + l >= w)
continue;
// writefln("%s %s, %s %s", i, j, k, l);
box[i][j] += (field[i + k][j + l] == '#').to!long;
}
}
}
}
foreach (i, e; field)
{
foreach (j, f; e)
{
if (f == '#')
write("#");
else
write(box[i][j]);
}
writeln();
}
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xs = readints();
int a = xs[0], b = xs[1], c = xs[2], d = xs[3];
int ans = 0;
for (int i = 0; i <= 100; i++) {
if (a <= i && i <= b && c <= i && i <= d) {
ans++;
}
}
writeln(max(0, ans - 1));
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int n; scan(n);
writeln(n ^^ 3);
}
|
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 H = RD;
auto W = RD;
auto K = RD;
auto c = new long[][](H, W);
foreach (i; 0..H)
{
auto s = RD!string;
foreach (j; 0..W)
{
if (s[j] == '#')
c[i][j] = 1;
}
}
long ans;
foreach (i; 0..2^^H)
{
foreach (j; 0..2^^W)
{
long cnt;
foreach (ii; 0..H)
{
auto bit1 = 1L << ii;
if (i & bit1) continue;
foreach (jj; 0..W)
{
auto bit2 = 1L << jj;
if (j & bit2) continue;
cnt += c[ii][jj];
}
}
if (cnt == K)
++ans;
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const N = readln.chomp.to!long;
auto as = readln.split.to!(long[]);
auto res = long.max;
while (true) {
auto candidates = as.filter!(a => a > 0);
if (candidates.empty) break;
const m = candidates.reduce!min;
foreach (ref a; as)
if (a != m)
a %= m;
res = min(res, m);
if (candidates.all!(a => a == m)) break;
}
writeln(res);
}
|
D
|
import std.stdio, std.algorithm, std.range, std.conv, std.string, std.math;
import core.stdc.stdio, core.bitop;
// foreach, foreach_reverse, writeln
void main() {
int n, MOD;
scanf("%d%d", &n, &MOD);
long[][] c = new long[][](n+1,n+2);
c[0][0] = 1;
foreach (i; 0..n) {
foreach (j; 0..i+1) {
(c[i+1][j] += c[i][j]) %= MOD;
(c[i+1][j+1] += c[i][j]) %= MOD;
}
}
long[][] dp = new long[][](n+1,n+1);
dp[0][0] = 1;
foreach (i; 0..n) {
foreach (j; 0..n+1) {
(dp[i+1][j] += dp[i][j]) %= MOD;
(dp[i+1][j] += dp[i][j]*j) %= MOD;
if (j != n) (dp[i+1][j+1] += dp[i][j]) %= MOD;
}
}
//long pow(long x, long times, long MOD) {
// long ret = 1;
// while (times) {
// if (times&1) (ret *= x) %= MOD;
// times >>= 1;
// (x *= x) %= MOD;
// }
// return ret;
//}
//long powpow(long x, long y, long MOD) {
// foreach (i; 0..y) {
// (x *= x) %= MOD;
// }
// return x;
//}
long[] p2 = new long[n+1];
p2[0] = 1;
foreach (i; 0..n) p2[i+1] = p2[i]*2%MOD;
long[] pp2 = new long[n+1];
pp2[0] = 2;
foreach (i; 0..n) pp2[i+1] = pp2[i]*pp2[i]%MOD;
long ans = 0;
foreach (i; 0..n+1) {
long cur = 0;
long two = 1;
foreach (j; 0..i+1) {
long a = dp[i][j]*c[n][i]%MOD;
//long x = powpow(2,n-i,MOD)*pow(pow(2,n-i,MOD),j,MOD)%MOD;
long x = pp2[n-i]*two%MOD;
(cur += a*x) %= MOD;
(two *= p2[n-i]) %= MOD;
}
if (i%2 == 0) {
ans += cur;
} else {
ans += MOD-cur;
}
ans %= MOD;
}
writeln(ans);
}
|
D
|
void main() {
int k = readln.chomp.to!int;
writeln(k / 2 * ((k + 1) / 2));
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.container;
import std.typecons;
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
alias Point = Tuple!(int, "x", int, "y", int, "h");
Point[101] points;
Point point_;
foreach (i; 0 .. N) {
auto data = readln.split;
Point p = Point(data[0].to!int, data[1].to!int, data[2].to!int);
points[i] = p;
if (p.h > 0) point_ = p;
}
if (N == 1) { auto p = points[0]; writeln(p.x, " ", p.y, " ", p.h); return; }
foreach (x; 0 .. 101) foreach (y; 0 .. 101) {
auto H = point_.h + (point_.x - x).abs + (point_.y - y).abs;
bool flag = true;
foreach (i; 0 .. N) {
auto p = points[i];
if ( max(H - abs(p.x - x) - abs(p.y - y), 0) != p.h ) { flag = false; break; }
}
if (flag) { writeln( x, " ", y, " ", H ); return; }
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.math;
int n, d; rd(n, d);
auto t=new long[](n);
auto f=new long[](n);
foreach(i; 0..n) rd(t[i], f[i]);
long sm=0, pos=1, tm=0, m=0;
foreach(i; 0..n){
if(tm+(pos-1)+(f[i]-1)<=t[i]){
sm+=m*(pos-1);
m=1;
}else if(tm+abs(pos-f[i])<=t[i] && m<d){
// sm+=m*abs(pos-f[i]);
sm+=m*abs(t[i]-tm);
m++;
}else{
writeln(-1);
return;
}
pos=f[i]; tm=t[i];
}
writeln(sm+m*(pos-1));
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
import std.string;
import std.algorithm;
void main() {
string S;
S = chomp(readln());
string res = "";
if(S[0] == '9' || S[1] == '9') {
res = "Yes";
}
else{
res = "No";
}
writeln(res);
}
|
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()
{
immutable mod = 1_000_000_000 + 7;
auto n = readln.chomp.to!int;
auto a = readln.chomp.split.to!(int[]);
auto r = new int[](n/2+1);
auto t = n % 2;
foreach (e; a) {
if (t == e % 2) continue;
r[(n/2) - (e/2) - ((n+1)%2)]++;
}
long res = 1;
foreach (i; 0..n/2 + n%2) {
res *= r[i];
res %= mod;
r[i]--;
}
foreach_reverse (i; 0..n/2) {
res *= r[i];
res %= mod;
}
res.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
bool isPalindrome(string s) {
int n = cast(int)s.length;
for (int i = 0; i < n/2; i++) {
if (s[i] != s[$-i-1]) return false;
}
return true;
}
bool calc(string s) {
int n = cast(int)s.length;
if (!isPalindrome(s)) return false;
if (!isPalindrome(s[0..(n-1)/2])) return false;
if (!isPalindrome(s[(n+3)/2-1..$])) return false;
return true;
}
void main() {
string s; scan(s);
writeln(calc(s) ? "Yes" : "No");
}
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
|
void main() {
int n = readln.chomp.to!int;
int[] a = new int[n];
foreach (i; 0 .. n) {
a[i] = readln.chomp.to!int;
}
bool[] light = new bool[n];
light[0] = true;
int now = a[0];
int cnt;
while (!light[1]) {
if (light[now-1]) {
cnt = -1;
break;
}
light[now-1] = true;
now = a[now-1];
++cnt;
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
unittest
{
assert( [ "6", "ooxoox" ].parse.expand.solve == "SSSWWS" );
assert( [ "3", "oox" ].parse.expand.solve == "-1" );
assert( [ "10", "oxooxoxoox" ].parse.expand.solve == "SSWWSSSWWS" );
}
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
input.popFront;
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
// 最初の二匹を仮定して整合性が取れるか判定
foreach( a; [ "SS", "SW", "WW", "WS" ] )
{
auto ans = solve2( s, a );
if( ans ) return ans.idup;
}
return "-1";
}
auto solve2( string s, string a )
{
s ~= s[ 0 ];
auto anss = new char[ s.length + 1 ];
anss[ 0 .. 2 ] = a;
for( auto i = 1L; i < s.length; i++ )
{
final switch( anss[ i - 1 .. i + 1 ] )
{
case "SS": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'S' : 'W'; break;
case "SW": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'W' : 'S'; break;
case "WW": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'S' : 'W'; break;
case "WS": anss[ i + 1 ] = ( s[ i ] == 'o' ) ? 'W' : 'S'; break;
}
}
return ( anss[ 0 .. 2 ] == anss[ $ - 2 .. $ ] ) ? anss[ 0 .. $ - 2 ] : null;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto a=readln.chomp.to!int;
auto b=readln.chomp.to!int;
auto h=readln.chomp.to!int;
writeln((a+b)*h/2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
auto P = 10L^^9+7;
long[100][5][5][5] MEMO;
void main()
{
auto N = readln.chomp.to!int;
foreach (ref xx; MEMO) foreach (ref yy; xx) foreach (ref zz; yy) foreach (ref memo; zz) memo = -1;
long solve(int i, int x, int y, int z) {
if (i == N) return 1;
if (MEMO[z][y][x][i] != -1) return MEMO[z][y][x][i];
long r = (solve(i+1, y, z, 1) + solve(i+1, y, z, 4)) % P;
if ( !(
(y == 1 && z == 3) ||
(x == 1 && z == 3) ||
(y == 3 && z == 1) ||
(x == 1 && y == 3)) ) r = (r + solve(i+1, y, z, 2)) % P;
if ( !(y == 1 && z == 2) ) r = (r + solve(i+1, y, z, 3)) % P;
return MEMO[z][y][x][i] = r;
}
writeln(solve(0, 0, 0, 0));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void get(Args...)(ref Args args)
{
import std.traits, std.meta, std.typecons;
static if (Args.length == 1) {
alias Arg = Args[0];
static if (isArray!Arg) {
static if (isSomeChar!(ElementType!Arg)) {
args[0] = readln.chomp.to!Arg;
} else {
args[0] = readln.split.to!Arg;
}
} else static if (isTuple!Arg) {
auto input = readln.split;
static foreach (i; 0..Fields!Arg.length) {
args[0][i] = input[i].to!(Fields!Arg[i]);
}
} else {
args[0] = readln.chomp.to!Arg;
}
} else {
auto input = readln.split;
assert(input.length == Args.length);
static foreach (i; 0..Args.length) {
args[i] = input[i].to!(Args[i]);
}
}
}
void get_lines(Args...)(size_t N, ref Args args)
{
import std.traits, std.range;
static foreach (i; 0..Args.length) {
static assert(isArray!(Args[i]));
args[i].length = N;
}
foreach (i; 0..N) {
static if (Args.length == 1) {
get(args[0][i]);
} else {
auto input = readln.split;
static foreach (j; 0..Args.length) {
args[j][i] = input[j].to!(ElementType!(Args[j]));
}
}
}
}
void main()
{
int T; get(T);
while (T--) {
char[] s; get(s);
if (s.length % 2 == 1 || s[0] == ')' || s[$-1] == '(') {
writeln("NO");
} else {
writeln("YES");
}
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
import std.numeric;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n; readV(n);
long[] t; readC(n, t);
writeln(t.reduce!((a, b) => a/gcd(a,b)*b));
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
T Read(T)() { return readln.chomp.to!(T); }
T[] Reads(T)() { return readln.split.to!(T[]); }
alias read = Read!(int);
alias reads = Reads!(int);
void main() {
int n = read();
int[] a = reads();
int ans = int.max;
foreach (i; 1..n) {
int d = abs(a[0..i].sum - a[i..$].sum);
ans = min(ans, d);
}
writeln(ans);
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!int;
auto M = scan!int;
auto Q = scan!int;
auto ABCD = Q.iota.map!(x => scan!int(4)).array;
long calcScore(int[] nm) {
long s;
foreach(abcd; ABCD) {
auto a = abcd[0];
auto b = abcd[1];
auto c = abcd[2];
auto d = abcd[3];
if (nm[b] - nm[a] == c) s += d;
}
nm.deb;
return s;
}
long solve() {
int[] numbers = new int[N + 1];
numbers[] = 1;
numbers[$-1] = 0;
long max_score;
while(true) {
foreach_reverse(right; 1..N+1) {
if (numbers[right] == M) continue;
numbers[right..$] = numbers[right] + 1;
break;
}
long score = calcScore(numbers);
if (score > max_score) max_score = score;
if (numbers[1] == M) break;
}
return max_score;
}
writeln(solve());
}
// ----------------------------------------------
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
|
void main() {
int n = readln.chomp.to!int;
int[] a1 = readln.split.to!(int[]);
int[] a2 = readln.split.to!(int[]);
int candy;
foreach (i; 0 .. n) {
candy = max(candy, a1[0..i+1].sum+a2[i..$].sum);
}
candy.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
auto n = readln.chomp.to!int;
foreach (_; 0..n) {
bool[int] f;
auto x = readln.chomp.to!int;
int cnt;
while (x > 9) {
if (f.get(x, 0)) {
cnt = -1;
break;
}
f[x] = 1;
int next;
auto str = x.to!string;
foreach (i; 1..str.length) {
foreach (j; i..str.length) {
next = max(next, str[0..i].to!int * str[i..$].to!int);
}
}
x = next;
cnt++;
}
cnt.writeln;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
long P = 10^^9+7;
long[10^^5*2] F, RF;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
long inv(long x)
{
return pow(x, P-2);
}
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
Tuple!(N, N)[] prime_division(N)(N n)
{
Tuple!(N, N)[] res;
foreach (N i; 2..10^^6+1) {
if (n%i == 0) {
N cnt;
while (n%i == 0) {
++cnt;
n /= i;
}
res ~= tuple(i, cnt);
}
}
if (n != cast(N)1) res ~= tuple(n, cast(N)1);
return res;
}
void main()
{
init();
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
long r = 1;
foreach (p; prime_division(M)) {
(r *= comb(N+p[1]-1, N-1)) %= P;
}
writeln(r);
}
|
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 K = RD;
auto N = RD;
auto A = RDA;
auto B = new long[](N);
B[0] = A[0]+K - A[$-1];
foreach (i; 1..N)
{
B[i] = A[i] - A[i-1];
}
auto pos = B.MIN_POS!"a > b"();
auto ans = K - B[pos];
writeln(ans);
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, core.stdc.string;
void main() {
auto s = readln.split.map!(to!long);
auto A = s[0];
auto B = s[1];
long[] X, Y;
bool[long] st;
long aa = A, bb = B;
for (long a = 1; a * a <= aa; a++) {
if (aa % a == 0) {
X ~= a;
while (a != 1 && aa % a == 0)
aa /= a;
}
}
if (aa > 1) X ~= aa;
for (long a = 1; a * a <= bb; a++) {
if (bb % a == 0) {
Y ~= a;
while (a != 1 && bb % a == 0)
bb /= a;
}
}
if (bb > 1) Y ~= bb;
foreach (a; X) if (A % a == 0 && B % a == 0) st[a] = true;
foreach (a; Y) if (A % a == 0 && B % a == 0) st[a] = true;
auto C = st.keys.array;
int M = C.length.to!int;
long ans = 1;
foreach (mask; 0..(1<<M)) {
long[] lst;
foreach (i; 0..M) {
if (mask & (1 << i)) lst ~= C[i];
}
bool ok = true;
foreach (i; 0..M) {
foreach (j; i+1..M) {
if (gcd(C[i], C[j]) != 1) {
ok = false;
break;
}
}
if (!ok) break;
}
if (ok) ans = max(ans, mask.popcnt);
}
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;
bool[][] G;
G.length = N;
foreach (i; 0..N) {
G[i].length = N;
foreach (j, x; readln.chomp) {
if (x == '1') G[i][j] = true;
}
}
auto S = new int[](N);
bool check(int i, int p, int s) {
if (S[i]) return S[i] == s;
S[i] = s;
foreach (j, n; G[i]) if (n && i != j && j != p) {
if (check(j.to!int, i, s%2+1) == false) return false;
}
return true;
}
if (check(0, -1, 1) == false) {
writeln(-1);
return;
}
auto M = new int[][](N, N);
foreach (i; 0..N) {
foreach (j, c; G[i]) {
if (i == j) continue;
if (c) {
M[i][j] = 1;
M[j][i] = 1;
} else {
M[i][j] = int.max / 3;
M[j][i] = int.max / 3;
}
}
}
foreach (k; 0..N) {
foreach (i; 0..N) {
foreach (j; 0..N) {
if (M[i][j] > M[i][k] + M[k][j]) {
M[i][j] = M[i][k] + M[k][j];
}
}
}
}
int r;
foreach (i; 0..N) foreach (j; 0..N) r = max(r, M[i][j]);
writeln(r+1);
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array,
std.functional, std.container, std.typecons;
void main() {
auto input = readln.split.to!(int[]);
auto N = input[0], D = input[1];
auto r = 2*D + 1;
((N + r -1) / r).writeln;
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
3
())
---+/
/+---test
6
)))())
---+/
/+---test
8
))))((((
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
const S = readln.chomp;
long dep, lcnt;
foreach (c; S) {
if (c == '(') ++dep;
if (c == ')') {
if (dep > 0) {
--dep;
} else {
++lcnt;
}
}
}
(cycle("(").take(lcnt).array.to!string~S~cycle(")").take(dep).array.to!string).writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
void main()
{
writeln("2018" ~ sread()[4 .. $]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
import std.math;
import std.range;
void main() {
auto tmp = readln.split.to!(int[]);
if (tmp[0] == tmp[1]) writeln(tmp[2]);
else if (tmp[1] == tmp[2]) writeln(tmp[0]);
else writeln(tmp[1]);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib, core.bitop;
void main(){
auto S = scanString;
string NS;
bool B;
for(int i = 0;i < S.length-1;i++)
{
if(S[i] == 'B' && S[i+1] == 'C'){
NS ~= 'D';
i++;
}else{
NS ~= S[i];
}
}
long aCount;
long res;
foreach(c; NS)
{
if(c=='A')
{
aCount++;
}else if(c=='D')
{
res+=aCount;
}else{
aCount=0;
}
}
writeln(res);
}
long gcd(long a, long b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
string scanString()
{
return readln.strip;
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(T[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
//素因数分解
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
//約数をすべて列挙
long[] divisors(long n)
{
long[] list;
void func(Factor[] fs, long n)
{
if(fs.empty){
list ~= n;
return;
}
foreach(c; 0..fs[0].c+1)
{
func(fs[1..$], n * (fs[0].n ^^ c));
}
}
func(factors(n), 1);
sort(list);
return list;
}
//nまでの素数のリスト
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
//素数判定
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!int;
auto as = readln.split.to!(int[]);
auto a_max = as.maxElement;
if (as.all!(a => a == a_max) && a_max+1 == N) {
writeln("Yes");
return;
}
int c = a_max, n = N;
foreach (a; as) {
if (a == a_max-1) {
--c;
--n;
} else if (a != a_max) {
writeln("No");
return;
}
}
writeln(c < 1 || c*2 > n ? "No" : "Yes");
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long a = read.to!long;
long b = read.to!long;
long c = read.to!long;
long k = read.to!long;
long ans;
if(k % 2) ans = b - a;
else ans = a - b;
if(ans > 1_000_000_000_000_000_000) writeln("Unfair");
else ans.writeln;
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc135/tasks/abc135_c
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
long n = readln.chomp.to!long;
long[] as = readln.split.map!(to!long).array;
long[] bs = readln.split.map!(to!long).array;
long monsters = 0L;
for(long i = n-1; i >= 0L; i--) {
long bugs = min(bs[i], as[i+1]);
//writefln("mate %d bichos", bugs);
monsters += bugs;
as[i+1] -= bugs;
bs[i] -= bugs;
bugs = min(as[i],bs[i]);
//writefln("mate %d bichos", bugs);
monsters += bugs;
bs[i] -= bugs;
as[i] -= bugs;
//as.writeln;
//bs.writeln;
}
monsters.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1L << 50;
enum mod = 10L^^9 + 7;
void main() {
int n;
scan(n);
auto adj = new int[][](n, 0);
foreach (i ; 0 .. n-1) {
int x, y;
scan(x, y);
x--, y--;
adj[x] ~= y;
adj[y] ~= x;
}
auto dp = new long[][](n, 2);
fillAll(dp, -1);
long dfs(int v, int p, int col) {
if (dp[v][col] != -1) {
return dp[v][col];
}
dp[v][col] = 1;
foreach (u ; adj[v]) {
if (u == p) continue;
long t = 0;
t += dfs(u, v, 0);
t %= mod;
if (col == 0) {
t += dfs(u, v, 1);
t %= mod;
}
dp[v][col] *= t;
dp[v][col] %= mod;
}
return dp[v][col];
}
auto ans = dfs(0, -1, 0) + dfs(0, -1, 1);
ans %= mod;
debug {
writefln("%(%s\n%)", dp);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.algorithm, std.conv, std.range, std.array;
void main(){
auto input = readln.chomp.split.map!(to!int).array;
chain(iota(input[0],min(input[0]+input[2],(input[1]+input[0]+1)/2)),iota(max(input[1]-input[2]+1,(input[0]+input[1])/2),input[1]+1)).uniq.each!writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.bigint;
import std.math;
void main()
{
while (1) {
auto x = readln.chomp.split.map!(to!int);
if (x[0] == 0) break;
foreach (i; 0..x[0]) {
foreach (j; 0..x[1]) {
if ((i+j)%2) write(".");
else write("#");
}
writeln("");
}
writeln("");
}
}
|
D
|
void main()
{
long n = rdElem;
long[][] lists = new long[][](10, 10);
foreach (i; 1 .. n+1)
{
long f = i;
long b = f % 10;
while (f > 9) f /= 10;
++lists[f][b];
}
long result;
foreach (i; 0 .. 10)
{
foreach (j; 0 .. 10)
{
result += lists[i][j] * lists[j][i];
}
}
result.writeln;
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;
void test () {
auto s = readln.strip;
int[26] c;
foreach (i; s) {
++c[i.to!int - 97];
}
auto x = iota (0, 26, 2), y = iota (1, 26, 2);
auto r1 = x.filter!(a => c[a] > 0);
auto r2 = y.filter!(a => c[a] > 0);
void p (int k) {
write (replicate ([(97+ k).to!char], c[k]));
}
void o(T) (T r) {
foreach (k; r) {
p (k);
}
writeln;
}
if (r1.empty) {
o (r2);
return;
}
if (r2.empty) {
o (r1);
return;
}
foreach (i; r1) {
foreach (j; r2) {
if (abs (i - j) > 1) {
foreach (k; r1) {
if (k != i) p (k);
}
p (i);
p (j);
foreach (k; r2) {
if (k != j) p (k);
}
writeln;
return;
}
}
}
writeln ("No answer");
}
void main() {
immutable nt = readln.strip.to!int;
foreach (tid; 0 .. nt) {
test ();
}
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
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];
}
void readlnTo(T...)(ref T t) {
auto s = readln().split();
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
class UnionFindTree {
public:
this(size_t n) {
parent = iota(n).array();
rank = new size_t[](n);
}
size_t find(size_t n) {
if(n == parent[n]) {
return n;
} else {
parent[n] = find(parent[n]);
return parent[n];
}
}
void unite(size_t n, size_t m) {
auto p1 = find(n);
auto p2 = find(m);
if(n == m) {
return;
}
if(rank[p1] < rank[p2]) {
swap(p1, p2);
}
if(rank[p1] == rank[p2]) {
++rank[p1];
}
parent[p2] = p1;
}
private:
size_t[] parent;
size_t[] rank;
}
unittest {
auto uft = new UnionFindTree(10);
foreach(i; iota(10)) {
assert(uft.find(i) == i);
}
foreach(i; iota(10).filter!(a => a%3 == 0).drop(1)) {
uft.unite(i, i-3);
}
foreach(i; iota(10).filter!(a => a%3 == 0)) {
assert(uft.find(0) == uft.find(i));
}
foreach(i; iota(10).filter!(a => a%3 != 0)) {
assert(uft.find(0) != uft.find(i));
}
}
//内部では1-based, 使うときは0-based
class BinaryIndexedTree(T, alias F = "a + b") if(is(typeof(binaryFun!F))) {
alias f = binaryFun!F;
public:
this(in size_t n, in T T0 = T.init) {
t.length = n+1;
t[] = T0;
}
void add(in size_t idx, in T v)
in {
assert(idx < t.length-1);
}
body {
one_based_add(idx+1, v);
}
T accumulate(size_t idx) {
return one_based_accumulate(idx+1);
}
private:
T[] t;
//1-based
void one_based_add(size_t idx, in ref T v) {
if(idx >= t.length) {
return;
}
t[idx] = f(t[idx], v);
one_based_add(idx + (idx & (-idx)), v);
}
T one_based_accumulate(size_t idx) {
if(idx == 0) {
return t[0];
} else {
return f(one_based_accumulate(idx - (idx & (-idx))), t[idx]);
}
}
}
unittest {
auto bit = new BinaryIndexedTree!(long)(10);
foreach(i; iota(10)) {
assert(bit.accumulate(i) == 0);
}
bit.add(5, 3);
foreach(i; iota(5)) {
assert(bit.accumulate(i) == 0);
}
foreach(i; iota(5, 10)) {
assert(bit.accumulate(i) == 3);
}
bit.add(7, 2);
foreach(i; iota(5)) {
assert(bit.accumulate(i) == 0);
}
foreach(i; iota(5, 7)) {
assert(bit.accumulate(i) == 3);
}
foreach(i; iota(7, 10)) {
assert(bit.accumulate(i) == 5);
}
}
unittest {
auto bit = new BinaryIndexedTree!(long, "a*b")(10, 1);
foreach(i; iota(10)) {
assert(bit.accumulate(i) == 1);
}
bit.add(5, 3);
foreach(i; iota(5)) {
assert(bit.accumulate(i) == 1);
}
foreach(i; iota(5, 10)) {
assert(bit.accumulate(i) == 3);
}
bit.add(7, 2);
foreach(i; iota(5)) {
assert(bit.accumulate(i) == 1);
}
foreach(i; iota(5, 7)) {
assert(bit.accumulate(i) == 3);
}
foreach(i; iota(7, 10)) {
assert(bit.accumulate(i) == 6);
}
}
//最大流、O(|V||E|^2)
template Flow_Dinic() {
class Edge {
public:
this(long to, long capacity) {
this.to = to;
this.capacity = capacity;
}
long to;
long capacity;
Edge reverse;
}
//gを残余グラフにする
void residualise(Edge[][] g) {
foreach(u; iota(g.length)) {
foreach(e; g[u]) {
if(e.capacity != 0) {
e.reverse = new Edge(u, 0);
e.reverse.reverse = e;
g[e.to] ~= e.reverse;
}
}
}
}
long INF = long.max / 2;
//gは残余グラフ
//gは計算の結果書き換わる
long solve(Edge[][] g, long s, long t) {
long ret;
while(true) {
auto level = new long[](g.length);
level[] = INF;
auto q = DList!long(s);
level[s] = 0;
//level graph
auto lg = new Edge[][](g.length, 0);
while(!q.empty()) {
auto u = q.front();
q.removeFront();
if(u == t) {
break;
}
foreach(e; g[u]) {
if(e.capacity > 0 && level[e.to] >= level[u] + 1) {
level[e.to] = level[u] + 1;
q.insertBack(e.to);
lg[u] ~= e;
}
}
}
if(level[t] == INF) {
break;
}
auto iter = new long[](g.length);
long dfs(long u, long c_min) {
if(u == t) {
return c_min;
}
foreach(i; iota(iter[u], lg[u].length)) {
iter[u] = i;
long f = dfs(lg[u][i].to, min(c_min, lg[u][i].capacity));
if(f > 0) {
lg[u][i].capacity -= f;
lg[u][i].reverse.capacity += f;
return f;
}
}
return 0;
}
while(true) {
long f = dfs(s, INF);
if(f == 0) {
break;
}
ret += f;
}
}
return ret;
}
}
const real eps = 1e-10;
const long p = 1_000_000_000 + 7;
void main(){
long n, a, b;
readlnTo(n, a, b);
writeln(abs(a-b) % 2 == 0 ? "Alice": "Borys");
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int A, B, M;
scan(A, B, M);
auto a = readln.split.to!(int[]);
auto b = readln.split.to!(int[]);
int ans = a.reduce!min + b.reduce!min;
foreach (i ; 0 .. M) {
int xi, yi, ci;
scan(xi, yi, ci);
xi--, yi--;
chmin(ans, a[xi] + b[yi] - ci);
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (i; 0..t)
{
auto n = RD!int;
auto x = RD!int;
auto a = RD!int-1;
auto b = RD!int-1;
auto aa = min(a, b);
auto bb = max(a, b);
auto cnt1 = min(aa, x);
x -= cnt1;
aa -= cnt1;
auto cnt2 = min(n-bb-1, x);
bb += cnt2;
ans[i] = bb - aa;
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
/+ dub.sdl:
name "C"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.range, std.algorithm, std.conv;
// import dcomp.scanner;
// import dcomp.algorithm;
int main() {
auto sc = new Scanner(stdin);
int n, m;
sc.read(n, m);
int[][] g = new int[][](n);
foreach (i; 0..m) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b;
g[b] ~= a;
}
bool[] used = new bool[n];
void visit(int p) {
if (used[p]) return;
used[p] = true;
foreach (d; g[p]) {
visit(d);
}
}
int[] col = new int[n]; col[] = -1;
bool dfs(int p, int c) {
if (col[p] != -1 && col[p] != c) return false;
if (col[p] != -1) return true;
col[p] = c;
foreach (d; g[p]) {
if (!dfs(d, 1-c)) return false;
}
return true;
}
long zc, oc, tc;
foreach (i; 0..n) {
if (g[i].length == 0) {
zc++;
continue;
}
if (used[i]) continue;
visit(i);
if (dfs(i, 0)) {
tc++;
} else {
oc++;
}
}
// writeln(zc, oc, tc);
long ans = 0;
ans += zc * n + n * zc - zc*zc;
ans += (oc + tc) * (oc + tc) + tc * tc;
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
string[] buf;
private bool succ() {
while (!buf.length) {
if (f.eof) return false;
buf = f.readln.split;
}
return true;
}
private bool readSingle(T)(ref T x) {
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
x = buf.front;
buf.popFront;
} else {
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf.map!(to!E).array;
buf.length = 0;
}
} else {
x = buf.front.to!T;
buf.popFront;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
//[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
unittest {
assert(minimum([2, 1, 3]) == 1);
assert(minimum!"a > b"([2, 1, 3]) == 3);
assert(minimum([2, 1, 3], -1) == -1);
assert(minimum!"a > b"([2, 1, 3], 100) == 100);
assert(maximum([2, 1, 3]) == 3);
assert(maximum!"a > b"([2, 1, 3]) == 1);
assert(maximum([2, 1, 3], 100) == 100);
assert(maximum!"a > b"([2, 1, 3], -1) == -1);
}
bool[ElementType!Range] toMap(Range)(Range r) {
import std.algorithm : each;
bool[ElementType!Range] res;
r.each!(a => res[a] = true);
return res;
}
|
D
|
import std.stdio, std.conv, std.array,std.string;
void main()
{
string[] input=readln.split(",");
writeln(input[0]~" "~input[1]~" "~input[2]);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto h = readln.chomp.to!int;
auto w = readln.chomp.to!int;
writeln((n-h+1)*(n-w+1));
}
|
D
|
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto s=readln().chomp().split().map!(to!int);
int a,b,c,d;
a=s[0];
b=s[1];
c=s[2];
d=s[3];
if(a+b>c+d) writeln("Left");
else if(a+b==c+d) writeln("Balanced");
else writeln("Right");
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto ma = 10^^5;
auto p = primes(10^^5);
auto b = new bool[](ma+1);
foreach (pi; p) b[pi] = true;
auto c = new bool[](ma+1);
foreach (i; 0..ma+1) c[i] = (i&1) && b[i] && b[(i+1)/2];
auto d = new int[](ma+1);
foreach (i; 0..ma+1) d[i] = d[i-1]+c[i];
auto q = readln.chomp.to!int;
foreach (_; 0..q) {
auto rd = readln.splitter;
auto l = rd.front.to!int-1; rd.popFront();
auto r = rd.front.to!int;
writeln(d[r]-d[l]);
}
}
pure T[] primes(T)(T n)
{
import std.algorithm, std.bitmanip, std.conv, std.range;
auto sieve = BitArray();
sieve.length((n + 1) / 2);
sieve = ~sieve;
foreach (p; 1..((nsqrt(n) - 1) / 2 + 1))
if (sieve[p])
for (auto q = p * 3 + 1; q < (n + 1) / 2; q += p * 2 + 1)
sieve[q] = false;
auto r = sieve.bitsSet.map!(to!T).map!("a * 2 + 1").array;
r[0] = 2;
return r;
}
pure T nsqrt(T)(T n)
{
import std.algorithm, std.conv, std.range, core.bitop;
if (n <= 1) return n;
T m = 1 << (n.bsr / 2 + 1);
return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
const mod = 10^^9 + 7;
long modPow(long x, long k, long m) {
if (k == 0) return 1;
if (k % 2 == 0) return modPow(x * x % m, k / 2, m);
return x * modPow(x, k - 1, m) % m;
}
long nck(long n, long k, long mod) {
long x = 1;
for (int i = 0; i < k; i++) {
x = (x * (n - i)) % mod;
}
long kk = iota(1, k+1).reduce!((a, b) => a * b % mod);
return (x * modPow(kk, mod - 2, mod)) % mod;
}
long calc(long n, long a, long b) {
long ans = modPow(2, n, mod);
ans = (ans - nck(n, a, mod) + mod) % mod;
ans = (ans - nck(n, b, mod) + mod) % mod;
ans--;
return ans;
}
void main() {
int n, a, b; scan(n, a, b);
writeln(calc(n, a, b));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp.to!(char[]).array;
int i, res;
i = res = 0;
while (i != s.length / 2) {
//writeln(i);
if (s[i] != s[$-i-1]) {
if (s[i] == 'x') {
s.insertInPlace(s.length-i, 'x');
} else if (s[$-i-1] == 'x') {
s.insertInPlace(i, 'x');
} else {
res = -1;
break;
}
res++;
}
/*s.writeln;
readln();*/
i++;
}
res.writeln;
}
|
D
|
import std.stdio, std.range, std.conv, std.string, std.array, std.functional;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
import std.container.binaryheap;
void main()
{
auto input = readln().strip.split.to!(int[]);
const N = input[0];
const Q = input[1];
const S = readln().strip;
ulong[] list;
list.length = S.length;
char pc = S[0];
foreach(i; 1..S.length) {
char c = S[i];
if(pc=='A' && c == 'C')
{
list[i] = list[i-1]+1;
}else{
list[i] = list[i-1];
}
pc = c;
}
foreach(i; 0..Q) {
input = readln().strip.split.to!(int[]);
const s = input[0]-1;
const g = input[1]-1;
writeln(list[g] - list[s]);
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln(min(readln.chomp.to!int, readln.chomp.to!int) + min(readln.chomp.to!int, readln.chomp.to!int));
}
|
D
|
void main() {
auto N = ri;
auto S = rs;
if(S.length == 1) {
writeln(3);
return;
}
bool[] arr;
ulong index = 0;
while(index < N - 1) {
if(S[index] != S[index+1]) {
arr ~= false;
if(index == N - 2) arr ~= false;
index++;
} else {
arr ~= true;
index += 2;
}
}
ulong res;
if(arr[0]) res = 6;
else res = 3;
const ulong MOD = 1000000007;
alias Pair = Tuple!(bool, "a", bool, "b");
foreach(k; 0..arr.length-1) {
auto t = arr[k], i = arr[k+1];
if([t, i] == [false, false] || [t, i] == [false, true])
res = (res * 2) % MOD;
if([t, i] == [true, true])
res = (res * 3) % MOD;
t = i;
}
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!ulong;
const K = scan!int;
const steps = K.iota.map!(x => [scan!ulong, 1 + scan!ulong]).array;
steps.deb;
ulong solve() {
ulong ans;
auto dp = new ulong[N + 1];
dp[N - 1] = 1;
auto acc = new ulong[2 * N + 2];
acc[N - 1] = 1;
foreach_reverse(i; 0..N-1) {
ulong cur = 0;
foreach(s; steps) {
const l = i + s[0];
const r = i + s[1];
const t = (acc[r] > acc[l] ? MOD : 0) + acc[l] - acc[r];
cur += t;
cur %= MOD;
}
dp[i] = cur;
acc[i] = (cur + acc[i + 1]) % MOD;
}
dp.deb;
return dp[0];
}
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");
const MOD = 998244353;
// -----------------------------------------------
|
D
|
void main()
{
long n = rdElem;
long[] b = rdRow;
long[] lists = new long[n];
long idx = n;
foreach (i; 0 .. n)
{
foreach_reverse (j; 0 .. idx)
{
if (j + 1 == b[j])
{
--idx;
lists[idx] = b[j];
b = b[0..j] ~ b[j+1..$];
break;
}
if (j == 0)
{
writeln(-1);
return;
}
}
}
foreach (x; lists)
{
x.writeln;
}
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import 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 N = lread();
auto A = aryread();
foreach (i; 0 .. N)
{
A[i] -= i + 1;
}
long s = A.sum();
long eval(long x)
{
return A.map!(a => abs(a - x)).sum();
}
long ans = long.max;
long ok = -(10 ^^ 9);
long ng = +(10 ^^ 9);
while (1 < ng - ok)
{
long m = (ok + ng) / 2;
if (eval(m + 1) <= eval(m))
{
ok = m;
}
else
{
ng = m;
}
}
writeln(min(eval(ok), eval(ok - 1), eval(ok + 1)));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main() {
int S = to!int(chomp(readln()));
int s = S % 60;
S /= 60;
int m = S % 60;
S /= 60;
int h = S;
writeln(to!string(h) ~ ":" ~ to!string(m) ~ ":" ~ to!string(s));
}
|
D
|
import std.stdio, std.math, std.conv, std.algorithm, std.string, std.array;
double[12][1 << 12] dp;
int[] arr;
double search_min_length(int used, int[] rest, int last) {
if (used == ((1 << arr.length) - 1)) return arr[last];
if (dp[used][last] != -1) return dp[used][last];
double res = int.max;
foreach(int i, x; arr) {
if (((used >> i) & 1) == 0) {
double d = 2 * sqrt((arr[last] * arr[i]).to!double);
res = min(res, d + search_min_length(used | (1 << i), [x] ~ rest, i));
}
}
dp[used][last] = res;
return res;
}
void main() {
string str;
while((str = readln.chomp).length != 0) {
arr = str.split(" ").map!(x => x.dup.to!int).array;
double threshold = arr.front.to!double;
double ans = double.max;
arr.popFront;
foreach(ref d; dp)
foreach(ref x; d) x = -1.0;
foreach(int i, x; arr) {
ans = min(ans, (search_min_length(1 << i, [x], i) + x));
}
writeln(threshold >= ans ? "OK" : "NA");
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
immutable long mod = 10^^9 + 7;
immutable long inf = mod;
void main(){
int n = readln.chomp.to!int;
int ans;
foreach(i ; 0 .. n){
int x = readln.chomp.to!int;
ans += is_prime(x);
}
writeln(ans);
}
bool is_prime(int x){
for(int i = 2; i*i <= x; ++i){
if (x % i == 0) {
return false;
}
}
return true;
}
T modpow(T)(T x, T y, T mod){
return y ? modpow(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1;
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// ModInt {{{
struct ModInt(ulong modulus) {
import std.traits : isIntegral, isBoolean;
import std.exception : enforce;
private {
ulong val;
}
this(const ulong n) {
val = n % modulus;
}
this(const ModInt n) {
val = n.value;
}
@property {
ref inout(ulong) value() inout {
return val;
}
}
T opCast(T)() {
static if (isIntegral!T) {
return cast(T)(val);
} else if (isBoolean!T) {
return val != 0;
} else {
enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ ".");
}
}
ModInt opAssign(const ulong n) {
val = n % modulus;
return this;
}
ModInt opOpAssign(string op)(ModInt rhs) {
static if (op == "+") {
val += rhs.value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs.value) {
val += modulus;
}
val -= rhs.value;
} else if (op == "*") {
val = val * rhs.value % modulus;
} else if (op == "/") {
this *= rhs.inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs.value > 0) {
if (rhs.value % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opOpAssign(string op)(ulong rhs) {
static if (op == "+") {
val += ModInt(rhs).value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
auto r = ModInt(rhs);
if (val < r.value) {
val += modulus;
}
val -= r.value;
} else if (op == "*") {
val = val * ModInt(rhs).value % modulus;
} else if (op == "/") {
this *= ModInt(rhs).inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs > 0) {
if (rhs % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opUnary(string op)() {
static if (op == "++") {
this += 1;
} else if (op == "--") {
this -= 1;
} else {
enforce(false, op ~ " is not implemented.");
}
return this;
}
ModInt opBinary(string op)(const ulong rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(ref const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinaryRight(string op)(const ulong lhs) const {
mixin("return ModInt(this) " ~ op ~ "= lhs;");
}
long opCmp(ref const ModInt rhs) const {
return cast(long)value - cast(long)rhs.value;
}
bool opEquals(const ulong rhs) const {
return value == ModInt(rhs).value;
}
ModInt inv() const {
ModInt ret = this;
ret ^^= modulus - 2;
return ret;
}
string toString() const {
import std.format : format;
return format("%s", val);
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n, k;
cin.scan(n, k);
int[] h = cin.nextArray!int(n);
int[] dp = new int[n];
dp[0 .. $] = int.max;
foreach (i; 0 .. n) {
foreach (j; 1 .. k + 1) {
if (i + j >= n) continue;
if (dp[i] == int.max) dp[i] = 0;
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
}
writeln(dp[n - 1]);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int r1, c1, r2, c2;
scan(r1, c1, r2, c2);
auto mc = ModComb(2 * 10^^6 + 100);
long ans;
foreach (i ; c1 .. c2 + 1) {
ans += mc.c(r2 + i + 1, r2) - mc.c(r1 + i, r1 - 1) + mod;
ans %= mod;
}
writeln(ans);
}
struct ModComb {
int _N;
long[] _fact, _factinv;
long _mod;
this(int n, long mod = 1_000_000_007L) {
_N = n;
_fact = new long[](_N + 1);
_factinv = new long[](_N + 1);
_mod = mod;
_fact[0] = 1;
foreach (i ; 1 .. _N + 1) {
_fact[i] = (_fact[i-1] * i) % mod;
}
_factinv[_N] = _powmod(_fact[_N], mod - 2);
foreach_reverse (i ; 0 .. _N) {
_factinv[i] = (_factinv[i+1] * (i+1)) % mod;
}
}
long c(int n, int k) {
if (k < 0 || k > n) return 0;
return f(n) * finv(n - k) % _mod * finv(k) % _mod;
}
long p(int n, int r) {
if (r < 0 || r > n) return 0;
return f(n) * finv(n - r) % _mod;
}
long f(int n) {
return _fact[n];
}
long finv(int n) {
return _factinv[n];
}
long _powmod(long x, long y) {
return y > 0 ? _powmod(x, y>>1)^^2 % _mod * x^^(y & 1) % _mod : 1;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import 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 countAB(string s) {
int n = 0;
for (int i = 1; i < s.length; i++) {
if (s[i-1] == 'A' && s[i] == 'B') n++;
}
return n;
}
int calc(string[] a) {
int ans = a.map!(countAB).sum;
int xa = 0;
int bx = 0;
int ba = 0;
foreach (s; a) {
auto front = s.front;
auto last = s[$-1];
if (front == 'B' && last == 'A') ba++;
else if (front == 'B') bx++;
else if (last == 'A') xa++;
}
if (xa > 0) xa--;
else if (ba > 0) ba--;
else return ans;
if (ba > 0) {
ans += ba;
ba = 0;
}
// 末尾が A で終わっている状態
while (xa > 0 || bx > 0) {
if (bx == 0) break;
ans++;
bx--;
int k = min(bx, xa);
if (k == 0) break;
ans += k;
bx -= k;
xa -= k;
if (xa == 0) break;
xa--;
}
return ans;
}
void main() {
int n = readint;
string[] a;
for (int i = 0; i < n; i++) a ~= read!string;
writeln(calc(a));
}
|
D
|
// import chie template :) {{{
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format, std.bitmanip;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(T[], "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n, k;
cin.scan(n, k);
int[] x = cin.nextArray!int(n);
int res;
foreach (i; 0 .. n) {
res += min(x[i] * 2, abs(x[i] - k) * 2);
}
writeln(res);
}
|
D
|
void main()
{
long[] tmp = rdRow;
long h = tmp[0], n = tmp[1];
long[] a = new long[n], b = new long[n];
foreach (i; 0 .. n)
{
tmp = rdRow;
a[i] = tmp[0], b[i] = tmp[1];
}
long inf = 1L << 60;
long[] dp = new long[](h+1);
dp[] = inf;
dp[0] = 0;
foreach (i; 0 .. n)
{
long cnt = h / a[i];
foreach (j; 0 .. cnt)
{
dp[j+1] = min(dp[j+1], b[i]*(j+1));
}
foreach (j; 0 .. h)
{
if (dp[j] != inf)
{
long k = j + a[i];
if (k <= h)
{
dp[k] = min(dp[k], dp[j]+b[i]);
}
}
long rem = h - j;
long num = (rem + a[i] - 1) / a[i];
dp[h] = min(dp[h], b[i]*num+dp[j]);
}
}
dp[h].writeln;
}
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[10^^5] AS, BS, CS;
int[10^^5][3] MEMO;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) {
auto abc = readln.split.to!(int[]);
AS[i] = abc[0];
BS[i] = abc[1];
CS[i] = abc[2];
}
int solve(int i, int b) {
if (i == N) return 0;
auto bb = b == -1 ? 0 : b;
if (MEMO[bb][i]) return MEMO[bb][i];
return MEMO[bb][i] =
max(
b == 0 ? -1 : solve(i+1, 0) + AS[i],
b == 1 ? -1 : solve(i+1, 1) + BS[i],
b == 2 ? -1 : solve(i+1, 2) + CS[i]
);
}
writeln(solve(0, -1));
}
|
D
|
import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
import std.typecons;
alias Pair = Tuple!(size_t, bool);
void main()
{
immutable n = readln.chomp.to!size_t,
a = readln.chomp.split.to!(long[]);
// dp[i, k, 0] := max sum of k elements < i
// dp[i, k, 1] := max sum of k-1 elements < i and a_i
auto dp = new long[Pair][n];
dp[0][Pair(size_t(0), false)] = 0;
dp[0][Pair(size_t(1), true)] = a[0];
foreach (i; 1..n)
{
foreach (kvp; dp[i-1].byKeyValue)
{
immutable
k = kvp.key[0],
f = kvp.key[1],
v = kvp.value;
if (k < n/2 - (n-i-f+1)/2)
continue;
if (auto p = Pair(k, false) in dp[i])
dp[i][Pair(k, false)] = (*p).max(v);
else
dp[i][Pair(k, false)] = v;
if (!f)
dp[i][Pair(k+1, true)] = v + a[i];
}
debug stderr.writeln(dp[i]);
}
max(dp[n-1][Pair(n/2, true)], dp[n-1][Pair(n/2, false)]).writeln;
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void theCode(){
int n;
n = scan!int;
auto arr = scanArray;
auto freq = new ll[1000];
for(int i = 0; i < n; ++i){
for(int j = 0; j < i; ++j){
++freq[to!int(abs(arr[j] - arr[i]))];
}
}
ll cnt = 0;
for(int i = 0; i < 1000; ++i){
if(freq[i]){
++cnt;
}
}
writeln(cnt);
}
void main(){
long tests = 1;
tests = scan; // Toggle!
while(tests--) theCode();
}
/********* That's All Folks! *********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] tk;
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
enum P = 10L^^9+7;
void main()
{
auto nm = readln.split.to!(long[]);
auto N = nm[0];
auto M = nm[1];
auto xx = readln.split.to!(long[]);
auto yy = readln.split.to!(long[]);
long[] xs, ys;
foreach (i; 1..N) xs ~= xx[i] - xx[i-1];
foreach (i; 1..M) ys ~= yy[i] - yy[i-1];
long e;
foreach (i; 0..N-1) (e += xs[i] * (i+1) % P * (N-i-1) % P) %= P;
long r;
foreach (i; 0..M-1) (r += ys[i] * e % P * (i+1) % P * (M-i-1) % P) %= P;
writeln(r);
}
|
D
|
void main() {
auto N = readAs!ulong;
auto k = 1;
ulong a = 0;
if(N % 2 == 1) {
writeln(0);
return;
}
for(ulong i = 10; N >= i; i *= 5) a += N / i;
a.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
3.iota.map!(i => readln.chomp[i]).array.writeln;
}
|
D
|
// Code By H~$~C
module main;
import core.stdc.stdlib, core.stdc.string;
import std.stdio, std.array, std.string, std.math, std.uni;
import std.algorithm, std.range, std.random, std.container;
import std.conv, std.typecons, std.functional;
immutable Maxn = 100005;
immutable LOG = 19;
int n, q;
Tuple!(int, int, int)[][Maxn] g, qry;
int[LOG][Maxn] par;
int[Maxn] depth, dist, D, ans;
void dfs1(int u, int fa) {
depth[u] = depth[fa] + 1;
par[u][0] = fa;
for (int j = 1; j < LOG; ++j)
par[u][j] = par[par[u][j - 1]][j - 1];
foreach(pr; g[u]) {
int v = pr[0], w = pr[2];
if (v == fa) continue;
dist[v] = dist[u] + w;
dfs1(v, u);
}
}
int get_lca(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
for (int j = LOG - 1; j >= 0; --j)
if ((depth[v] - depth[u]) & (1 << j))
v = par[v][j];
if (u == v) return u;
for (int j = LOG - 1; j >= 0; --j) {
if (par[u][j] != par[v][j]) {
u = par[u][j];
v = par[v][j];
}
}
return par[u][0];
}
int[Maxn] dis, cnt;
void dfs2(int u, int fa) {
foreach(pr; g[u]) {
int v = pr[0], c = pr[1], w = pr[2];
if (v == fa) continue;
dis[c] += w, cnt[c]++;
dfs2(v, u);
dis[c] -= w, cnt[c]--;
}
foreach(pr; qry[u]) {
int c = pr[0], v = pr[1], id = pr[2];
ans[id] += (D[id] * cnt[c] - dis[c]) * v;
}
}
void solve(in int testcase) {
n.read, q.read;
foreach(i; 1 .. n) {
int u, v, c, d;
u.read, v.read, c.read, d.read;
g[u] ~= tuple(v, c, d);
g[v] ~= tuple(u, c, d);
}
dfs1(1, 0);
foreach(i; 1 .. q + 1) {
int u, v, c;
c.read, D[i].read, u.read, v.read;
int lca = get_lca(u, v);
ans[i] = dist[u] + dist[v] - 2 * dist[lca];
qry[u] ~= tuple(c, 1, i);
qry[v] ~= tuple(c, 1, i);
qry[lca] ~= tuple(c, -2, i);
}
dfs2(1, 0);
foreach(i; ans[1 .. q + 1])
writeln(i);
}
// ************************************************************
T reader(T)() { return readToken().to!T; }
void read(T)(ref T t) { t = reader!T; }
T[] readarray(T)() { return readln.splitter.map!(to!T).array; }
void chmax(T)(ref T a, T b) { a = max(a, b); }
void chmin(T)(ref T a, T b) { a = min(a, b); }
alias less = (a, b) => a < b;
alias greater = (a, b) => a > b;
T min_element(T)(in T[] a) { return a.element!less; }
T max_element(T)(in T[] a) { return a.element!greater; }
alias unique = uniq;
// constant or some variables
immutable int inf = 0x3f3f3f3f;
immutable long lnf = 0x3f3f3f3f3f3f3f3f;
immutable typeof(null) NULL = null;
string[] _tokens;
// functions
T element(alias pred, T)(in T[] a) {
int pos = 0;
for (int i = 1; i < cast(int)(a.length); ++i)
if (binaryFun!pred(a[i], a[pos])) pos = i;
return a[pos];
}
string readToken() {
for (; _tokens.empty; ) {
if (stdin.eof) return "";
_tokens = readln.split;
}
auto token = _tokens.front;
_tokens.popFront;
return token;
}
int binary_bearch(alias pred, T)(in T[] as) {
int l = -1, h = cast(int)(as.length), mid;
for (; l + 1 < h; ) {
mid = (l + h) >> 1;
(unaryFun!pred(as[mid]) ? h : l) = mid;
}
return h;
}
int lower_bound(alias pred = less, T)(in T[] as, T val) {
return as.binary_bearch!(a => a == val || pred(val, a));
}
int upper_bound(alias pred = less, T)(in T[] as, T val) {
return as.binary_bearch!(a => pred(val, a));
}
void main(string[] args) {
int tests = 1;
// tests.read();
foreach(test; 1 .. tests + 1) solve(test);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
bool mode;
ans[ti] = true;
foreach (i; 0..n)
{
if (!mode)
{
if (a[i] < i)
mode = true;
}
if (mode)
{
a[i].chmin(a[i-1]-1);
if (a[i] < 0)
ans[ti] = false;
}
}
}
foreach (e; ans)
writeln(e ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
void main() {
string s = readln.chomp;
foreach (x; lowercase) {
if (!s.canFind(x)) {
x.writeln;
break;
}
if (x == 'z') "None".writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
const MOD = 10^^9 + 7;
void naive(long k, int[] a) {
int[] b;
for (int i = 0; i < k; i++) {
for (int j = 0; j < a.length; j++) {
b ~= a[j];
}
}
long ans = 0;
for (int i = 0; i < b.length; i++) {
for (int j = i + 1; j < b.length; j++) {
if (b[i] > b[j]) {
ans++;
ans %= MOD;
}
}
}
writeln("ans ", ans);
}
long calc(long k, int[] a) {
long ans = 0;
for (int i = 0; i < a.length; i++) {
long left = 0;
long right = 0;
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) right++;
}
for (int j = i - 1; j >= 0; j--) {
if (a[i] > a[j]) left++;
}
long t = (k * (k + 1) / 2) % MOD;
long x = right * t % MOD;
x += left * (((k - 1) * k) / 2 % MOD) % MOD;
ans = (ans + x) % MOD;
}
return ans;
}
void main() {
int n, k; scan(n, k);
auto a = readints;
writeln(calc(k, a));
}
|
D
|
void main() {
problem();
}
void problem() {
const S = scan;
auto N = S.length;
long solve() {
if (N < 4) return 0;
const P = 2019;
long ans;
auto cs = new long[](P);
cs[0] = 1;
long x, t = 1;
foreach_reverse (c; S) {
x = ((c-'0').to!long * t + x) % P;
t = (t * 10) % P;
ans += cs[x];
++cs[x];
}
return ans;
}
long solve2() {
const MOD = 2019;
auto countsPerMod = new long[MOD];
long answer;
countsPerMod[0] = 1;
long acc;
foreach(i, c; S) {
const number = c - '0';
auto newCountsPerMod = new long[MOD];
foreach(m; 0..MOD) {
newCountsPerMod[(10 * m) % MOD] += countsPerMod[m];
}
acc = (acc * 10 + number) % MOD;
answer += newCountsPerMod[acc];
newCountsPerMod[acc]++;
countsPerMod = newCountsPerMod;
}
return answer;
}
solve2().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
import std.bigint, std.functional;
// -----------------------------------------------
|
D
|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
void main()
{
long n, m;
scan(n, m);
//writeln(n, m);
auto color = new bool[](n);
color[0] = true;
auto cup = new long[](n);
cup[] = 1;
foreach (i; 0 .. m)
{
long x, y;
scan(x, y);
//writeln(x, y);
cup[x - 1] -= 1;
cup[y - 1] += 1;
if (color[x - 1])
{
color[y - 1] = true;
}
if (cup[x - 1] == 0)
{
color[x - 1] = false;
}
}
// writeln(cup);
// writeln(color);
auto cnt = 0L;
foreach (i; 0 .. n)
{
if (cup[i] != 0 && color[i])
{
cnt += 1;
}
}
writeln(cnt);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio;
void main()
{
for (int i = 0; i < 1000; i++) {
writeln("Hello World");
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n;
scan(n);
int d, x;
scan(d, x);
foreach (i ; 0 .. n) {
int ai;
scan(ai);
x += (d + ai - 1) / ai;
}
writeln(x);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
writeln(readln.chomp.replace(" ", "").to!int % 4 ? "NO" : "YES");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
pragma(inline, true) T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
pragma(inline, true) void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args) arg = ss[i].parse!int;
}
void main() {
int n; scan(n);
int[] a = reads().map!(i => i-1).array;
int[] b = reads();
int[] c = reads();
int pre = -100, ans=0;
foreach (x; a) {
ans += b[x];
if (x == pre + 1) ans += c[pre];
pre = x;
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
char[] s = readln.chomp.to!(char[]);
long k = readln.chomp.to!long;
foreach(i, c; s){
int x = 'z' + 1 - c;
if(s[i] > 'a' && x <= k){
s[i] = 'a';
k -= x;
}
}
s[$ - 1] = 'a' + (cast(long)(s[$ - 1] - 'a') + k) % 26;
s.writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }
long mod = 10^^9 + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto H = RDR.ARR;
long ans;
long big;
foreach (i; 0..N)
{
if (H[i] >= big) ++ans;
big = max(big, H[i]);
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std;
void calc(int n, int m) {
int lo = 1;
int hi = n;
bool[int] used;
foreach (i; 0..m) {
int a = abs(lo - hi);
int b = n - a;
if (a == b || a in used || b in used) {
hi--;
}
writeln(lo, " ", hi);
lo++;
hi--;
used[a] = used[b] = true;
}
}
void main() {
int n, m; scan(n, m);
calc(n, m);
}
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, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void main() {
int a, b, c;
scan(a, b, c);
int k;
scan(k);
int cnt;
while (a >= b) {
b *= 2;
cnt++;
}
while (b >= c) {
c *= 2;
cnt++;
}
yes(cnt <= k);
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio;
import std.string;
void main() {
string ans = "WA";
string s = readln.strip;
if (s[0] == 'A') {
ulong c = 0;
foreach(i, e; s[0..$ - 1]) {
if (e == 'C' && i > 1) {
c = i;
break;
}
}
if (c > 1) {
ans = "AC";
foreach(i, e; s) {
if (i == 0 || i == c)
continue;
if (e < 97 || e > 122) {
ans = "WA";
break;
}
}
}
}
writeln(ans);
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
auto s = readln.chomp.to!(char[]);
int q;
rd(q);
auto ks = readln.split.to!(int[]);
foreach (k; ks) {
long d = 0, dm = 0, dmc = 0;
long m = 0;
foreach (i; 0 .. k) {
if (s[i] == 'D') {
d++;
} else if (s[i] == 'M') {
dm += d;
m++;
} else if (s[i] == 'C') {
dmc += dm;
}
}
foreach (i; k .. n) {
if (s[i - k] == 'D') {
d--;
dm -= m;
} else if (s[i - k] == 'M') {
m--;
}
if (s[i] == 'D') {
d++;
} else if (s[i] == 'M') {
dm += d;
m++;
} else if (s[i] == 'C') {
dmc += dm;
}
}
writeln(dmc);
}
}
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
|
class UnionFind{
int[] root;
this(int n){
root.length=n;
foreach(i; 0..n) root[i]=i;
}
int find(int x){
if(x==root[x]) return x;
else return root[x]=find(root[x]);
}
void merge(int x, int y){
x=find(x); y=find(y);
if(x==y) return;
root[x]=y;
}
}
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, k, l; rd(n, k, l);
auto s1=new UnionFind(n), s2=new UnionFind(n);
foreach(_; 0..k){
int a, b; rd(a, b);
s1.merge(a-1, b-1);
}
foreach(_; 0..l){
int a, b; rd(a, b);
s2.merge(a-1, b-1);
}
struct Pair{int fi, se;}
int[Pair] freq;
foreach(i; 0..n){
auto pair=Pair(s1.find(i), s2.find(i));
if(pair in freq) freq[pair]++;
else freq[pair]=1;
}
foreach(i; 0..n){
writeln(freq[Pair(s1.find(i), s2.find(i))]);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
long bignum = 1_000_000_007;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a, b, c, d;
scan(a, b, c, d);
long e = gcd(c, d);
e = min(c, d) * (max(c, d) / e);
long f = b / c - a / c + !(a % c);
long g = b / d - a / d + !(a % c);
long h = b / e - a / e + !(a % c);
long i = f + g - h;
(b - a - i + 1).max(0).writeln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
int main()
{
string[] num = readln().split();
num.sort;
writeln(num[0], " ", num[1], " ", num[2]);
return 0;
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.array, std.container;
int n, k, c;
bool[] solve() {
scanf("%d %d %d\n", &n, &k, &c);
string s = readln.chomp;
bool[] banned = s.map!(c => c=='x').array;
int[] l = new int[k];
int k_cnt = 0;
for(int i=0; i<n; i++) {
if(!banned[i]) {
l[k_cnt] = i;
i += c;
if(++k_cnt == k) break;
}
}
if(k_cnt < k) return new bool[n];
int[] r = new int[k];
k_cnt = k;
for(int i=n-1; i>=0; i--) {
if(!banned[i]) {
r[--k_cnt] = i;
i -= c;
if(k_cnt == 0) break;
}
}
bool[] result = new bool[n];
for(int i=0; i<l.length; i++) {
result[l[i]] = l[i]==r[i];
}
return result;
}
void main() {
auto res = solve();
for(int i=0; i<res.length; i++) {
if(res[i]) writeln(i+1);
}
}
|
D
|
import std.stdio;
//import std.string;
//import std.conv;
import core.stdc.stdio;
void main(){
//int a = to!int(chomp(readln()));
//int b = to!int(chomp(readln()));
int a;
int b;
scanf("%d",&a);
scanf("%d",&b);
if(a == b)
writeln("a == b");
else
writeln((a < b) ? "a < b" : "a > b");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main()
{
while (true) {
string[] str = readln().split();
if (str[0] == "0" && str[1] == "0") break;
for (int i = 0; i < str[0].to!int(); i++) {
for (int j = 0; j < str[1].to!int(); j++) {
write("#");
}
writeln();
}
writeln();
}
return 0;
}
|
D
|
void main() {
long[] tmp = readln.split.to!(long[]);
long n = tmp[0], k = tmp[1];
writeln(k * (k - 1) ^^ (n - 1));
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main() {
(700 + readln.chomp.count!"a == 'o'" * 100).writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.math;
void main()
{
string w = chomp(readln());
int[26] alphabet_list;
for(int i = 0;i < w.length;i ++)
{
alphabet_list[to!int(w[i])-97] ++;
}
int odd_num_count = 0;
for(int i=0;i < 26;i ++)
{
if(alphabet_list[i]%2 != 0)
{
odd_num_count ++;
}
}
if(odd_num_count == 0)
{
writeln("Yes");
}
else
{
writeln("No");
}
}
|
D
|
import std.stdio, std.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 X = RD;
auto N = RD;
auto P = RDA;
bool[long] set;
foreach (i; 0..N)
{
set[P[i]] = true;
}
long ans;
foreach (i; 0..10^^7)
{
auto x1 = X - i;
if (set.get(x1, false) == false)
{
ans = x1;
break;
}
auto x2 = X + i;
if (set.get(x2, false) == false)
{
ans = x2;
break;
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
int[(10^^5)*2] AS;
void main()
{
auto N = readln.chomp.to!int;
foreach (i; 0..N) AS[i] = readln.chomp.to!int;
if (AS[0]) {
writeln("-1");
return;
}
long cnt;
foreach (i; 1..N) {
if (AS[i] > AS[i-1]+1) {
writeln("-1");
return;
}
if (AS[i] == AS[i-1]+1) {
++cnt;
} else {
cnt += AS[i];
}
}
writeln(cnt);
}
|
D
|
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); }
struct SegTree(T)
{
T delegate(const(T), const(T)) f;
T[] data;
T init;
this(T[] _data, T delegate(const(T), const(T)) _f, T _init)
{
f = _f; init = _init;
size_t n = 1; while (n < _data.length) n *= 2; data.length = n*2-1;
foreach (i; 0.._data.length) data[i+n-1] = _data[i];
foreach (i; _data.length..n) data[i+n-1] = _init;
foreach_reverse (i; 0..n-1) data[i] = f(data[i*2+1], data[i*2+2]);
}
T query(size_t l, size_t r) const
{
size_t n = (data.length+1) / 2; l += n-1; r += n-1; T res = init;
while (l < r)
{
if ((l & 1) == 0) res = f(res, data[l]);
if ((r & 1) == 0) res = f(res, data[r-1]);
l = l/2; r = (r-1)/2;
}
return res;
}
void update(size_t i, T v)
{
i += (data.length+1) / 2 - 1; data[i] = v;
while (i != 0) { i = (i-1)/2; data[i] = f(data[i*2+1], data[i*2+2]); }
}
}
void main()
{
auto N = RD;
auto Q = RD;
auto a = RDA;
auto st = new SegTree!(long)(a, (in long x, in long y)=>x+y, 0);
foreach (i; 0..Q)
{
auto t = RD;
if (t == 0)
{
auto p = RD;
auto x = RD;
auto last = st.query(p, p+1);
st.update(p, last+x);
}
else
{
auto l = RD;
auto r = RD;
debug writeln(st.data);
writeln(st.query(l, r));
}
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
auto a = iota(n).map!(i => readln.chomp.to!int).array;
auto left = new int[](n + 1);
foreach (i ; 1 .. n + 1) {
left[i] = max(left[i - 1], a[i - 1]);
}
auto right = new int[](n + 1);
foreach_reverse (i ; 0 .. n) {
right[i] = max(right[i + 1], a[i]);
}
foreach (i ; 0 .. n) {
auto ans = max(left[i], right[i + 1]);
writeln(ans);
}
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.