code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
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 A, B;
scan(A, B);
long n = A + B;
writeln((n / 2) + (n & 1));
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto S = readln.chomp.map!(c => c.to!(int) - '0');
auto K = readln.chomp.to!long;
long pos = 1;
foreach(x; S) {
if (x == 1 && pos < K) {
pos++;
} else {
writeln(x);
return;
}
}
assert(false);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void main()
{
int x, y, z; readV(x, y, z);
writeln((x-z)/(y+z));
}
|
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 N = lread();
writeln(0);
stdout.flush();
auto s = sread();
if (s == "Vacant")
return;
long z = (s == "Male") ? 0 : 1;
long ok = 0;
long ng = N - 1;
while (1 < (ng - ok))
{
long x = (ok + ng) / 2;
writeln(x);
stdout.flush();
s = sread();
if (s == "Vacant")
return;
long g = (s == "Male") ? 0 : 1;
if ((x & 1) == (z ^ g))
{
ok = x;
}
else
{
ng = x;
}
}
writeln(ng);
stdout.flush();
s = sread();
if (s == "Vacant")
return;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b; readV(a, b);
writeln(a <= 8 && b <= 8 ? "Yay!" : ":(");
}
|
D
|
import std.stdio;
import std.string;
import std.range;
import std.algorithm;
void main() {
auto s = readln.chomp.retro.array;
auto cands = ["dream", "dreamer", "erase", "eraser"].map!(s => s.retro.array).array;
int idx = 0;
while(true) {
bool succ;
if (idx >= s.length-1) break;
foreach(cand; cands) {
if (idx+cand.length <= s.length && s[idx..idx+cand.length] == cand) {
idx += cand.length;
succ = true;
break;
}
}
if (!succ) {
"NO".write;
return;
}
}
"YES".write;
}
|
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[]);
auto D = tmp[0], N = tmp[1];
auto res = 100^^D;
if (N < 100) res *= N;
else res *= N+1;
writeln(res);
}
|
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()
{
char input;
scanf("%c", &input);
if(input == 'A')
{
writeln("T");
}
if(input == 'T')
{
writeln("A");
}
if(input == 'G')
{
writeln("C");
}
if(input == 'C')
{
writeln("G");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.conv;
void main() {
auto c = readMatrix!int(3, 3);
(check(c) ? "Yes" : "No").writeln;
}
bool check(int[][] a) {
return a[0][0] == a[0][1] + a[1][0] - a[1][1]
&& a[1][0] == a[1][1] + a[2][0] - a[2][1]
&& a[0][1] == a[0][2] + a[1][1] - a[1][2]
&& a[1][1] == a[1][2] + a[2][1] - a[2][2];
}
// ===================================
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 (isBasicType!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
int ri() {
return readAs!int;
}
|
D
|
import std.stdio;
void main() {
long n;
scanf("%ld", &n);
write((n*(n-1))/2);
}
|
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!long;
long r = long.max;
for (long i = 1; i^^2 <= N; ++i) {
if (N%i != 0) continue;
r = min(r, i-1 + N/i-1);
}
writeln(r);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
void main(){
while(true){
auto input = readln.split.map!(to!int);
int h = input[0], n = input[1];
if(n == 0 && h == 0) break;
int[int] cube;
foreach(i; 0..n){
auto line = readln.split;
int a = line[1].to!int - 1, b = line[2].to!int - 1;
final switch(line[0]){
case "xy":
foreach(z; 0..h){
cube[(a << 18) + (b << 9) + z] = 0;
}
break;
case "xz":
foreach(y; 0..h){
cube[(a << 18) + (y << 9) + b] = 0;
}
break;
case "yz":
foreach(x; 0..h){
cube[(x << 18) + (a << 9) + b] = 0;
}
break;
}
}
(h * h * h - cube.length).writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int h, w, n;
//scan(h, w, n);
scan(h);
scan(w);
scan(n);
h = max(h, w);
auto ans = (n + h - 1) / h;
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; // readln
import std.array; // split
import std.conv; // to
int[] dx = [1,1,1,0,-1,-1,-1,0];
int[] dy = [1,0,-1,-1,-1,0,1,1];
void main(){
string[] s = split(readln());
int h = to!int(s[0]);
int w = to!int(s[1]);
char[][] m = new char[][](h + 2,w + 2);
for(int i = 1; i < m.length - 1; i++){
string str = readln();
for(int j = 1; j < m[0].length - 1; j++){
m[i][j] = str[j - 1];
}
}
char[][] ans = new char[][](h + 2,w + 2);
for(int i = 0; i < m.length; i++){
ans[i][0] = '.';
ans[i][w + 1] = '.';
}
for(int i = 0; i < m[0].length; i++){
ans[0][i] = '.';
ans[h + 1][i] = '.';
}
for(int i = 1; i < m.length - 1; i++){
for(int j = 1; j < m[0].length - 1; j++){
if(m[i][j] == '#'){
ans[i][j] = '#';
}else{
int cnt = 0;
for(int k = 0; k < 8; k++){
if(m[i + dy[k]][j + dx[k]] == '#') cnt++;
}
ans[i][j] = to!char(cnt + 48);
}
}
}
for(int i = 1; i < m.length - 1; i++){
for(int j = 1; j < m[0].length - 1; j++){
write(ans[i][j]);
}
writeln("");
}
}
|
D
|
import std.stdio,std.string,std.conv;
void main(){
auto debt = 10_0000;
auto week = to!int( readln().chomp() ) ;
for( ; 0<week ; week-- ){
debt *= 1.05;
debt += (debt%1000)?1000-debt%1000:0;
}
writeln(debt);
}
|
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;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.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 main()
{
auto cin = new Scanner;
string s;
cin.scan(s);
writeln(s[0] ~ (s.length - 2).to!string ~ s[$ - 1]);
}
|
D
|
import std.stdio,std.string;
void main(){
char[] str2="".dup;
auto str = readln().chomp() ;
foreach( c ; str ){
str2 = c~str2;
}
writeln(str2);
}
|
D
|
import std.stdio;
import std.algorithm;
import core.stdc.stdio;
static immutable maxM = 5000;
struct t{
int idx;
int len;
}
class State{
int[] sizes;
t[][] data;
this(){
sizes = new int[3];
data = new t[][3];
t[] buf = new t[3*(maxM*2+1)];
for(int i=0;i<3;i++){
data[i] = buf[i*(maxM*2+1)..(i+1)*(maxM*2+1)];
}
}
void Clear(){
sizes[] = 0;
}
}
void main(){
State now = new State;
State next = new State;
while(true){
int n,m,p,q,r;
scanf("%d",&n);
if(n==0)
break;
scanf("%d%d%d%d",&m,&p,&q,&r);
p--;
now.Clear;
now.data[0][now.sizes[0]++] = t(1,n);
for(int _=0;_<m;_++){
next.Clear;
int x,y;
scanf("%d%d",&x,&y);
int len=0;
for(int i=0;i<3;i++){
for(int j=0;j<now.sizes[i];j++){
t a = now.data[i][j];
t b;
if(len < x){
if(len+a.len > x){
b.len = len+a.len-x;
a.len -= b.len;
b.idx = a.idx+a.len;
now.data[i][j] = b;
j--;
}
next.data[2][next.sizes[2]++] = a;
}else if(len < y){
if(len+a.len > y){
b.len = len+a.len-y;
a.len -= b.len;
b.idx = a.idx+a.len;
now.data[i][j] = b;
j--;
}
next.data[1][next.sizes[1]++] = a;
}else{
next.data[0][next.sizes[0]++] = a;
}
len += a.len;
}
}
swap(now,next);
}
int res=0;
int len=0;
for(int i=0;i<3;i++){
for(int j=0;j<now.sizes[i];j++){
t a = now.data[i][j];
t b;
if(len < p){
if(len+a.len > p){
b.len = len+a.len-p;
a.len -= b.len;
b.idx = a.idx+a.len;
now.data[i][j] = b;
j--;
}
}else if(len < q){
if(len+a.len > q){
a.len = q-len;
}
res += max(0,min(a.len,r+1-a.idx));
}
len += a.len;
}
}
printf("%d\n",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;
void main()
{
auto x = readln.chomp.split.to!(int[]);
if (abs(x[0] - x[2]) <= x[3] ||
(abs(x[0] - x[1]) <= x[3] && abs(x[1] - x[2]) <= x[3])) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=readln.split.to!(int[]);
auto freq=new int[](n+1);
int cnt=0;
foreach(e; a){
if(e>n) cnt++;
else freq[e]++;
}
for(int i=1; i<=n; i++){
if(i<freq[i]) cnt+=(freq[i]-i);
else if(i>freq[i]) cnt+=freq[i];
}
writeln(cnt);
}
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));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) stderr.write(e, " ");
stderr.writeln();
}
|
D
|
void main()
{
string[] tmp = rdRow!string;
string s = tmp[0], t = tmp[1];
writeln(t, s);
}
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, std.datetime;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
bool g(string s)
{
foreach (i; 0 .. s.length / 2)
if (s[i] != s[$ - i - 1])
return false;
// writeln(s);
return true;
}
bool f()
{
return g(S) && g(S[0 .. $ / 2]) && g(S[($ + 1) / 2 .. $]);
}
writeln(f() ? "Yes" : "No");
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;
void main ()
{
int n;
while (scanf (" %d", &n) > 0)
{
bool s = false;
foreach (i; 0..n)
{
foreach (j; 0..n)
{
int t;
scanf (" %d", &t);
if (i == j)
{
if (t)
{
s ^= true;
}
}
}
}
int q;
scanf (" %d", &q);
string res;
foreach (k; 0..q)
{
int t, i;
scanf (" %d", &t);
switch (t)
{
case 1:
case 2:
scanf (" %d", &i);
s ^= true;
break;
case 3:
res ~= s ? '1' : '0';
break;
default:
assert (false);
}
}
writeln (res);
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n; rd(n);
auto a=new int[](n);
foreach(i; 0..n) rd(a[i]), a[i]--;
int cur=0, cnt=0;
foreach(_; 0..n+1){
cur=a[cur];
cnt++;
if(cur==1){
writeln(cnt);
return;
}
}
writeln(-1);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string s1 = chomp(readln());
string s2 = chomp(readln());
string s3 = chomp(readln());
int a = to!int(s1); // 第0要素を整数に変換
int b = to!int(s2); // 第1要素を整数に変換
int c = to!int(s3); // 第1要素を整数に変換
writeln((a+b)*c/2); // 表示
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum P = 10L^^9+7;
void main()
{
auto N = readln.chomp.to!int;
auto DP = new long[][][][](4, 4, 4, N);
DP[0][0][0][0] = 1;
DP[0][0][1][0] = 1;
DP[0][0][2][0] = 1;
DP[0][0][3][0] = 1;
foreach (i; 1..N) {
foreach (a; 0..4) foreach (b; 0..4) foreach (c; 0..4) foreach (x; 0..4) {
if (i >= 2 && x == 1 && c == 2 && b == 0) continue;
if (i >= 2 && x == 1 && c == 0 && b == 2) continue;
if (i >= 2 && x == 2 && c == 1 && b == 0) continue;
if (i >= 3 && x == 1 && c == 2 && a == 0) continue;
if (i >= 3 && x == 1 && b == 2 && a == 0) continue;
(DP[b][c][x][i] += DP[a][b][c][i-1]) %= P;
}
}
long r;
foreach (a; 0..4) foreach (b; 0..4) foreach (c; 0..4) {
(r += DP[a][b][c][N-1]) %= P;
}
writeln(r);
}
|
D
|
void main(){
import std.stdio, std.algorithm;
long n, a, b, k; rd(n, a, b, k);
const long mod=998244353;
const int M=1_000_00*4;
long powmod(long a, long x, long mod){
if(x==0) return 1;
else if(x==1) return a;
else if(x&1) return a*powmod(a, x-1, mod)%mod;
else return powmod(a*a%mod, x/2, mod);
}
long[] genFact(int n, long mod){
auto fact=new long[](n);
fact[0]=fact[1]=1;
foreach(i; 2..n) fact[i]=i*fact[i-1]%mod;
return fact;
}
long[] genInv(long[] arr, long mod){
auto inv=new long[](arr.length);
foreach(i, elem; arr) inv[i]=powmod(arr[i], mod-2, mod);
return inv;
}
auto fact=genFact(M, mod);
auto invFact=genInv(fact, mod);
long comb(long nn, long rr){
if(nn<rr) return 0;
long ret=fact[nn]%mod;
(ret*=invFact[rr])%=mod;
(ret*=invFact[nn-rr])%=mod;
return ret;
}
long tot=0;
for(long x=0; x<=n; x++)if(k-a*x>=0){
if((k-a*x)%b==0){
long y=(k-a*x)/b;
if(y>n) continue;
(tot+=comb(n, x)*comb(n, y)%mod)%=mod;
}
}
writeln(tot);
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio;
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 n = readln.chomp.to!int;
writeln(n*800-(n/15)*200);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int t = read.to!int;
foreach(_; 0 .. t){
long a = read.to!long;
long b = read.to!long;
long n = read.to!long;
if(n % 3 == 0) a.writeln;
if(n % 3 == 1) b.writeln;
if(n % 3 == 2) (a ^b).writeln;
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.array;
void main(){
auto A = readln.chomp.to!(int);
auto B = readln.chomp.to!(int);
auto C = readln.chomp.to!(int);
auto D = readln.chomp.to!(int);
auto E = readln.chomp.to!(int);
int ans;
if(A<0){
ans += -A*C;
A=0;
}
if(A==0){
ans += D;
}
if(A>=0){
ans += (B-A)*E;
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
bool isRightTriangle(int a, int b, int c) {
int a2 = a * a;
int b2 = b * b;
int c2 = c * c;
return a2 + b2 == c2 || b2 + c2 == a2 || c2 + a2 == b2;
}
void main() {
uint n = to!uint(chomp(readln()));
int[][] data;
for(uint i = 0; i < n; i++) {
auto input = split(readln());
data ~= [to!int(input[0]), to!int(input[1]), to!int(input[2])];
}
foreach(tri; data) {
if(isRightTriangle(tri[0], tri[1], tri[2])) writeln("YES");
else writeln("NO");
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], d = tmp[1];
writeln((n + 2 * d) / (2 * d + 1));
}
|
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;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
int n = cin.next!int,
a = cin.next!int;
if (n % 500 <= a) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
void main() {
string[] tmp = readln.split('/');
string y = "2018", m = tmp[1], d = tmp[2];
writeln(y, '/', m, '/', d);
}
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;
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 A, B, C;
scan(A, B, C);
if (A == B)
{
C.writeln();
}
else if (A == C)
{
B.writeln();
}
else
{
A.writeln();
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[10^^5] AS;
void main()
{
auto N = readln.chomp.to!int;
long r;
foreach (i; 0..N) {
auto A = readln.chomp.to!long;
if (i != 0 && A > 0 && AS[i-1] > 0) {
--A;
++r;
}
r += A/2;
if (A%2 == 1) AS[i] = 1;
}
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
writeln(N <= K ? 1 : 0);
}
|
D
|
/+ dub.sdl:
name "A"
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);
long a, b, c, k;
sc.read(a, b, c, k);
if (k % 2 == 0) {
writeln(a-b);
} else {
writeln(b-a);
}
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
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(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
import std.exception;
enforce(readSingle(x));
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
read!enforceEOF(args);
}
}
void read(bool enforceEOF = false, Args...)(auto ref Args args) {
import std.exception;
static if (args.length == 0) {
enforce(enforceEOF == false || !succ());
} else {
enforce(readSingle(args[0]));
read!enforceEOF(args);
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/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);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void 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, m; readV(n, m);
int[] l, r; readC(n, l, r);
auto k = new int[][](m+1);
foreach (i; 0..n) k[r[i]-l[i]+1] ~= l[i];
auto ft = new FenwickTree!int(m+1), t = n;
foreach (d; 1..m+1) {
auto s = t;
for (auto i = d; i <= m; i += d) s += ft[0..i+1];
writeln(s);
foreach (ki; k[d]) {
++ft[ki];
--ft[ki+d];
}
t -= k[d].length;
}
}
class FenwickTree(T)
{
const size_t n;
T[] buf;
this(size_t n)
{
this.n = n;
this.buf = new T[](n+1);
}
void opIndexOpAssign(string op)(T val, size_t i) if (op == "+" || op == "-")
{
++i;
for (; i <= n; i += i & -i) mixin("buf[i] " ~ op ~ "= val;");
}
void opIndexUnary(string op)(size_t i) if (op == "++" || op == "--")
{
++i;
for (; i <= n; i += i & -i) mixin("buf[i]" ~ op ~ ";");
}
pure T opSlice(size_t r, size_t l) { return get(l) - get(r); }
pure T opIndex(size_t i) { return opSlice(i, i+1); }
pure size_t opDollar() { return n; }
private:
pure T get(size_t i)
{
auto s = T(0);
for (; i > 0; i -= i & -i) s += buf[i];
return s;
}
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto s = sread();
auto t = sread();
// writeln(s);
// writeln(t);
long ans = -1;
foreach (i; 0 .. n + 1)
{
if (func(s, t, i))
{
// writeln(s[($ - i) .. $], t[0 .. i]);
ans = max(ans, i);
}
}
// writeln(ans);
writeln((s.length + t.length) - ans);
}
bool func(string s, string t, long x)
{
if (s[($ - x) .. $] == t[0 .. x])
{
return true;
}
else
{
return false;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
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 n, k;
scan(n, k);
if (k > 1)
writeln(n - k);
else
writeln(0);
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
void main()
{
int n, k;
readln.chomp.split.tie(n, k);
long cnt = 0;
debug {
int ans = 0;
/+
for (int a = 1; a <= n; ++a) {
for (int b = 1; b <= n; ++b) {
if (a % b >= k) {
verbose(a, b);
++ans;
}
}
}
+/
for (int b = 1; b <= n; ++b) {
for (int a = b + 1; a <= n; ++a) {
if (a % b >= k) {
verbose(a, b, "...", a % b);
++ans;
}
}
}
for (int a = 1; a <= n; ++a) {
for (int b = a + 1; b <= n; ++b) {
if (a % b >= k) {
verbose(a, b, "...", a % b);
++ans;
}
}
}
verbose("ans", ans);
}
// 上 (a <= b)
for (int a = max(k, 1); a <= n; ++a) {
cnt += n - a;
}
debug verbose("ue", cnt);
// 下 (a > b)
for (int b = k + 1; b <= n; ++b) {
if (k == 0) {
cnt += n - b + 1;
} else {
debug verbose(b);
for (int d = 0; d < b - k; ++d) {
if (n < k + b + d) {
break;
}
int v = (n - (k + b + d)) / b + 1;
debug verbose("\t", d, v);
int w = v - 1;
w *= b;
int x = n - w - k - b;
debug verbose("\t\tx = ", x, n - (k + b + x));
x = min(x, b - k - 1);
debug verbose("\t\tx' = ", x);
cnt += (x - d + 1) * v;
d = x;
}
}
}
cnt.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 Args args)
{
stderr.write("[");
foreach (i, ref v; args) {
if (i) stderr.write(", ");
stderr.write(v);
}
stderr.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;
bool calc(int a, int b, int c) {
return min(a, b) < c && c < max(a, b);
}
void main() {
auto abc = readints;
int a = abc[0];
int b = abc[1];
int c = abc[2];
writeln(calc(a, b, c) ? "Yes" : "No");
}
|
D
|
string solve(string s){
return s.replace(',',' ');
}
void main(){
string s = readln().chomp();
solve(s).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!long);
auto N = s[0];
auto Q = s[1];
while (Q--) {
s = readln.split.map!(to!long);
auto r = s[0];
auto c = s[1];
if ((r + c) % 2 == 0) {
long odd = r / 2;
long even = r - 1 - odd;
long n = odd * (N / 2 + N % 2) + even * (N / 2);
writeln(n + (c - 1) / 2 + 1);
} else {
long odd = r / 2;
long even = r - 1 - odd;
long n = odd * (N / 2) + even * (N / 2 + N % 2);
n += N * N / 2 + N % 2;
writeln(n + (c - 1) / 2 + 1);
}
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long a, b;
scan(a, b);
writeln((a * b) % 2 ? "Odd" : "Even");
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
const mx=1_000_00+5;
auto isPrime=new bool[](mx);
auto sub=new int[](mx);
isPrime[2]=true;
for(int i=3; i<=1_000_00; i+=2){
for(int j=3; j*j<=i; j+=2){
if(i%j==0) goto hell;
}
isPrime[i]=true;
if(isPrime[(i+1)/2]) sub[i]=1;
hell:;
}
foreach(i; 1..mx) sub[i]+=sub[i-1];
int q; rd(q);
while(q--){
int l, r; rd(l, r);
writeln(sub[r]-sub[l-1]);
}
}
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));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
auto n = readln.chomp.to!int;
bool include3(int x) {
for (; x > 0; x /= 10)
if (x % 10 == 3) return true;
return false;
}
foreach (i; 1..n + 1)
if (i % 3 == 0 || include3(i)) write(" ", i);
writeln;
}
|
D
|
import std;
void main() {
readln;
auto S = readln.split[0].to!string;
auto N = S.length;
size_t[][char] rgb;
rgb['R'] = [], rgb['G'] = [], rgb['B'] = [];
foreach (n, c; S) {
rgb[c] ~= n;
}
size_t ans;
ans += rgb['R'].length * rgb['G'].length * rgb['B'].length;
// j - i = k - j
foreach (i; 0 .. N-2) {
foreach (k; iota(i+2, N, 2)) {
auto j = (k+i)/2;
bool[char] set;
set[S[i]] = true; set[S[j]] = true; set[S[k]] = true;
if (set.keys.length == 3) ans--;
}
}
writeln(ans);
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm;
void main() {
int[] a;
for (int i = 0; i < 4; i++) {
a ~= readln.chomp.to!(int);
}
writeln(min(a[0], a[1]) + min(a[2], a[3]));
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc176/tasks/abc176_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[] a = readln.split.map!(to!long).array;
long last = a[0];
long ans = 0;
for(int i = 1; i < n; i++)
if(a[i] < last)
ans += last - a[i];
else
last = a[i];
ans.writeln;
}
|
D
|
import std.algorithm, std.array, std.conv, std.stdio, std.string;
void main()
{
readln;
auto l = readln.chomp;
l.count("ABC").writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
void main() {
string s;
string op = "+";
int res = 0;
while(1) {
s = readln();
int a = to!int(chomp(s));
if(op[0] == '+')
res += a;
else if(op[0] == '-')
res -= a;
else if(op[0] == '*')
res *= a;
else
res /= a;
op = readln();
if(op[0] == '=')
break;
}
writeln(res);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto hmk = readln.split.to!(int[]);
auto h1 = hmk[0];
auto m1 = hmk[1];
auto h2 = hmk[2];
auto m2 = hmk[3];
auto K = hmk[4];
auto s = h1 * 60 + m1;
auto e = h2 * 60 + m2;
auto r = e - K - s;
writeln(r);
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int[] a = readln.chomp.split.to!(int[]);
if (a[0] < a[1]) {
"a < b".writeln;
} else if (a[0] > a[1]) {
"a > b".writeln;
} else {
"a == b".writeln;
}
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
uint figure(uint num) {
if(num == 0) return 0;
else return figure(num / 10) + 1;
}
void main() {
string[][] inputs;
while(true) {
auto s = readln();
if(stdin.eof()) break;
else inputs ~= split(s);
}
foreach(input; inputs) {
uint a = to!uint(input[0]);
uint b = to!uint(input[1]);
writeln(figure(a + b));
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto n = readln.chomp.to!int;
auto as = readln.split.to!(int[]);
int even_cnt;
int quart_cnt;
foreach (a; as) {
if (!(a&1)) {
++even_cnt;
if (!(a&2))
++quart_cnt;
}
}
if (quart_cnt >= (n - even_cnt) || (even_cnt == quart_cnt && (n - quart_cnt * 2) == 1))
writeln("Yes");
else
writeln("No");
}
|
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()
{
auto X = lread();
auto a = X == 7 || X == 5 || X == 3;
writeln(a ? "YES" : "NO");
}
|
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;
// }}}
// nep.scanner {{{
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.chomp.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 main()
{
auto cin = new Scanner;
char[] s;
cin.scan(s);
foreach (i; 0 .. 1 << s.length - 1)
{
int sum = s.front - '0';
char[] op;
foreach (j; 0 .. s.length - 1)
{
if ((i >> j) & 1)
{
op ~= '+';
sum += s[j + 1] - '0';
}
else
{
op ~= '-';
sum -= s[j + 1] - '0';
}
}
if (sum == 7)
{
s ~= '7';
op ~= '=';
foreach (j; 0 .. 9)
{
if (j & 1)
{
write(op[j / 2]);
}
else
{
write(s[j / 2]);
}
}
writeln;
break;
}
}
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] a = [1, 5, 10, 25];
int coin = 0;
foreach_reverse (x; a) {
coin += n / x;
n %= x;
}
coin.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;
void main() {
int a, b;
scan(a, b);
int n = b - a;
int ans = n * (n - 1) / 2 - a;
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string[] inputs = split(readln());
bool a = inputs[0] == "H";
bool b = inputs[1] == "H";
if(a == b) 'H'.writeln;
else 'D'.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() {
for (int i = 0; i < 3; i++) {
write(readln[i]);
}
writeln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
string s;
s = readln.chomp;
auto cnt = new int[](26);
foreach (ch ; s) {
cnt[ch - 'a']++;
}
int odd;
foreach (i ; 0 .. 26) {
if (cnt[i] & 1) odd++;
}
if (odd > 1) {
writeln(-1);
return;
}
auto c = new int[](26);
auto d = new int[][](26, 0);
auto b = new int[](s.length);
int mae = 0;
foreach (i, ch ; s) {
int j = ch - 'a';
if ((cnt[j] & 1) && c[j] == cnt[j] / 2) {
b[i] = s.length.to!int /2;
d[j] ~= b[i];
c[j]++;
continue;
}
if (c[j] < cnt[j] / 2) {
b[i] = mae++;
d[j] ~= b[i];
}
else {
b[i] = s.length.to!int - 1 - d[j][cnt[j] - 1 - c[j]];
}
c[j]++;
}
debug {
writeln(b);
}
auto ft = FenwickTree!(int)(s.length.to!int);
long ans;
foreach_reverse (bi ; b) {
ft.add(bi, 1);
ans += ft.sum(bi);
}
writeln(ans);
}
struct FenwickTree(T) {
private {
int _size;
T[] _data;
}
this(int N) {
_size = N;
_data = new T[](_size + 1);
}
void add(int i, T x) {
i++;
while (i <= _size) {
_data[i] += x;
i += i & (-i);
}
}
T sum(int r) {
T res = 0;
while (r > 0) {
res += _data[r];
r -= r & (-r);
}
return res;
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
auto DP = new long[](2019);
DP[0] = 1;
int n, d = 1;
foreach_reverse (c; S) {
n = ((c - '0').to!int * d + n) % 2019;
(d *= 10) %= 2019;
DP[n] += 1;
}
long r;
foreach (x; DP) r += x * (x-1) / 2;
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.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 = 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 cnt = new long[](N);
auto red = new bool[](N);
cnt[] = 1;
red[0] = true;
foreach (i; 0..M)
{
auto x = RD-1;
auto y = RD-1;
red[y] |= red[x];
--cnt[x];
++cnt[y];
if (cnt[x] == 0)
red[x] = false;
}
long ans;
foreach (i; 0..N)
{
if (red[i])
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
void main(){
int r, g, b;
scanf("%d %d %d", &r, &g, &b);
( (100*r+10*g+b)%4?"NO":"YES").writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;
void main() {
char[] s = readln.chomp.to!(char[]);
int k; scan(k);
foreach (i ; 0 .. s.length.to!int - 1) {
if (s[i] == 'a') continue;
int t = 26 - (s[i] - 'a');
if (t <= k) {
k -= t;
s[i] = 'a';
}
}
k %= 26;
s[$ - 1] = (s[$ - 1] - 'a' + k) % 26 + 'a';
writeln(s);
}
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.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
auto H = readln.split.to!(int[]);
int ans;
foreach (i, height; H) {
bool flag = true;
foreach (h; H[0..i]) if (h > height) flag = false;
if (flag) ++ans;
}
writeln(ans);
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n;
sc.read(n);
long[] a = new long[n], b = new long[n];
foreach (i; 0..n) {
sc.read(a[i], b[i]);
}
if (equal(a, b)) {
writeln(0);
return 0;
}
long ma = 10L^^15;
foreach (i; 0..n) {
if (a[i] > b[i]) ma = min(ma, b[i]);
}
writeln(a.sum - ma);
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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
writeln(N <= K ? 1 : 0);
}
|
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.map!(to!int);
auto N = s[0];
auto M = s[1];
auto ok = new bool[](N);
auto p = new int[](N);
auto ans = 0;
auto pena = 0;
foreach (_; 0..M) {
auto t = readln.split;
auto a = t[0].to!int - 1;
auto b = t[1];
if (ok[a]) continue;
if (b == "WA") {
p[a] += 1;
} else {
ans += 1;
pena += p[a];
ok[a] = true;
}
}
writeln(ans, " ", pena);
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int[3] cnt;
string s;
sc.read(s);
int n = s.length.to!int;
foreach (c; s) cnt[c - 'a']++;
bool solve(int a, int b) {
int[] cnt2 = cnt.dup;
foreach (i; 0..n) {
int c = 0;
if (c == a || c == b) c++;
if (c == a || c == b) c++;
if (cnt2[c] == 0) return false;
cnt2[c]--;
a = b;
b = c;
}
cnt = cnt2;
return true;
}
foreach (i; 0..3) foreach (j; 0..3) {
if (solve(i, j)) {
writeln("YES");
return 0;
}
}
writeln("NO");
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.array;
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 {
FastAppender!(E[]) buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;
T[N] fixed(T, size_t N)(T[N] a) {return a;}
struct FastAppender(A, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private alias T = ElementEncodingType!A;
private T* _data;
private uint len, cap;
@property size_t length() const {return len;}
bool empty() const { return len == 0; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
ref inout(T) back() inout { assert(len); return _data[len-1]; }
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.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 core.bitop : popcnt;
static if (!__traits(compiles, popcnt(ulong.max))) {
public import core.bitop : popcnt;
int popcnt(ulong v) {
return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32));
}
}
bool poppar(ulong v) {
v^=v>>1;
v^=v>>2;
v&=0x1111111111111111UL;
v*=0x1111111111111111UL;
return ((v>>60) & 1) != 0;
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, k; readV(n, k);
writeln(k*(k-1)^^(n-1));
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string s; readV(s);
writeln(s.count('o') * 100 + 700);
}
|
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 w = readln.chomp;
int[char] cnt;
foreach (e; w) {
cnt[e]++;
}
auto f = false;
foreach (k; cnt.keys) {
if (cnt[k] % 2) {
f = true;
}
}
if (f) writeln("No");
else writeln("Yes");
}
|
D
|
import std.stdio,std.math,std.string,std.conv,std.typecons,std.format;
import std.algorithm,std.range,std.array;
T[] readarr(T=long)(){return readln.chomp.split.to!(T[]);}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
//END OF TEMPLATE
void main(){
ulong n,k;
scan(n,k);
ulong x;
if(n<k)
x=min(n,k-n);
else if(k==n)
x=0;
else{
x=min(n%k,abs((n%k).to!long-k.to!long));
}
x.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.ascii;
void main()
{
auto n = readln.chomp.map!(a=>a-'0').array;
auto res = n.sum;
int lsum;
foreach (i; 0..n.length) {
if (i == 0) {
if (n[i] != 1) {
lsum += n[i] - 1;
}
} else {
lsum += 9;
}
}
max(res, lsum).writeln;
}
|
D
|
import std.stdio, std.string, std.array, std.conv;
void main() {
int n = readln.chomp.to!int;
int[] a = readln.chomp.split.to!(int[]);
int q = readln.chomp.to!int;
int[] m = readln.chomp.split.to!(int[]);
bool[] check = new bool[2001];
foreach (i; 0 .. 1<<n) {
int sum = 0;
foreach (j; 0 .. n) {
if (i & 1<<j) sum += a[j];
}
check[sum] = true;
}
foreach (x; m) {
writeln(check[x] ? "yes" : "no");
}
}
|
D
|
import std.conv,std.string,std.stdio;
void main(){
immutable x=readln().chomp().to!int();
writeln(x*x*x);
}
|
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 x = readln.chomp.split.to!(int[]);
auto s = readln.chomp;
auto ab = x[1] + x[2];
auto b = x[2];
foreach (e; s) {
auto f = false;
if (e == 'a' && ab) {
ab--;
f = true;
} else if (e == 'b' && ab && b) {
ab--;
b--;
f = true;
}
writeln(f ? "Yes" : "No");
}
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a,b;
scan(a,b);
if(a <= 5)
writeln(0);
else if(a <= 12)
writeln(b / 2);
else
writeln(b);
}
|
D
|
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range, std.functional;
void main() {
auto tmp = readln.split.to!(int[]);
writeln(abs(tmp[0] - tmp[1]) < abs(tmp[0] - tmp[2]) ? "A" : "B");
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
int n, q, com, x, y;
void main() {
scan(n, q);
auto sg = SegTree(n);
while (q--) {
scan(com, x, y);
if (com == 0) {
sg.update(x, y);
}
else {
int ans = sg.getmin(x, y + 1);
writeln(ans);
}
}
}
struct SegTree {
private {
int N = 1;
int[] data;
immutable inf = int.max;
}
this(int n) {
while (N < n) N *= 2;
data = new int[](2*N - 1);
data[] = inf;
}
void update(int i, int x) {
i += N - 1;
data[i] = x;
while (i > 0) {
i = (i - 1) / 2;
data[i] = min(data[2*i + 1], data[2*i + 2]);
}
}
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0) r = N;
if (b <= l || r <= a) return inf;
if (a <= l && r <= b) return data[k];
int v1 = getmin(a, b, 2*k + 1, l, l + (r - l) / 2);
int v2 = getmin(a, b, 2*k + 2, l + (r - l) / 2, r);
return min(v1, v2);
}
}
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
|
void main()
{
long k = rdElem;
long a, b;
rdVals(a, b);
foreach (i; a .. b+1)
{
if (i % k == 0)
{
"OK".writeln;
return;
}
}
"NG".writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
string[] input = split(readln());
int x = to!int(input[0]);
int y = to!int(input[1]);
writeln(x * y, ' ', 2 * (x + y));
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable mod = 10^^9 + 7;
void main() {
int n, m;
scan(n, m);
auto adj = new bool[][](n, n);
foreach (i ; 0 .. m) {
int ai, bi;
scan(ai, bi);
ai--; bi--;
adj[ai][bi] = adj[bi][ai] = 1;
}
int ans;
void dfs(bool[] visited, int node) {
visited[node] = 1;
foreach (i ; 0 .. n) {
if (adj[node][i] && !visited[i]) {
dfs(visited, i);
}
}
}
foreach (i ; 0 .. n) {
foreach (j ; i + 1 .. n) {
if (adj[i][j]) {
adj[i][j] = adj[j][i] = 0;
auto visited = new bool[](n);
dfs(visited, 0);
ans += !all(visited);
adj[i][j] = adj[j][i] = 1;
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
auto a = readln.chomp.to!int;
auto b = a % 10;
auto c = a / 10 % 10;
auto d = a / 100 % 10;
auto e = a /1000 % 10;
writeln(b==c&&c==d||c==d&&d==e ? "Yes":"No");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, t; readV(n, t);
int[] w, v; readC(n, w, v);
auto dp = new long[][](n+1, t+1);
foreach (ref dpi; dp) dpi[] = -1;
dp[0][0] = 0;
foreach (i; 0..n)
foreach (j; 0..t+1) {
dp[i+1][j] = dp[i][j];
if (j >= w[i] && dp[i][j-w[i]] >= 0)
dp[i+1][j] = max(dp[i+1][j], dp[i][j-w[i]] + v[i]);
}
writeln(dp[$-1].reduce!max);
}
|
D
|
import std.stdio, std.conv, std.string;
void main() {
uint n = readln.strip.to!int;
writeln(n & 1 ? n / 2 + 1 : n / 2);
}
|
D
|
import std.string;
import std.stdio;
import std.conv;
import std.algorithm;
import std.typecons;
void main() {
int n, q;
int[][int] map;
scanf("%d %d\n", &n, &q);
int[] cnts, checks;
cnts = new int[n+1];
checks = new int[n+1];
foreach(i;0..n-1) {
int a, b;
scanf("%d %d\n", &a, &b);
map[a] ~= b;
}
foreach(i;0..q) {
int p, x;
scanf("%d %d\n", &p, &x);
checks[p] += x;
}
int[] que = [1];
for(size_t i; i < que.length; ++i) {
auto v = que[i];
cnts[v] += checks[v];
if (v in map) {
foreach(child; map[v]) {
cnts[child] += cnts[v];
que ~= child;
}
}
}
foreach (v; cnts[1..$]) {
write(v, " ");
}
}
|
D
|
void main()
{
dchar[] s = rdDchar;
dchar[] t = rdDchar;
long pos = t.length - 1;
bool ok;
foreach_reverse (long i, x; s)
{
if (x == t[pos] || x == '?')
{
bool check;
if (i - pos >= 0)
{
check = true;
foreach (j; 0 .. pos)
{
if (s[i-pos+j] != t[j] && s[i-pos+j] != '?')
{
check = false;
break;
}
}
}
if (check)
{
ok = true;
foreach (j; 0 .. pos+1)
{
s[i-pos+j] = t[j];
}
foreach (j; 0 .. s.length)
{
if (s[j] == '?') s[j] = 'a';
}
break;
}
}
}
writeln(ok ? s.to!string : "UNRESTORABLE");
}
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;
import std.algorithm;
import std.string;
import std.conv;
void main() {
foreach (i; 0..7) {
auto a = readln.split.map!(to!int);
writeln(a[0] - a[1]);
}
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
void main()
{
int[] t = map!(to!int)(split(readln())).array;
int[] checker = [0,0];
for(int i=0;i < 3;i ++)
{
if(t[i] == 5)
{
checker[0] ++;
}
else
{
checker[1] ++;
}
}
if(checker[0] == 2 && checker[1] == 1)
{
writeln("YES");
}
else
{
writeln("NO");
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
// dfmt on
void main()
{
long N = lread();
auto S = sread();
long K = lread();
char C = S[K - 1];
foreach (c; S)
{
write(c == C ? C : '*');
}
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.string, core.stdc.stdlib;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp.map!(c => c.to!int).array;
S.uniq.array.length.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
void main() {
auto input = map!(to!int)(readln.split);
int N = input[0], T = input[1], H = input[2], L = input[3];
Tuple!(int, int)[] data;
foreach (i; 0..N) {
input = map!(to!int)(readln.split);
auto t = input[0], h = input[1];
data ~= tuple(t, h);
}
int i = 0;
int remain = 0;
while (true) {
if (data[i][0] > 0) {
T++;
data[i][0]--;
remain += 10;
if (T > L){
break;
}
} else if(data[i][1] > 0) {
H++;
data[i][1]--;
remain += 100;
} else {
break;
}
if (remain >= 90) {
remain -= 90;
if (remain <= 10 * T) {
data[i][0] += remain / 10;
T -= remain / 10;
} else if ((remain % 100) <= 10 * T && remain / 100 <= H) {
data[i][1] += remain / 100;
H -= remain / 100;
remain %= 100;
data[i][0] += remain / 10;
T -= remain / 10;
} else {
break;
}
remain = 0;
}
i++;
if (i == N) i = 0;
}
writeln(i+1);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main(){
auto a=readln.chomp.to!int;
auto b=readln.chomp.to!int;
auto c=readln.chomp.to!int;
writeln((a-b)%c);
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
static import std.ascii;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] 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, K;
scan(N, K);
foreach (i; 1 .. K + 1)
{
assert(N - K + 1 <= i);
long a = combination_mod(N - K + 1, i, MOD);
long b = combination_mod(K - 1, i - 1, MOD);
writeln((a * b) % MOD);
}
}
T combination_mod(T = long)(T n, T k, T m)
{
if (n < k)
return 0;
long a = 1;
long b = 1;
long c = 1;
foreach (i; 1 .. n + 1)
{
a = (a * i) % m;
if (i == k)
b = a;
if (i == (n - k))
c = a;
}
T powmod(T = long)(T x, T n, T m)
{
if (n < 1)
return 1;
if (n & 1)
{
return x * powmod(x, n - 1, m) % m;
}
T tmp = powmod(x, n / 2, m);
return tmp * tmp % m;
}
long bc = b * c % m;
long bc_inv = powmod(bc, m - 2, m);
return a * bc_inv % m;
}
|
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 A = tmp[0], B = tmp[1];
// f(A,B) = AからBまでのすべての整数のxor
// 各bitでもとめる
// 1bit目はAからBまでのうち1のbitが奇数個なら1、偶数個なら0を返す
// これは交互に来るから、結局(B-A+1)が奇数個かどうかで決まる
// 2bit目はAからBまでのうち1のbitが奇数個なら1、偶数個なら0を返す
// これは2個ずつ飛ばしで来るから、一般に
// Nの2bit目が1であるとは、N%4が2か3であることを意味する
// 一般にnbit目が1であるとは、N%(2^^n)が2^^(n-1)以上であることを意味する
// 各桁を求めるにあたって、A%(2^^n) = a, B%(2^^n) = bであるとき、
// aからはじめて2^^n個のセットは打ち消し合うので、結局そこ以降のxorをとればいい。
long result;
foreach (n; 1..64) {
// A = (2^^n) * a + x
// B = (2^^n) * b + y
// C = (2^^n) * (a+1)
// D = (2^^n) * b
// [C,D)の区間には(D-C)/2個の1がある。
// [A,C)の区間にはmax(2^^n-x, 2^^(n-1))個の1がある
// [D,B]の区間にはmax(0, y-2^^(n-1)+1)個の1がある
auto a = A / (1L << n);
auto b = B / (1L << n);
auto x = A % (1L << n);
auto y = B % (1L << n);
auto C = (1L << n) * (a+1L);
auto D = (1L << n) * b;
long cnt;
cnt += (D-C)/2;
cnt += min((1L << n)-x, (1L << (n-1L)));
cnt += max(0, y-(1L << (n-1L))+1L);
if (cnt % 2 == 1L) result |= (1L << (n-1L));
}
writeln(result);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.