code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto s = read!string;
writeln(s.count('1'));
}
|
D
|
import std;
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}
T[] lreads(T=long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}
void arywrite(T=long)(T[] ary){ary.map!(text).join(' ').writeln;}
void scan(TList...)(ref TList Args){auto line = readln.split;
foreach (i,T;TList){T val=line[i].to!(T);Args[i]=val;}}
void dprint(TList...)(TList Args){debug{auto s=new string[](TList.length);
static foreach(i,a;Args) s[i]=text(a);s.map!(text).join(' ').writeln;}}
alias sread=()=>readln.chomp();enum MOD =10^^9+7;
alias PQueue(T,alias less="a<b") = BinaryHeap!(Array!T,less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
auto H = aryread();
auto G = new long[][](N);
foreach (i; 0 .. M)
{
long a, b;
scan(a, b);
a--, b--;
G[a] ~= b;
G[b] ~= a;
}
long ans;
o: foreach (i; 0 .. N)
{
foreach (g; G[i])
if (H[i] <= H[g])
{
continue o;
}
ans++;
}
writeln(ans);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long n, m;
scan(n, m);
long max_x = long.min;
long min_x = long.max;
foreach (_; 0 .. m)
{
long l, r;
scan(l, r);
// writeln(l, r);
max_x = max(max_x, l);
min_x = min(min_x, r);
}
// writeln("max", max_x);
// writeln("min", min_x);
if (min_x >= max_x)
{
writeln(min_x - max_x + 1);
}
else
{
writeln(0);
}
}
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.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T read(T)(){ return read.to!T; }
T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; }
T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int n = read!int;
string s = readln.chomp;
int sum = 0;
int[] sums = [0]; // sum[i] = sum(0 .. i)
foreach(i; 0 .. n){
if(s[i] == '#') sum += 1;
sums ~= sum;
}
int ans = n + 10;
foreach(i; 0 .. n + 1){
int tmp = sums[i] + ((n - i) - (sums[n] - sums[i]));
if(tmp < ans) ans = tmp;
}
ans.writeln;
}
|
D
|
import std.stdio,std.string,std.conv,std.array,std.algorithm;
void main(){
readln();
auto arr = readln().chomp().split();
reverse(arr);
arr.join(" ").writeln();
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long H, W;
scan(H, W);
auto S = new string[](H);
foreach (i; 0 .. H)
scan(S[i]);
auto G = new long[][](H * W, H * W);
long id(long h, long w)
{
return h * W + w;
}
foreach (ref row; G)
row[] = 1234567;
foreach (i; 0 .. H)
foreach (j; 0 .. W)
if (S[i][j] != '#')
G[id(i, j)][id(i, j)] = 0;
foreach (h; 0 .. H)
foreach (w; 0 .. W)
{
if (S[h][w] == '#')
continue;
if (0 < h && S[h - 1][w] != '#')
G[id(h, w)][id(h - 1, w)] = 1;
if (0 < w && S[h][w - 1] != '#')
G[id(h, w)][id(h, w - 1)] = 1;
if (h < H - 1 && S[h + 1][w] != '#')
G[id(h, w)][id(h + 1, w)] = 1;
if (w < W - 1 && S[h][w + 1] != '#')
G[id(h, w)][id(h, w + 1)] = 1;
}
// foreach (ref row; G)
// writeln(row);
// writeln();
foreach (k; 0 .. (H * W))
foreach (i; 0 .. (H * W))
foreach (j; 0 .. (H * W))
if (G[i][j] > G[i][k] + G[k][j])
G[i][j] = G[i][k] + G[k][j];
long ans = -1;
foreach (row; G)
{
// writeln(row);
foreach (x; row)
if (x < 1234567)
ans = max(ans, x);
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.numeric;
import std.math;
import std.string;
void main()
{
auto tokens = split(chomp(readln()));
auto n = to!int(tokens[0]);
auto m = to!int(tokens[1]);
int[][] nodes;
nodes.length = n;
foreach (i; 0..m)
{
auto tokens2 = split(chomp(readln()));
auto a = to!int(tokens2[0]) - 1;
auto b = to!int(tokens2[1]) - 1;
nodes[a] ~= b;
nodes[b] ~= a;
}
int search(int pos, int[] open)
{
if (open.length == 0) return 1;
int cnt;
foreach (next; nodes[pos])
{
auto oi = countUntil(open, next);
if (oi == -1) continue;
cnt += search(next, remove(open.dup, oi));
}
return cnt;
}
int[] open_start;
foreach (i; 1..n) open_start ~= i;
auto result = search(0, open_start);
writeln(result);
stdout.flush();
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long n; rd(n);
for(long a=1; a<=3500; a++)for(long b=1; b<=3500; b++){
auto x=4*a*b-n*a-n*b;
if(x<=0) continue;
auto y=n*a*b;
if(y%x==0){
writeln(a, " ", b, " ", y/x); return;
}
}
}
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.array;
import std.conv;
void main()
{
auto input = split(readln());
int a = to!(int)(input[0]);
int b = to!(int)(input[1]);
if (a < b)
writeln("a < b");
else if (a > b)
writeln("a > b");
else
writeln("a == b");
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
auto S = sread();
if ('A' <= S[0] && S[0] <= 'Z')
{
writeln("A");
}
else
{
writeln("a");
}
}
|
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()
{
long X = lread();
writeln((X < 1200) ? "ABC" : "ARC");
}
|
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 bool[](t);
foreach (i; 0..t)
{
auto a = RD!int;
auto b = RD!int;
if (min(a, b)*2 < max(a, b))
ans[i] = false;
else
ans[i] = (a+b) % 3 == 0;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto n=readln.chomp.to!(char[]);
int sum1=0;
foreach(c; n) sum1+=(c-'0');
if(n.length>1){
int tmp=(n[0]-'0')-1;
tmp+=(n.length.to!(int)-1)*9;
sum1=max(sum1, tmp);
}
writeln(sum1);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
|
D
|
void main()
{
long n = rdElem;
long[] a = rdRow;
long[long] cnt;
foreach (x; a)
{
++cnt[x];
}
long total;
long[long] list;
foreach (key, val; cnt)
{
list[key] = val * (val - 1) / 2 - (val - 1) * (val - 2) / 2;
total += val * (val - 1) / 2;
}
foreach (x; a)
{
writeln(total - list[x]);
}
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[][10^^5] G;
bool[3][10^^5] F;
alias S = Tuple!(int, "p", int, "c");
void main()
{
auto nm = readln.split.to!(int[]);
auto N = nm[0];
auto M = nm[1];
foreach (_; 0..M) {
auto uv = readln.split.to!(int[]);
G[uv[0]-1] ~= uv[1]-1;
}
auto st = readln.split.to!(int[]);
auto S = st[0]-1;
auto T = st[1]-1;
auto ss = [S];
F[S][0] = true;
int c;
while (!ss.empty) {
++c;
int[] nss;
foreach (s; ss) {
foreach (n; G[s]) {
if (F[n][c%3]) continue;
F[n][c%3] = true;
if (c % 3 == 0 && n == T) {
writeln(c/3);
return;
}
nss ~= n;
}
}
ss = nss;
}
writeln(-1);
}
|
D
|
import std.stdio, std.conv, std.string, std.array;
bool f(int[] nList, int i, int m)
{
if(m == 0)
return true;
if(m < 0)
return false;
if(i == nList.length)
return false;
return f(nList, i+1, m) || f(nList, i+1, m-nList[i]);
}
void main()
{
readln();
int[] nList = readln.split.to!(int[]);
readln();
int[] qList = readln.split.to!(int[]);
foreach(int q; qList)
{
if(f(nList, 0, q))
writeln("yes");
else
writeln("no");
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, k, q;
scan(n, k, q);
auto a = iota(q).map!(i => readln.chomp.to!int).array;
a[] -= 1;
auto s = new int[](n);
s[] = q;
foreach (ai ; a) {
s[ai]--;
}
foreach (i ; 0 .. n) {
writeln(s[i] < k ? "Yes" : "No");
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto nm = readNums!int;
auto ks = new int[][](nm[1]);
foreach(i; 0 .. nm[1]){
ks[i] ~= readNums!int;
}
auto p = readNums!int;
int cnt;
for(int i = 0; i < 1<<nm[0]; i++){
int[] search;
for(int j = 0; j < nm[0]; j++){
if(i & (1 << j)){
search ~= j + 1;
}
}
for(int j = 0; j < nm[1]; j++){
int c;
for(int k = 1; k <= ks[j][0]; k++){
if(canFind(search, ks[j][k])){
c++;
}
}
if(c % 2 != p[j]) break;
if(j == nm[1]-1) {
cnt++;
}
}
}
writeln(cnt);
}
|
D
|
void main(){
int n=_scan();
int d, ans;
int[int] dic;
foreach(i; 0..n){
d = _scan();
if(dic.get(d, 0)==0){
ans++;
}
dic[d]++;
}
ans.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
//辞書順順列はiota(1,N),nextPermituionを使う
void end(T)(T v)
if(isIntegral!T||isSomeString!T||isSomeChar!T)
{
import core.stdc.stdlib;
writeln(v);
exit(0);
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto x = scanElem;
auto a = scanElem-scanElem;
if(a>=0)end("delicious");
if(a+x>=0)end("safe");
end("dangerous");
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int a, b, c;
scan(a, b, c);
writeln(b - a == c - b ? "YES" : "NO");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv;
void main() {
string s = readln.chomp;
auto cur = s.length / 2;
char pi = s[cur];
if (s.length % 2 == 0) {
while (cur < s.length && pi == s[$ - cur - 1] && pi == s[cur]) {
++cur;
}
} else {
++cur;
while (cur < s.length && pi == s[$ - cur - 1] && pi == s[cur]) {
++cur;
}
}
writeln(cur);
}
|
D
|
import std.stdio, std.string, std.conv, std.math, std.algorithm;
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], d = tmp[1];
int[][] x = new int[][](n);
foreach (i; 0 .. n) {
x[i] = readln.split.to!(int[]);
}
int[] square;
for (int i = 1; i * i <= 16000; ++i) {
square ~= i * i;
}
int cnt;
foreach (i; 0 .. n-1) {
foreach (j; i+1 .. n) {
int sed;
foreach (k; 0 .. d) {
sed += (x[i][k] - x[j][k]) ^^ 2;
}
if (square.canFind(sed)) ++cnt;
}
}
cnt.writeln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto M = s[1];
auto A = readln.split.map!(to!int).array;
auto numbers = new int[](N + 1);
auto segment = new int[](N);
foreach (a; A) numbers[a] += 1;
foreach (i; 1..N+1) {
if (numbers[i] == 0) continue;
foreach (j; 0..numbers[i]) {
if (i - j - 1 < 0) break;
segment[i - j- 1] += 1;
}
}
int ans = segment.map!(x => x == 0).sum;
while (M--) {
s = readln.split.map!(to!int);
auto X = s[0] - 1;
auto Y = s[1];
if (A[X] != Y) {
int a = A[X] - (numbers[A[X]]);
int b = Y - (numbers[Y] + 1);
if (a != b && a >= 0 && segment[a] == 1) ans += 1;
if (a != b && b >= 0 && segment[b] == 0) ans -= 1;
numbers[A[X]] -= 1;
numbers[Y] += 1;
A[X] = Y;
if (a >= 0) segment[a] -= 1;
if (b >= 0) segment[b] += 1;
}
ans.writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
string s;
scan(s);
auto cnt = new int[](3);
foreach (ch ; s) {
cnt[ch - 'a']++;
}
cnt.sort();
writeln(cnt[2] - cnt[0] <= 1 ? "YES" : "NO");
}
struct UnionFind {
private {
int N;
int[] p;
int[] rank;
}
this (int n) {
N = n;
p = iota(N).array;
rank = new int[](N);
}
int find_root(int x) {
if (p[x] != x) {
p[x] = find_root(p[x]);
}
return p[x];
}
bool same(int x, int y) {
return find_root(x) == find_root(y);
}
void unite(int x, int y) {
int u = find_root(x), v = find_root(y);
if (u == v) return;
if (rank[u] < rank[v]) {
p[u] = v;
}
else {
p[v] = u;
if (rank[u] == rank[v]) {
rank[u]++;
}
}
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this(int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head = (head + 1) % N;
}
void clear() {
head = tail = 0;
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
void maxAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) max(dst, src);
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N = lread();
auto A = aryread();
long sum = A.sum();
long ans = long.max;
foreach (i; 1 .. N)
A[i] = A[i] + A[i - 1];
foreach (i; 0 .. N - 1)
ans = ans.min(abs(A[i] * 2 - sum));
// writeln(sum, A);
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto tmp = readln.split.to!(long[]);
auto N = tmp[0], K = tmp[1];
auto xs = readln.split.to!(long[]);
long minTime = long.max;
foreach (i; 0..N-K+1) {
auto j = i + K - 1;
auto t = min(abs(xs[i]) + xs[j] - xs[i], abs(xs[j]) + xs[j] - xs[i]);
if (t < minTime) minTime = t;
}
if (minTime == long.max) minTime = 0;
writeln(minTime);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.array;
import std.string;
import std.math;
import std.functional;
import std.range;
import std.typecons;
import std.format;
import std.bigint;
void main(string[] args) {
readln.solve.writeln;
}
auto solve(string line1) {
auto _ = line1.chomp.split.map!(to!BigInt).array;
BigInt X = _[0];
BigInt Y = _[1];
int res = 0;
BigInt ind = 1;
while (X * ind <= Y) {
res++;
ind *= 2;
}
return res;
} unittest {
assert(solve("3 20") == 3, "1");
assert(solve("25 100") == 3, "2");
assert(solve("314159265 358979323846264338") == 31, "3");
}
|
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 a = RD!int;
auto b = RD!int;
auto x = min(a, b);
auto y = max(a, b);
auto d = y - x;
ans[i] += d / 5;
x += ans[i] * 5;
if (x != y)
{
if (y-x >= 3)
ans[i] += 2;
else
++ans[i];
}
}
foreach (e; ans)
writeln(e);
stdout.flush();
debug readln();
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/dp/tasks/dp_a
// dynamic programming
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
void main() {
const long n = readln.chomp.to!long;
const long[] h = 0~readln.split.map!(to!long).array;
long[] dp = new long[n+1];
dp[1] = 0;
dp[2] = abs(h[2] - h[1]);
for(int i = 3; i <= n; ++i)
dp[i] = min(dp[i-1] + abs(h[i] - h[i - 1]),
dp[i-2] + abs(h[i] - h[i - 2]));
dp[n].writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main()
{
char[] buf;
int a;
stdin.readln(buf);
a = to!(int)(chomp(buf));
a = a * a * a;
writeln(a);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto edges = new int[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
edges[s[0]-1] ~= s[1]-1;
edges[s[1]-1] ~= s[0]-1;
}
if (N == 2) {
writeln("Second");
return;
} else if (N == 3) {
writeln("First");
return;
}
bool first = false;
int dfs(int n, int p) {
int[] tmp;
foreach (m; edges[n]) if (m != p)
tmp ~= dfs(m, n);
if (tmp.sum > 1)
first = true;
//writeln(n, tmp);
return tmp.sum ? 0 : 1;
}
if (dfs(0, -1))
first = true;
writeln( first ? "First" : "Second" );
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
void main()
{
auto A = scanElem;
auto B = scanElem;
auto C = scanElem;
if(A+B>=C)writeln("Yes");else writeln("No");
}
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
import core.stdc.stdlib;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
|
D
|
import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main()
{
auto x = readln.chomp.to!int;
while (x.notPrime)
x += 1;
x.writeln;
}
bool notPrime(int x)
{
if (x < 4)
return x < 2;
int d = 2;
while (d*d <= x)
{
if (!(x % d)) return true;
d += 1;
}
return false;
}
|
D
|
import std;
long calc(long[] a) {
if (a.any!(e => e == 0)) return 0;
long x = 1;
long m = 10L ^^ 18;
foreach (e; a) {
long y = x * e;
if (y < x) return -1;
if (y > m) return -1;
x = y;
}
return x;
}
void main() {
int n; scan(n);
auto a = reads!long;
writeln(calc(a));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
import std.math;
void main()
{
string[] input = split(readln());
int x = to!int(input[0]);
int a = to!int(input[1]);
int b = to!int(input[2]);
if(abs(x - a) < abs(x - b)){
writeln("A");
} else {
writeln("B");
}
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//}}}
void main() {
Scanner sc = new Scanner;
int X, Y;
sc.scan(X, Y);
writeln(X + Y / 2);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
foreach(s;stdin.byLine())
{
int[10] c; foreach(n;s) ++c[n-'0'];
bool dfs(int n, bool d)
{
if(n>9) return d;
if(c[n]==0) return dfs(n+1,d);
if(c[n]>=3)
{
c[n]-=3;
auto r = dfs(n,d);
c[n]+=3;
if(r) return true;
}
if(n<=9-2 && c[n]>0 && c[n+1]>0 && c[n+2]>0)
{
foreach(i;0..3) --c[n+i];
auto r = dfs(n,d);
foreach(i;0..3) ++c[n+i];
if(r) return true;
}
if(!d && c[n]>=2)
{
c[n]-=2;
auto r = dfs(n,true);
c[n]+=2;
if(r) return true;
}
return false;
}
bool solve(int n)
{
if(c[n]>=4) return false;
++c[n];
auto r = dfs(1,false);
--c[n];
return r;
}
auto a = iota(1,10).filter!solve().map!(to!string).join(" ");
if(a=="") a="0";
a.writeln();
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
int n, q;
scan(n, q);
auto sd = SqrtDecomp(n);
foreach (_ ; 0 .. q) {
auto line = readln.split.to!(int[]);
if (line[0] == 0) {
sd.update(line[1], line[2]);
}
else {
writeln(sd.getMin(line[1], line[2] + 1));
}
}
}
struct SqrtDecomp {
private {
enum sqrtN = 512;
int N, K;
int[] bucket;
int[] data;
}
this(int n) {
N = n;
K = (N + sqrtN - 1) / sqrtN;
bucket = new int[](K);
data = new int[](K * sqrtN);
bucket[] = int.max;
data[] = int.max;
}
void update(int i, int x) {
int k = i / sqrtN;
data[i] = x;
bucket[k] = int.max;
foreach (j ; k * sqrtN .. (k + 1) * sqrtN) {
bucket[k] = min(bucket[k], data[j]);
}
}
int getMin(int l, int r) {
int res = int.max;
foreach (k ; 0 .. K) {
int bl = k * sqrtN, br = (k + 1) * sqrtN;
if (r <= bl || br <= l) continue;
if (l <= bl && br <= r) {
res = min(res, bucket[k]);
}
else {
foreach (i ; max(l, bl) .. min(r, br)) {
res = min(res, data[i]);
}
}
}
return res;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
/+ dub.sdl:
name "D"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n, m;
sc.read(n, m);
int[] perm;
sc.read(perm); perm[] -= 1;
int[][] g = new int[][n];
foreach (i; 0..m) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b;
g[b] ~= a;
}
int[] v;
bool[] vis = new bool[n];
void dfs(int p) {
if (vis[p]) return;
v ~= p;
vis[p] = true;
foreach (d; g[p]) {
dfs(d);
}
}
int ans = 0;
int[] buf = new int[n];
foreach (i; 0..n) {
if (vis[i]) continue;
v = [];
dfs(i);
int id = i + 114514;
foreach (d; v) {
buf[d] = id;
}
foreach (d; v) {
int e = perm[d];
if (buf[e] == id) ans++;
}
}
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/5;
enum long MOD = 10L^^9+7;
void main() {
writeln(6 - 2.rep!(() => readln.chomp.to!long).sum);
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (y > 0 && t * y < x) t++;
if (y < 0 && t * y > x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (y > 0 && t * y > x) t--;
if (y < 0 && t * y < x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
enum sep = " ";
enum n = Args.length;
enum fmt = n.rep!(()=>"%s").join(sep) ~ "\n";
static if (__VERSION__ >= 2071) {
readf!fmt(args);
} else {
enum argsTemp = n.iota.map!(
i => "&args[%d]".format(i)
).join(", ");
mixin(
"readf(fmt, " ~ argsTemp ~ ");"
);
}
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
import std.stdio, std.algorithm, std.array, std.range, std.string, std.conv, std.uni;
void main() {
auto s = readln.strip;
auto yy = s[0..2].to!int;
auto mm = s[2..$].to!int;
if (1 <= mm && mm <= 12) {
if (1 <= yy && yy <= 12) {
writeln("AMBIGUOUS");
} else {
writeln("YYMM");
}
} else {
if (1 <= yy && yy <= 12) {
writeln("MMYY");
} else {
writeln("NA");
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
int main(string[] argv)
{
auto s = split(readln());
int x = to!int(s[0]), y = to!int(s[1]);
writeln(x * y, ' ', (x + y) * 2);
return 0;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;
void main()
{
auto s = readln.chomp;
auto hi = new int[](s.length + 1);
foreach (i, c; s)
hi[i + 1] = hi[i] + c.predSwitch('\\', -1, '_', 0, '/', 1);
auto maxH = hi.reduce!(max);
auto fs1 = hi.findSplitBefore([maxH]), hiL = fs1[0], hiMR = fs1[1];
if (!hiL.empty) hiL ~= maxH;
hiMR.reverse();
auto fs2 = hiMR.findSplitBefore([maxH]), hiR = fs2[0], hiM = fs2[1];
if (!hiR.empty) hiR ~= maxH;
auto aiL = hiL.calcAreas;
auto aiM = hiM.calcAreas; aiM.reverse();
auto aiR = hiR.calcAreas; aiR.reverse();
auto ai = aiL ~ aiM ~ aiR;
writeln(ai.sum);
write(ai.length);
ai.each!(a => write(" ", a));
writeln;
}
int[] calcAreas(int[] hi)
{
int[] areas;
int maxH = int.min, area = 0;
foreach (i, h; hi) {
if (h < maxH) {
area += 2 * (maxH - h) + (hi[i - 1] - h).sgn;
} else if (h == maxH) {
if (area > 0) {
areas ~= area / 2;
area = 0;
}
} else {
maxH = h;
}
}
return areas;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nl = readln.split.to!(int[]);
auto N = nl[0];
long L = nl[1];
auto as = readln.split.to!(long[]);
foreach (i, a; as) {
if (i == 0) continue;
if (as[i-1] + a >= L) {
writeln("Possible");
foreach (j; 1..i) writeln(j);
foreach_reverse (j; i+1..N) writeln(j);
writeln(i);
return;
}
}
writeln("Impossible");
}
|
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;
void main()
{
int sum;
foreach (line; stdin.byLine) {
sum += line.chomp.to!int;
}
writeln(sum/60);
writeln(sum%60);
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
class UnionFind {
long[] data;
this (size_t size) {
data.length = size;
foreach (ref e; data) {
e = -1;
}
}
void unite(long x, long y) {
x = root(x);
y = root(y);
if (x != y) {
if (size(x) < size(y)) { swap(x, y); }
data[x] += data[y];
data[y] = x;
}
}
bool same(long x, long y) {
return root(x) == root(y);
}
long root(long x) {
return data[x] < 0 ? x : (data[x] = root(data[x]));
}
size_t size(long x) {
return -data[root(x)];
}
}
void main() {
int[] NM = readln.chomp.split.to!(int[]);
int N = NM[0],
M = NM[1];
int ans;
int[][] es;
foreach (_; 0..M) {
int[] ab = readln.chomp.split.to!(int[]);
int a = ab[0],
b = ab[1];
a--;
b--;
es ~= [a, b];
}
foreach (i, e; es) {
UnionFind uf = new UnionFind(N);
foreach (j, e2; es) {
if (i == j) { continue; }
uf.unite(e2[0], e2[1]);
}
if (uf.size(0) != N) {
ans++;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto n = readln.chomp;
if((n[0] == n[1] && n[1] == n[2])||(n[1] == n[2] && n[2] == n[3])){
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
alias Pair = Tuple!(int, "a", int, "b");
void main() {
int N;
scan(N);
auto ans = (N + 110) / 111 * 111;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import 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();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
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()
{
auto n = lread();
long[string] dict;
foreach (_; iota(n))
{
auto s = sread();
auto tmp = (cast(ubyte[]) s);
tmp.sort();
s = cast(string) tmp;
if (s in dict)
dict[s]++;
else
dict[s] = 1;
}
long ans;
foreach (e; dict)
if (e > 1)
ans += e * (e - 1) / 2;
ans.writeln();
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
int a, z;
foreach (i, e; s) {
if (e == 'A') {
a = i.to!int;
break;
}
}
foreach_reverse(i, e; s) {
if (e == 'Z') {
z = i.to!int;
break;
}
}
writeln(z - a + 1);
}
|
D
|
import std;
void main() {
long a; string b; scan(a, b);
int x = b[0] - '0';
int y = b[2] - '0';
int z = b[3] - '0';
long ans = a * 100 * x;
ans += a * 10 * y;
ans += a * z;
writeln(ans / 100);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
foreach (i ; 0 .. n) {
int yi, mi, di;
scan(yi, mi, di);
writeln(mill(yi, mi, di));
}
}
int mill(int y, int m, int d) {
int res;
if (y % 3 == 0) {
res += 10 * 20 - ((m - 1) * 20 + d) + 1;
}
else {
res += 5*19 + 5*20 - ((m - 1) / 2 * 39 + ((m-1)&1)*20 + d) + 1;
}
foreach (yy ; y + 1 .. 1000) {
if (yy % 3 == 0) res += 10*20;
else res += 5*39;
}
return res;
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.range;
import std.string;
import std.algorithm;
import std.conv;
void main() {
ulong x = readln.chomp.to!ulong;
(x/11*2 + (x%11 <= 6 ? 1 : 2) + (x%11 == 0 ? -1 : 0)).writeln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
char decode(char c, int n) {
while(n > 0) {
switch(c) {
case 'a':
c = 'Z';
break;
case 'A':
c = 'z';
break;
default:
--c;
}
--n;
}
return c;
}
void main(){
while(true) {
auto n = readInt;
if(n == 0) return;
auto k = readInts;
auto s = readln.chomp;
foreach(i, c; s) {
write(decode(c, k[i%n]));
}
writeln();
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto q = readln.chomp.to!size_t;
foreach (_; q.iota) {
auto x = readln.chomp, xl = x.length;
auto y = readln.chomp, yl = y.length;
auto memo = new int[][](xl + 1, yl + 1);
foreach (i; 1..xl+1)
foreach (j; 1..yl+1)
if (x[i-1] == y[j-1])
memo[i][j] = memo[i-1][j-1] + 1;
else
memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
writeln(memo[xl][yl]);
}
}
|
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;
}
}
// }}}
// 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);
bool[] s = new bool[n];
foreach (i; 0 .. k) {
int d;
cin.scan(d);
foreach (j; 0 .. d) {
int a = cin.next!int - 1;
s[a] = true;
}
}
writeln(s.filter!"a == false".array.length);
}
|
D
|
import std.algorithm,
std.string,
std.range,
std.stdio,
std.conv;
bool chmin(T)(ref T a, T b) {
if (b < a) {
a = b;
return true;
} else {
return false;
}
}
void chChar(ref string base, size_t idx, char c) {
char[] tmp = base.to!(char[]);
tmp[idx] = c;
base = cast(string)tmp;
}
void main() {
string Sd = readln.chomp.to!(char[]);
string T = readln.chomp.to!(char[]);
string ans;
foreach (i, s; Sd) {
string tmp = Sd;
bool match = true;
foreach (j, t; T) {
if (i + j >= Sd.length) {
match = false;
break;
}
if (Sd[i + j] == T[j] || Sd[i + j] == '?') {
chChar(tmp, i + j, T[j]);
} else {
match = false;
break;
}
}
if (!match) {
continue;
}
foreach (j, c; tmp) {
if (c == '?') {
chChar(tmp, j, 'a');
}
}
if (ans.empty) {
ans = tmp;
} else {
chmin(ans, tmp);
}
}
if (ans.empty) {
writeln("UNRESTORABLE");
} else {
writeln(ans);
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
int cnt;
foreach (i ; 0 .. n) {
string s = readln.chomp;
if (s == "A") cnt++;
else {
if (cnt <= 0) {
writeln("NO");
return;
}
else {
cnt--;
}
}
}
writeln(cnt == 0 ? "YES" : "NO");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.algorithm;
import std.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;
alias Key = Tuple!(int, "cost", int, "mask");
long calc(int num, Key[] keys) {
int n = cast(int)keys.length;
// dp[i 番目の鍵][宝箱の鍵(ビットマスク)]
auto dp = new long[][](n + 1, 1 << num);
for (int i = 0; i < dp.length; i++) dp[i][] = int.max;
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
int cost = keys[i].cost;
int mask = keys[i].mask;
for (int j = 0; j < (1 << num); j++) {
dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
dp[i+1][j | mask] = min(dp[i+1][j | mask], dp[i][j] + cost);
}
}
auto ans = dp[n][(1 << num) - 1];
return ans == int.max ? -1 : ans;
}
void main() {
int n, m; scan(n, m);
Key[] keys;
foreach (_; 0..m) {
int a, b; scan(a, b);
auto c = readints;
int mask = 0;
foreach (e; c) mask |= (1 << (e-1));
keys ~= Key(a, mask);
}
writeln(calc(n, keys));
}
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.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
string s = readln.chomp;
writeln(s.startsWith("YAKI") ? "Yes" : "No");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import 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;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
void main()
{
auto n = lread();
auto s = sread();
s.uniq.array.length.writeln();
}
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;
}
}
|
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 f(int n) {
if (n % 2 == 0) return n / 2;
return 3 * n + 1;
}
void main() {
int s = readint;
int[int] d;
d[s] = 1;
int prev = s;
for (int i = 2; ; i++) {
int x = f(prev);
if (x in d) {
writeln(i);
break;
}
else {
d[x] = i;
prev = x;
}
}
}
|
D
|
void main(){
int[] input;
foreach(i; 0..3)input ~= _scan();
( (input[0]-input[1])%input[2] ).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto d = readln.chomp.split.to!(int[]);
immutable mod = 998244353;
int[int] cnt;
foreach (i; 0..n) {
cnt[d[i]]++;
}
if (d[0] != 0 || cnt.get(0, 0) != 1) {
writeln(0);
return;
}
if (n == 1) {
writeln(1);
return;
}
long res = 1;
auto m = cnt.keys.reduce!(max);
auto pat = new long[](m+1);
pat[0..2] = 1;
foreach (i; 2..m+1) {
auto num = cnt.get(i, 0);
long tmp = cnt.get(i-1, 0);
foreach (_; 1..num) {
tmp = (tmp * cnt.get(i-1, 0)) % mod;
}
pat[i] = (pat[i-1] * tmp) % mod;
}
pat[$-1].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;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
auto nm = aryread;
auto n = nm[0];
auto m = nm[1];
long result = long.min;
for (long i = 1; i * i <= m; i++)
{
if (i <= m / n && m % i == 0)
{
result = result.max(i);
if (m / i <= m / n)
result = result.max(m / i);
}
}
writeln(result);
}
|
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; }
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 modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void main()
{
auto T = RD!int;
auto ans = new long[](T);
foreach (ti; 0..T)
{
auto n = RD;
auto d = RD;
long last = long.max, x;
foreach (i; 1..d+2)
{
auto y = d / i + (d % i != 0 ? 1 : 0);
if (y >= last)
{
x = i-1;
break;
}
last = y;
}
ans[ti] = d / x + (d % x != 0 ? 1 : 0) + x - 1 <= n;
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.math, std.algorithm, std.array, std.string, std.conv, std.container, std.range;
T[] Reads(T)() { return readln.split.to!(T[]); }
alias reads = Reads!int;
void scan(Args...)(ref Args args) {
string[] ss = readln.split;
foreach (i, ref arg ; args)
arg = ss[i].parse!int;
}
enum MOD = 10^^9 + 7;
pragma(inline, true)
int mul(long x, long y) {
return (x * (y % MOD)) % MOD;
}
int[] f;
void make_table(int n) {
f = new int[n + 1];
f[0] = f[1] = 1;
foreach (i ; 2..n+1)
f[i] = mul(i, f[i-1]);
}
int powmod(S,T,U)(S x, T n, U MOD) {
int r = 1;
while (n > 0) {
if (n & 1) r = mul(r, x);
x = mul(x, x);
n >>= 1;
}
return r;
}
pragma(inline, true)
int mod_inv(int x) {
return powmod(x, MOD - 2, MOD);
}
pragma(inline, true)
int comb(int n, int r) {
if (n - r < 0 || n < 0 || r < 0) return 0;
return mul(mul(f[n], f[r].mod_inv), f[n - r].mod_inv);
}
void main() {
int n, k;
scan(n, k);
make_table(2010);
foreach (i; 1..k+1) {
int x = mul(comb(n-k+1, i), comb(k-1, i-1));
writeln(x);
}
}
|
D
|
import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
auto buf = readln.chomp.split.to!(ulong[]);
buf[0].solve(buf[1]).writeln;
}
ulong solve(in ulong a, in ulong b)
{
assert (a <= b);
if (a == b)
return a;
if (a.even && b.odd)
return (~(b-a)>>1) & 1;
if (a.odd)
return a ^ solve(a + 1, b);
if (b.even)
return solve(a, b - 1) ^ b;
assert (false);
}
bool even(in ulong a)
{
return !(a & 1);
}
bool odd(in ulong a)
{
return a & 1;
}
|
D
|
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;
void main()
{
auto ab = readln.chomp.split().to!(long[]);
long a = ab[0];
long b = ab[1];
writeln(a * b - (a + b - 1));
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
while (1) {
int n;
scan(n);
if (!n) return;
auto ans = cntp(n);
writeln(ans);
}
}
int cntp(int n) {
auto s = new bool[](2*n + 1);
s[] = 1;
s[0] = s[1] = 0;
for (int p = 2; p*p <= 2*n; p++) {
if (s[p]) {
for (int q = p*p; q <= 2*n; q += p) {
s[q] = 0;
}
}
}
int ans;
foreach (i ; n + 1 .. 2*n + 1) {
ans += s[i];
}
return ans;
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.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.ascii;
void main()
{
auto x = readln.chomp.split;
auto a = x[0].to!int;
auto b = x[2].to!int;
if (x[1] == "+") {
writeln(a+b);
} else {
writeln(a-b);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
int n = readint;
int m = cast(int)sqrt(1.0 * n);
writeln(m * m);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto p = aryread();
long minimam = INF, cnt;
foreach (i; iota(n))
{
if(p[i] < minimam)
{
cnt++;
minimam = p[i];
}
}
cnt.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto n = RD;
auto a = RDA;
auto x = a[MIN_POS!"a > b"(a)];
/*long x = 1;
foreach (e; a)
x = lcm(x, e);*/
long y;
foreach (e; a)
y = gcd(y, x-e);
long z;
foreach (e; a)
z += (x-e) / y;
writeln(z, " ", y);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
immutable long MOD = 10^^9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto cnt = new int[](2);
foreach (s; S) cnt[s=='8'] += 1;
int ans = 0;
while (cnt[1]) {
cnt[1] -= 1;
int x = min(10, cnt[0]);
cnt[0] -= x;
int y = min(10-x, cnt[1]);
cnt[1] -= y;
if (x + y < 10) break;
ans += 1;
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
void main(string[] args)
{
auto t = next!int;
foreach(tc; 0 .. t)
{
auto a = next!long;
auto b = next!long;
auto c = next!long;
auto maxside = max(a, b, c);
writeln(maxside);
}
}
static this()
{
popChar;
}
struct Any(T)
{
static auto read()
{
static if (is(T == char))
{
auto res = frontChar;
popChar;
return res;
}
}
}
struct Unsigned(T)
{
auto read()
{
auto res = T(0);
while(!frontChar.isDigit) popChar;
do
{
res = res * T(10) + T(frontChar - '0');
popChar;
}
while(frontChar.isDigit);
return res;
}
}
struct Matrix(T, alias rows, alias cols)
{
T[][] _cell;
alias _cell this;
// Ring constructor
this(int n)
{
final switch(n)
{
case 0:
_cell = new T[][](rows, cols);
break;
case 1:
debug assert(rows == cols);
_cell = new T[][](rows, cols);
foreach(i; 0 .. rows)
_cell[i][i] = T(1);
break;
}
}
// Copy constructor
this(Matrix!(T, rows, cols) other)
{
_cell = new T[][](rows, cols);
foreach(i; 0 .. rows)
_cell[i][] = other[i][];
}
auto opBinary(string op)(Matrix!(T, rows, cols) other)
if (op == "+" || op == "-")
{
auto res = Matrix!(T, rows, cols)(0);
foreach(i; 0 .. rows)
foreach(j; 0 .. cols)
res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
return res;
}
auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
if (op == "*")
{
debug assert(cols == otherRows);
auto res = Matrix!(T, rows, otherCols)(0);
foreach(i; 0 .. rows)
foreach(k; 0 .. cols)
foreach(j; 0 .. otherCols)
res[i][j] += this[i][k] * other[k][j];
return res;
}
ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
{
static if (op == "*")
{
debug assert(rows == cols);
alias n = rows;
auto res = Matrix!(T, n, n)(0);
T factor;
foreach(i; 0 .. n)
foreach(k; 0 .. n)
foreach(j; 0 .. n)
res[i][j] += this[i][k] * other[k][j];
foreach(i; 0 .. n)
this[i][] = res[i][];
}
else
{
foreach(i; 0 .. rows)
foreach(j; 0 .. cols)
mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
}
return this;
}
mixin powMethod;
}
struct Mod(T, alias mod)
{
alias This = Mod!(T, mod);
T _rep;
this(T t)
{
_rep = t % mod;
if (_rep < 0) _rep += mod;
}
static auto plain(T t)
{
auto res = Mod!(T, mod)();
res._rep = t;
return res;
}
static auto fromPositive(T t)
{
pragma(inline, true);
return Mod!(T, mod).plain(t % mod);
}
static auto fromNegative(T t)
{
pragma(inline, true);
return Mod!(T, mod).plain(t % mod + mod);
}
string toString()
{
return to!string(_rep);
}
debug invariant
{
assert(_rep >= 0 && _rep < mod);
}
auto opBinary(string op)(This b)
{
static if (op == "+")
{
T resrep = _rep + b._rep;
if (resrep >= mod) resrep -= mod;
return This.plain(resrep);
}
else static if (op == "-")
{
T resrep = _rep - b._rep;
if (resrep < 0) resrep += mod;
return This.plain(resrep);
}
else static if (op == "*")
{
return This.fromPositive(_rep * b._rep);
}
}
auto opOpAssign(string op)(This b)
{
mixin(q{_rep }, text(op, "="), q{b._rep;});
static if (op == "+")
{
if (_rep >= mod) _rep -= mod;
}
else static if (op == "-")
{
if (_rep < 0) _rep += mod;
}
else static if (op == "*")
{
_rep %= mod;
}
return this;
}
auto opBinary(string op)(long exp)
if (op == "^^")
{
return pow(exp);
}
mixin powMethod;
}
mixin template powMethod()
{
auto pow(this ThisType)(long exp)
{
auto res = ThisType(1);
auto pow = ThisType(this);
while(exp)
{
if (exp & 1) res *= pow;
pow *= pow;
exp >>= 1;
}
return res;
}
}
// INPUT
char frontChar;
void popChar()
{
import core.stdc.stdio;
frontChar = cast(char) getchar;
if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
enum brackets = () {
string res;
foreach(i; 0 .. s.length)
res ~= "[]";
return res;
} ();
mixin(q{alias Type = T}, brackets, q{;});
return new Type(s);
}
template isNumberLike(T)
{
enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
import std.ascii;
while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
static foreach(ti; t)
{
static if (is(typeof(ti) == string))
{
write(ti, " ");
}
else static if (isArray!(typeof(ti)))
{
foreach(e; ti) print(e);
}
else static if (isTuple!(typeof(ti)))
{
static foreach(i; ti.length) writesp(ti[i]);
}
else
{
write(ti, " ");
}
}
}
void println(T...)(T t)
{
static if (t.length == 0)
writeln;
static foreach(ti; t)
{
print(ti);
writeln;
}
}
auto next(alias T, S...)(S s)
{
pragma(inline, true);
static if (s.length > 0)
{
auto res = makeArray!(typeof(next!T()))(s);
S t;
enum loops = () {
string res;
foreach(i; 0 .. s.length)
{
auto ti = text(q{t[}, i, q{]});
res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
}
return res;
} ();
enum indexing = () {
string res = "res";
foreach(i; 0 .. s.length)
res ~= text(q{[t[}, i, q{]]});
return res;
} ();
mixin(loops, indexing, q{ = next!T;});
return res;
}
else
{
static if (isNumberLike!T)
{
import std.ascii: isDigit;
T res;
while(frontChar.isWhite) popChar;
T multiplier = T(1);
if (frontChar == '-')
{
multiplier = T(-1);
popChar;
}
else if (frontChar == '+')
{
popChar;
}
debug assert(frontChar.isDigit);
do
{
res = res * T(10) + T(frontChar - '0');
popChar;
}
while(frontChar.isDigit);
return multiplier * res;
}
else static if (is(T == string))
{
import std.ascii: isWhite;
string res;
while(frontChar.isWhite)
popChar;
while(!frontChar.isWhite)
{
res ~= frontChar;
popChar;
}
return res;
}
else static if (is(T == char))
{
while(frontChar.isWhite) popChar;
auto res = frontChar;
popChar;
return res;
}
else
{
return T.read;
}
}
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void solve(){
long n = scan;
for(int i = 2; i < 2 + n; ++i){
write(i, " ");
}
writeln;
}
void main(){
long tests = scan; // Toggle!
while(tests--) solve;
}
/************** ***** That's All Folks! ***** **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
void main()
{
enum N = 14;
long[] a = readln.chomp.split.map!(to!long).array;
long ans = 0;
foreach (i, v; a) {
// simulate
long[] b = a.dup;
b[i] = 0;
foreach (j; 0..N) {
b[cast(uint)((i + j + 1) % N)] += v / N;
}
foreach (j; 0 .. v % N) {
++b[cast(uint)((i + j + 1) % N)];
}
long sub = 0;
foreach (w; b) {
if (w % 2 == 0) {
sub += w;
}
}
ans = max(ans, sub);
}
ans.writeln;
}
void tie(R, Args...)(R arr, ref Args args)
if (isRandomAccessRange!R || isArray!R)
in
{
assert (arr.length == args.length);
}
body
{
foreach (i, ref v; args) {
alias T = typeof(v);
v = arr[i].to!T;
}
}
void verbose(Args...)(in ref Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.writeln("]");
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
void main() {
auto n = readln.strip.to!int;
auto s = readln.strip[0 .. n];
auto psteps = (n - 11) >> 1;
auto t = s[0 .. n - 10];
int e;
foreach (i; 0 .. t.length) {
if (t[i] == '8') {
++e;
}
}
writeln (e > psteps ? "YES" : "NO");
}
|
D
|
import std.stdio;
import std.ascii;
import std.algorithm;
import core.stdc.stdio;
int main()
{
int t = readInt!int;
while (t--)
{
int n = readInt!int;
auto a = new int[](2 * n);
foreach(ref ai; a)
ai = readInt!int;
sort(a);
debug writeln(a);
auto low = a[0 .. n];
auto high = a[n .. $];
foreach(i; 0 .. n)
{
write(low[i], " ", high[i], " ");
}
writeln;
}
return 0;
}
/* INPUT ROUTINES */
int currChar;
static this()
{
currChar = getchar();
}
char topChar()
{
return cast(char) currChar;
}
void popChar()
{
currChar = getchar();
}
auto readInt(T)()
{
T num = 0;
int sgn = 0;
while (topChar.isWhite) popChar;
while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
if (sgn) return -num; return num;
}
string readString()
{
string res = "";
while (topChar.isWhite) popChar;
while (!topChar.isWhite) { res ~= topChar; popChar; }
return res;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto n = RD!int;
long x1, y1, x2 = 1, y2;
foreach (i; 0..n*n)
{
auto num = RD;
if (num != 1)
{
if (x1 < n && y1 < n)
{
writeln(1, " ", x1+1, " ", y1+1);
stdout.flush;
if (x1+2 < n)
x1 += 2;
else
{
++y1;
x1 = 1 - x1 % 2;
}
}
else
{
auto z = num == 2 ? 3 : 2;
writeln(z, " ", x2+1, " ", y2+1);
stdout.flush;
if (x2+2 < n)
x2 += 2;
else
{
++y2;
x2 = 1 - x2 % 2;
}
}
}
else
{
if (x2 < n && y2 < n)
{
writeln(2, " ", x2+1, " ", y2+1);
stdout.flush;
if (x2+2 < n)
x2 += 2;
else
{
++y2;
x2 = 1 - x2 % 2;
}
}
else
{
writeln(3, " ", x1+1, " ", y1+1);
stdout.flush;
if (x1+2 < n)
x1 += 2;
else
{
++y1;
x1 = 1 - x1 % 2;
}
}
}
}
debug readln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void 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 h, w; readV(h, w);
string[] c; readC(h, c);
foreach (ci; c) {
writeln(ci);
writeln(ci);
}
}
|
D
|
import std.array;
import std.conv;
import std.stdio;
import std.math;
void main() { readln.split[0].to!int.pow(2).writeln; }
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
auto s=readln.chomp.to!(char[]);
if(s.count("AC")) writeln("Yes");
else writeln("No");
}
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 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[] 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()
{
static immutable primes = () {
long[] primes;
long i;
while (primes.length < 55)
{
long x = 5 * i + 2;
if (factorize(x).length == 1)
primes ~= x;
i++;
}
return primes;
}();
long N = lread();
primes[0 .. N].map!text().join(' ').writeln();
}
long[] factorize(long x)
{
assert(0 < x, "x is negative");
long[] result;
while ((x & 1) == 0)
{
x /= 2;
result ~= 2;
}
for (long i = 3; i * i <= x; i += 2)
while (x % i == 0)
{
x /= i;
result ~= i;
}
if (x != 1)
result ~= x;
return result;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm;
import std.math :abs;
void main() {
int n;
scan(n);
int x, y, t;
foreach (i ; 0 .. n) {
int ti, xi, yi;
scan(ti, xi, yi);
int d = abs(xi - x) + abs(yi - y);
int e = ((x + y) % 2) ^ ((xi + yi) % 2);
if ((ti - t) < d || (ti - t) % 2 != e) {
writeln("No");
return;
}
x = xi, y = yi, t = ti;
}
writeln("Yes");
return;
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto M = RD;
auto b = new long[](N);
foreach (i; 0..M)
{
auto a = RD-1;
b[a] = 1;
}
auto dp = new long[](N+1);
dp[0] = 1;
if (b[0] == 0)
dp[1] = 1;
foreach (i; 1..N)
{
if (b[i] == 1) continue;
moda(dp[i+1], dp[i]);
moda(dp[i+1], dp[i-1]);
}
debug writeln(dp);
writeln(dp[N]);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
writeln(3 * readln.chomp.to!int ^^ 2);
}
|
D
|
import std.stdio, std.algorithm, std.string, std.range, std.array,std.conv;
void main() {
const tmp = readln.split.map!(to!int).array;
writeln(tmp[0] < tmp[1] ? 0:10);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.range;
void main(){
// input
int[] input;
input.length = 4;
foreach(ref i; input) {
i = to!int(readln().chomp());
}
immutable int five_hund = input[0];
immutable int hund = input[1];
immutable int fifty = input[2];
immutable int sum_num = input[3];
writeln(get_all_paterns(five_hund, hund, fifty, sum_num));
}
int get_all_paterns(int five_hund, int hund, int fifty, int sum_num){
int result;
foreach(fh_num; iota(0,five_hund+1)) {
foreach(h_num; iota(0, hund+1)) {
foreach(f_num; iota(0, fifty+1)) {
if(500 * fh_num + 100 * h_num + 50 * f_num == sum_num){
result++;
}
}
}
}
return result;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
const N = tmp[0], M = tmp[1];
auto as = readln.split.to!(long[]);
as = 0 ~ as;
foreach (i; 0..N) as[i+1] += as[i];
long[long] cnt;
foreach (a; as) {
const key = a % M;
if (key !in cnt) cnt[key] = 0;
cnt[key]++;
}
cnt.values.map!(c => c * (c-1)/2).sum.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto xy = reads!string;
auto x = xy[0], y = xy[1];
if (x == y) writeln('=');
else if (x < y) writeln('<');
else writeln('>');
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto s = readln.split;
auto N = s[0].to!int;
auto S = s[1].to!(dchar[]);
bool f(dchar[] T) {
foreach (i; 0..N) {
if (T[i] != 'B') continue;
for (int j = i+1, w = 0; j < N; ++j) {
if (T[j] == '.') break;
else if (T[j] == 'W') w += 1;
else if (T[j] == 'B') {
if (w > 0) return true;
else break;
}
}
}
return false;
}
int g(dchar[] T) {
int ans = 0;
foreach (i; 0..N) {
if (T[i] != 'W') continue;
for (int j = i+1, b = 0; j < N; ++j) {
if (T[j] == '.') break;
else if (T[j] == 'B') b += 1;
else if (T[j] == 'W') {
ans = max(ans, b);
break;
}
}
}
return ans;
}
int ans = 0;
foreach (i; 0..N) {
if (S[i] != '.') continue;
S[i] = 'W';
if (!f(S)) ans = max(ans, g(S));
S[i] = '.';
}
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abc = readln.split.to!(int[]);
auto A = abc[0];
auto B = abc[1];
auto C = abc[2];
writeln(max(0, C - A + B));
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
while(1){
auto n = readln.chomp.to!int;
int cnt;
if(n==0) break;
while(n!=1){
if(n%2==0)
n/=2;
else
n = n*3+1;
cnt++;
}
cnt.writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main()
{
int i = 0;
string str;
while ((str = readln().chomp()) != "0") {
i++;
writeln("Case ", i, ": ", str);
}
return 0;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
int n = to!int(readln().chomp());
int[] a = readln().split().map!(a => to!int(a)).array;
int m = int.min;
for (int l = 0; l < a.length; ++l)
{
for (int r = l + 1; r < a.length; ++r)
{
m = max(m, abs(a[l] - a[r]));
}
}
writeln(m);
}
|
D
|
import std;
void main(){
long a = readln.chomp.to!long - 1;
char[] ans;
while(a >= 0){
ans ~= 'a' + (a % 26);
a /= 26, a -= 1;
}
ans.reverse.writeln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const tmp = readln.split.to!(long[]);
[tmp[0]+tmp[1], tmp[0]-tmp[1], tmp[0]*tmp[1]].reduce!max.writeln;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.