code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio;
import std.string;
import std.ascii;
void main() {
string s = readln.chomp;
writeln( (s.length >= 6 && s.count!(isDigit) >= 1 && s.count!(isUpper) >= 1 && s.count!(isLower) >= 1) ? "VALID" : "INVALID" );
}
|
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() {
int N;
int[] a;
scan(N);
a = readln.split.to!(int[]);
long x = 0, y = a.sum(0L);
long ans = long.max;
foreach (i ; 0 .. N - 1) {
x += a[i], y -= a[i];
ans = min(ans, abs(x - y));
}
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
|
void main()
{
long n = readln.chomp.to!long;
if (n & 1)
{
0.writeln;
}
else
{
long cnt;
long d = 10;
while (d <= n)
{
cnt += n / d;
d *= 5;
}
cnt.writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main() {
auto S = rs;
ulong res = ulong.max;
foreach(i; 0..S.length - 2) {
auto a = S[i..i + 3];
res = min(res, abs(753 - a.to!int));
}
res.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
long P = 10^^9+7;
long[2001] F, RF;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
long inv(long x)
{
return pow(x, P-2);
}
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long comb(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n]; // n!
auto nk_b = RF[n-k]; // 1 / (n-k)!
auto k_b = RF[k]; // 1 / k!
auto nk_b_k_b = (nk_b * k_b) % P; // 1 / (n-k)!k!
return (n_b * nk_b_k_b) % P; // n! / (n-k)!k!
}
long perm(N)(N n, N k)
{
if (k > n) return 0;
auto n_b = F[n];
auto n_k_b = RF[n-k];
return (n_b * n_k_b) % P;
}
void main()
{
init();
auto nk = readln.split.to!(long[]);
auto N = nk[0];
auto K = nk[1];
writeln(comb(K, N));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;
void main()
{
auto s = readln.chomp.to!(dchar[]);
sort(s);
s = s.uniq.array;
if (s[0] != 'a') {
writeln("a");
return;
}
foreach (i, c; s[0..$-1]) {
if (s[i+1] - c != 1) {
writeln((c + 1).to!char);
return;
}
}
if (s[$-1] != 'z') {
writeln("z");
return;
}
writeln("None");
}
|
D
|
void main()
{
long n = rdElem;
Train[] train = rdCol!Train(n-1);
long[] list = new long[n];
foreach (i; 0 .. n-1)
{
foreach (t; train[i..$])
{
list[i] = t.c + t.s + max(0, (list[i]-t.s+t.f-1)/t.f) * t.f;
}
}
foreach (l; list)
{
l.writeln;
}
}
struct Train
{
long c, s, f;
}
enum long mod = 10L^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import 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()
{
auto s = sread();
long first_C, first_F;
foreach (i,e; s)
{
if(!first_C && e == 'C')
first_C = i + 1;
if(e == 'F')
first_F = i + 1;
}
if(first_C && first_F)
{
if(first_C < first_F)
{
writeln("Yes");
return;
}
}
writeln("No");
}
|
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[][](2, 2019);
long r;
foreach (i, c; S) {
auto n = c - '0';
foreach (x; 0..2019) DP[i%2][x] = 0;
DP[i%2][n] = 1;
foreach (x; 0..2019) DP[i%2][(x * 10 + n) % 2019] += DP[(i+1)%2][x];
r += DP[i%2][0];
}
writeln(r);
}
|
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 MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
ulong MIDDLE = 100_000;
alias Pair = Tuple!(long, "flag", long, "num");
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, m;
scan(n, m);
auto box = new long[](n);
auto red = new long[](n);
box[] = 1;
red[0] = 1;
foreach (i; iota(m))
{
long x, y;
scan(x, y);
x--, y--;
if (box[x])
{
box[x]--, box[y]++;
if(red[x])
red[y] = 1;
if (!box[x])
red[x] = 0;
}
// box.writeln();
// red.writeln();
}
red.sum.writeln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm;
void main() {
int n;
int[] a;
scan(n);
a = iota(n).map!(i => readln.chomp.to!int).array;
auto dp = repeat(10^^9 + 7, n + 2).array.assumeSorted;
dp[0] = -1;
int ans = 1;
foreach (i ; 0 .. n) {
auto j = dp.lowerBound(a[i]).length.to!int;
dp[j] = a[i];
ans = max(ans, j);
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.array : split;
import std.conv : to;
import std.range.primitives;
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
int main()
{
int i = 0;
while (++i <= 1000)
writeln("Hello World");
return 0;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.container;
import std.array;
import std.math;
import std.range;
import std.typecons;
import std.ascii;
void main()
{
writeln = readln.chomp.split.join.to!int % 4 ? "NO" : "YES";
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(int[]);
auto BS = readln.split.to!(int[]);
auto CS = readln.split.to!(int[]);
int r;
foreach (i; 0..N) {
auto x = AS[i] - 1;
r += BS[x];
if (i < N-1 && AS[i+1] == x+2) r += CS[x];
}
writeln(r);
}
|
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()
{
writeln(scanString.count!"a=='1'");
}
|
D
|
void main() {
auto S = rs;
bool flag = true;
if(S[0] != 'A') flag = false;
ulong tmp;
foreach(i; 2..S.length-1) {
tmp += S[i] == 'C';
}
if(flag) {
ulong tmp2;
foreach(i; S) {
if(i.isUpper) tmp2++;
}
if(tmp2!=2) flag = false;
}
if(tmp != 1) flag = false;
writeln(flag ? "AC" : "WA");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Clue = Tuple!(long, "x", long, "y", long, "h");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
long cnt;
void main()
{
long n, m;
scan(n, m);
auto graph = new long[][](n, 0);
foreach (_; iota(m))
{
long a, b;
scan(a, b);
a--, b--;
graph[a] ~= b;
graph[b] ~= a;
}
auto visit = new bool[](n);
visit[0] = true;
dfs(graph, 0, visit);
cnt.writeln();
}
void dfs(T)(T g, long current, bool[] visit)
{
// current.writeln();
foreach (e; g[current])
{
if (!visit[e])
{
visit[e] = true;
dfs(g, e, visit);
visit[e] = false;
}
}
if (visit.all_true())
cnt++;
}
bool all_true(bool[] a)
{
bool ret = true;
foreach (e; a)
ret &= e;
return ret;
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
int[][][] MEMO;
MEMO.length = N;
foreach (i; 0..N) {
auto A = readln.chomp.to!int;
MEMO[i].length = A;
foreach (j; 0..A) {
auto xy = readln.split.to!(int[]);
xy[0] -= 1;
MEMO[i][j] = xy;
}
}
int solve(int i, uint s) {
if (i == N) {
int c;
foreach (j; 0..N) if (s & (1<<j)) {
++c;
foreach (xy; MEMO[j]) {
auto x = xy[0];
auto y = xy[1];
if (!!(s & (1<<x)) != !!y) return 0;
}
}
return c;
} else {
return max(solve(i+1, s), solve(i+1, s | (1<<i)));
}
}
writeln(solve(0, 0));
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.range;
int n, m, t; rd(n, m, t);
auto r=readln.split.to!(int[]);
auto d=new long[][](n, n);
const long inf=1_000_000_000_000_000_000;
foreach(i; 0..n) fill(d[i], inf);
foreach(i; 0..n) d[i][i]=0;
foreach(_; 0..m){
int a, b; long c; rd(a, b, c);
a--; b--;
d[a][b]=d[b][a]=c;
}
foreach(k; 0..n){
foreach(i; 0..n){
foreach(j; 0..n){
d[i][j]=min(d[i][j], d[i][k]+d[k][j]);
}
}
}
auto v=new int[](0);
auto usd=new bool[](t);
long f(int k){
if(k==t){
long ret=0;
foreach(i; 1..v.length){
ret+=d[v[i-1]][v[i]];
}
return ret;
}else{
long ret=inf;
foreach(i; 0..t)if(!usd[i]){
v~=(r[i]-1); usd[i]=true;
ret=min(ret, f(k+1));
v.popBack; usd[i]=false;
}
return ret;
}
}
writeln(f(0));
}
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, 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;
/*if (N % 2 == 0)
{
writeln(N/2, " ", N, " ", N);
}
else*/
{
long h, n, w;
(){
foreach (i; 1..3501)
{
foreach (j; 1..3501)
{
auto y = (4*i*j - N*j - N*i);
if (y <= 0.0) continue;
auto z = cast(double)(N*i*j) / y;
if (z < 1.0) continue;
if (z == cast(double)cast(long)z)
{
h = i;
n = j;
w = cast(long)z;
return;
}
}
}}();
writeln(h, " ", n, " ", w);
}
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
string str;
string[] input = split(readln());
int a = to!int(input[0]), b = to!int(input[1]);
if(a > b)
str = " > ";
else if(a < b)
str = " < ";
else
str = " == ";
writeln('a',str,'b');
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.conv;
void main()
{
int t;
scanf("%d", &t);
getchar();
foreach(_; 0..t)
{
auto str = readln.strip;
int zero_cnt, five_cnt;
bool zero_f = false, five_f = false;
for(int i = cast (int) str.length - 1; i > 0; i--)
{
if (str[i] == '0')
{
int j = i - 1;
while((str[j] != '0') && (str[j] != '5') && (!zero_f))
{
zero_cnt++;
j--;
if (j == -1)
break;
}
if (j >= 0)
zero_f = true;
if (!five_f)
five_cnt++;
}
else if (str[i] == '5')
{
int j = i - 1;
while((str[j] != '2') && (str[j] != '7') && (!five_f))
{
five_cnt++;
j--;
if (j == -1)
break;
}
if (j >= 0)
five_f = true;
if (!zero_f)
zero_cnt++;
}
else
{
if (!zero_f)
zero_cnt++;
if (!five_f)
five_cnt++;
}
}
writeln(min(zero_cnt, five_cnt));
}
}
|
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 ip = readAs!(int[]), N = ip[0], K = ip[1];
(K * ((K - 1) ^^ (N - 1))).writeln;
}
// ===================================
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;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.math;
bool check(string s){
for(int i=0;i<s.length-3;i++){
string str = s[i..i+4];
if(str == "this" || str == "that"){
writeln(s);
return true;
}
}
return false;
}
void main(){
int n = to!int(chomp(readln()));
for(int i=0;i<n;i++){
string ans;
auto s = chomp(readln());
int[] ss;
for(int a=1;a<=27;a++){
bool flg = false;
for(int b=0;b<=26;b++){
flg = false;
ans = "";
for(int j=0;j<s.length;j++){
int c = to!int(s[j]-'a');
if(0 <= c && c <= 25){
while((c-b+26)%a!=0){
c += 26;
if(c>1000) break;
}
c = (c-b+26)/a;
c %= 26;
}
ans ~= to!char(c +to!int('a'));
}
flg = check(ans);
if(flg) break;
}
if(flg){
break;
}
}
}
}
|
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[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N, M;
scan(N, M);
auto G = new long[][](N);
foreach (_; 0 .. M)
{
long a, b;
scan(a, b);
a--, b--;
G[a] ~= b;
G[b] ~= a;
}
foreach (v; G[0])
{
foreach (w; G[v])
if (w == N - 1)
{
writeln("POSSIBLE");
return;
}
}
writeln("IMPOSSIBLE");
}
|
D
|
void main() {
problem();
}
void problem() {
auto N = scan!long;
const K = scan!long;
long solve() {
while(true) {
real ratio = cast(real)N / cast(real)K;
if (ratio <= 0.5) return N;
if (ratio < 1.0) return K - N;
N = N - (N/K)*K;
}
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio,std.string,std.algorithm,std.conv;
void main(){
readln;
auto a=readln.chomp.split.to!(ulong[]);
ulong[ulong] l;
foreach(x;a)
l[x]++;
ulong sum;
foreach(x;l.values)
sum+=x*(x-1)>>1;
foreach(x;a)
(sum-(l[x]-1)).writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
void main()
{
auto S = readln.strip;
if(S[0]==S[$-1]){
writeln(S.length%2==0?"First":"Second");
}else{
writeln(S.length%2==1?"First":"Second");
}
}
|
D
|
module aoj;
import std.array;
import std.stdio;
import std.string;
void main() {
string line;
while ((line = chomp(readln())) != "END OF INPUT") {
foreach (word; split(line, " ")) {
write(word.length);
}
writeln();
}
}
|
D
|
import std;
void main() {
auto t = readln.strip.to!int;
Attempt: foreach(tt; 0 .. t) {
uint cm = 1;
readln;
auto ai = readln.split.to!(int[]);
if(ai[0] == 1) {
cm++;
}
if(ai.length >= 2) {
foreach(i, a; ai[1 .. $]) {
if(a == 0 && ai[i] == 0) {
writeln("-1");
continue Attempt;
} else if(a == 1 && ai[i] == 1) {
cm += 5;
} else if(a == 1) {
cm++;
}
}
}
writeln(cm);
}
}
|
D
|
/+ dub.sdl:
name "F"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
import core.bitop;
immutable int L = 20;
int main() {
Scanner sc = new Scanner(stdin);
int[][] dp = new int[][](L+1, 1<<(L+1));
int[] ans = new int[1<<(L+1)];
int n, k;
sc.read(n, k);
foreach (l; 0..n+1) {
string s;
sc.read(s);
foreach (i; 0..(1<<l)) {
if (s[i] == '1') {
dp[0][(1<<l)|i] = 1;
}
}
}
foreach (l; 0..L+1) {
foreach (a; 0..(1<<l)) {
foreach (b; 1..1<<(L+1-l)) {
int x = dp[l][(b<<l)|a];
ans[(1<<l)|a]+=x;
if (b) {
int z = bsr(b);
int msk = (1<<z)-1;
b ^= (1<<z);
if (b) {
dp[l+1][b<<(l+1)|(a<<1|1)] += x;
}
int zm = ~b & msk;
if (zm) {
int topz = bsr(zm);
int nb = b & ((1<<topz)-1);
dp[l+1][(nb|(1<<topz))<<(l+1)|(a<<1)] += x;
}
}
}
}
}
int result = 0, rb = -1;
foreach (i, d; ans) {
if (d < k) continue;
int b = bsr(i);
if (rb < b) {
rb = b;
result = i.to!int;
}
}
string s;
while (result > 1) {
if (result & 1) s ~= "1";
else s ~= "0";
result >>= 1;
}
writeln(s.retro.array);
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.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
foreach (line; stdin.byLine) {
auto x = line.chomp;
if (x.length == 0) continue;
auto f = new string[](10);
f[0] = f[9] = "0000000000";
f[1] = "0" ~ line.to!string ~ "0";
foreach (i; 2..9) {
f[i] = "0" ~ readln.chomp ~ "0";
}
bool ff;
foreach (i; 1..9) {
foreach (j; 1..9) {
if (f[i][j] == '0') continue;
if (f[i+1][j-1] == '1' && f[i][j+1] == '1') writeln("G");
else if (f[i+1][j-1] == '1') writeln("D");
else if (f[i][j+1] == '1' && f[i+1][j] == '1') writeln("A");
else if (f[i][j+1] == '1' && f[i+1][j+1] == '1') writeln("E");
else if (f[i+1][j] == '1' && f[i+1][j+1] == '1') writeln("F");
else if (f[i+1][j] == '1') writeln("B");
else if (f[i][j+1] == '1') writeln("C");
ff = 1;
break;
}
if (ff) break;
}
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
for (;;) {
auto N = readln.chomp.to!int;
if (N == 0) return;
auto ws = readln.split.to!(int[]);
auto DP = new int[][](N, N);
int max_l;
foreach (i; 0..N-1) if (abs(ws[i] - ws[i+1]) <= 1) {
DP[i][i+1] = 2;
}
foreach (jj; 1..N) {
foreach (i; 0..N) {
auto j = i+jj;
if (j >= N) break;
DP[i][j] = max(DP[i][j-1], DP[i+1][j]);
if (abs(ws[i] - ws[j]) <= 1 && DP[i+1][j-1] == j-i-1) {
DP[i][j] = j-i+1;
}
foreach (k; i+1..j) {
DP[i][j] = max(DP[i][j], DP[i][k] + DP[k+1][j]);
}
}
}
writeln(DP[0][N-1]);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto B = RD;
auto C = RD;
writeln(B - A == C - B ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std.stdio, std.string, std.uni;
void main() {
string w = readln.chomp;
int cnt = 0;
while (true) {
string t = readln.chomp;
if (t == "END_OF_TEXT") break;
string[] s = t.toLower.split;
foreach (x; s) {
if (x == w) ++cnt;
}
}
cnt.writeln;
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T = string)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T)(in string str) { return str.split.to!(T[]); }
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; }
long mod = pow(10, 9) + 7;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
struct UnionFind
{
void init(long n) { par = new long[](n); foreach (i; 0..n) par[i] = i; }
long root(long i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(long i, long j) { return root(i) == root(j); }
void unite(long i, long j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; }
long[] par;
}
void main()
{
auto A = RD!long;
auto B = RD!long;
if (A == 1 && B == 1)
{
writeln(1, " ", 2);
writeln(".#");
return;
}
long diff = abs(A - B);
auto eq = min(A, B) * 2;
long x = min(100, eq), y = eq / 100;
auto c1 = A > B ? '.' : '#';
auto c2 = A > B ? '#' : '.';
stderr.writeln(c1, c2);
stderr.writeln(x, " ", y);
writeln(100, " ", 100);
auto last = new long[](100);
foreach (i; 0..y)
{
foreach (j; 0..100)
{
if (j >= x)
write(c1);
else
{
last[j] = (i + j) % 2 == 0 ? 1 : 0;
write((i + j) % 2 == 0 ? c1 : c2);
}
}
writeln();
}
if (eq % 100 != 0)
{
foreach (j; 0..100)
{
if (j >= eq % 100)
write(c1);
else
{
last[j] = (y + j) % 2 == 0 ? 1 : 0;
write((y + j) % 2 == 0 ? c1 : c2);
}
}
writeln();
}
y += eq % 100 == 0 ? 0 : 1;
write(y % 2 == 0 ? [c2, c1] : [c1, c2]);
foreach (j; 2..100)
{
write(c1);
}
writeln();
foreach (j; 0..100)
{
write(c2);
}
writeln();
stderr.writeln(diff);
long cnt;
foreach (e; last) if (e == 1) ++cnt;
cnt += y % 2 == 1 ? -1 : 0;
--cnt;
diff += cnt;
stderr.writeln(diff);
foreach (i; y+2..100)
{
foreach (j; 0..100)
{
if (j == 0 || j == 99)
{
write(c2);
continue;
}
if (diff != 0 && (i + j) % 3 == 0)
{
--diff;
write(c1);
}
else
write(c2);
}
writeln();
}
if (diff > 0) foreach (i; 0..100_000_000) stderr.writeln(i);
stdout.flush();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
void main(){
auto ab = readln().chomp().split().map!(to!int).array();
ab.solve().writeln();
}
int solve(int[] input){
auto a = input[0];
auto b = input[1];
if (b >= a){
return a;
}else{
return a - 1;
}
}
|
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;
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;
void main() {
readint;
auto a = readints;
int n = a.reduce!max;
// n を候補から取り除く
auto b = a.dup.remove(a.countUntil!(e => e == n));
int d = b.reduce!((a, b) => abs(a - n/2) <= abs(b - n/2) ? a : b);
writeln(n, " ", d);
}
|
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;
void main()
{
foreach (line; stdin.byLine) {
auto p = line.map!(a=>a.to!int-'0').array;
foreach (i; 0..10) {
foreach (j; 0..9-i) {
p[j] = p[j] + p[j+1];
}
}
writeln(p[0]%10);
}
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto n = lread();
auto a = aryread();
long sum_a = a.sum();
// writeln(sum_a);
auto s = new long[](n + 1);
foreach (i; 0 .. n)
{
s[i + 1] = s[i] + a[i];
}
// writeln(s);
long ans = long.max;
long tmp;
foreach (i; 1 .. (s.length) - 1)
{
// writeln(s[i], ":", sum_a - s[i]);
tmp = abs(s[i] - (sum_a - s[i]));
ans = min(ans, tmp);
}
writeln(ans);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
/*
----------------------------------------
xs[m]は、
mの立っているビットの個数をkとするとき、
mのビットが立っているところにいるk人のと、
0番からk - 1番までの男との間で成立するマッチングの個数
を表すとする
xs[0] = 1
xs[m]は、累乗を用いて
m = or(i; 0 .. k) m[i]
とかかれるとき
m = sum xs[m[i] xor m]
ただしこの和は、k - 1番の男とm[i]の表す女がマッチする場合にわたる和
求めるものは
xs[1<<n - 1]
----------------------------------------
xs[0] = 1
xs[m]は、(i; 0 .. n) に対して、以下の和
mのビットの立っている個数をkとする
1<<i & m == 0 ならば 0
1<<i & m > 0 であって、
as[i][k - 1] == 0 ならば 0
as[i][k - 1] > 0 ならば xs[m ^ (1<<i)]
*/
const long mod = 1_000_000_007;
void main(){
int n = read.to!int;
int[][] as;
foreach(i; 0 .. n) as ~= readln.chomp.split.map!(to!int).array;
long[] xs = [1];
foreach(m; 1 .. 1<<n){
long k = 0;
foreach(i; 0 .. n) if(m & (1<<i)) k += 1;
long x = 0;
foreach(i; 0 .. n){
debug writeln("m:", m, " k:", k, " i:", i, " xs:", xs);
if(m & (1<<i)) if(as[i][k - 1] > 0){
x += xs[m ^ (1<<i)];
x %= mod;
}
}
xs ~= x;
}
xs[(1<<n) - 1].writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto t = "";
foreach (c; s)
switch (c) {
case '0':
case '1':
t ~= c;
break;
case 'B':
t = t.empty ? t : t[0..$-1];
break;
default:
assert(0);
}
writeln(t);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto N = RD;
auto S = RD;
auto A = RDA;
auto dp = new long[][](N+1, S+1);
long ans;
foreach (i; 0..N)
{
dp[i][0].moda(1);
foreach (j; 0..S)
{
dp[i+1][j].moda(dp[i][j]);
if (j+A[i] > S) continue;
dp[i+1][j+A[i]].moda(dp[i][j]);
}
long t = dp[i+1][S];
t.modm(N-i);
ans.moda(t);
}
debug writeln(dp);
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] h; readA(n, h);
h ~= 0;
for (auto i = 0; ; ++i) {
auto l = h.countUntil!"a>0";
if (l == -1) {
writeln(i);
return;
}
auto c = h[l..$].countUntil(0);
--h[l..l+c];
}
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.math, std.functional;
void main() {
auto N = readln.chomp.to!int;
int cx = 0, cy = 0;
int ct = 0;
bool ok = true;
foreach (i; 0..N) {
auto tmp = readln.split.to!(int[]);
if (!ok) continue;
auto t = tmp[0];
auto x = tmp[1];
auto y = tmp[2];
auto dt = t - ct;
auto dx = x - cx;
auto dy = y - cy;
auto d = abs(dx) + abs(dy);
if (d > dt) ok = false;
if ((dt - d) % 2 == 1) ok = false;
cx = x;
cy = y;
ct = t;
}
writeln(ok ? "Yes" : "No");
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
long n = read.to!long;
long[] as = readln.chomp.split.to!(long[]);
long ans;
foreach(a; as) while(a % 2 == 0) a /= 2, ans += 1;
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()
{
immutable N = readln.chomp.to!long;
long res;
foreach (i; 1..N.to!real.sqrt.to!long+1) {
if (N / i == i) {
continue;
}
if (N % i == 0) {
immutable t = N / i - 1;
if (N % t) {
res += t;
}
}
}
res.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 N, A, B;
scan(N, A, B);
string s = readln.chomp;
int tot, avs;
foreach (i ; 0 .. N) {
bool ok;
if (s[i] == 'a') {
if (tot < A + B) {
ok = true;
}
}
else if (s[i] == 'b') {
if (tot < A + B && avs < B) {
ok = true;
}
}
yes(ok);
if (ok) {
tot++;
if (s[i] == 'b') {
avs++;
}
}
}
}
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;
void main(string[]args){
long l,r;scanf("%d %d",&l,&r);
long a=1;int ans=0;
for(int i=0;i<32;++i){
long b=a;
for(int j=0;j<30;++j){
if(l<=b&&b<=r)++ans;
b*=3;if(b>r)break;
}
a*=2;if(a>r)break;
}
printf("%d",ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto A = readln.split.map!(to!int).array;
auto C = new int[](13);
foreach (a; A)
C[a] += 1;
if (N >= 24 || C[0] > 0 || C[12] > 1 || C.map!(c => c >= 3).any) {
writeln(0);
return;
}
foreach (i; 0..12) {
if (C[i] >= 2 && C[i+1] >= 2) {
writeln(1);
return;
}
}
int ans = 0;
foreach (i; 0..(1 << N)) {
auto jikan = new int[](24);
jikan[0] = 1;
foreach (j; 0..N) {
if (i & (1 << j)) {
jikan[A[j]] += 1;
} else {
jikan[24-A[j]] += 1;
}
}
int tmp = 1 << 29;
foreach (j; 0..24) {
foreach (k; 0..24) {
if (j != k && jikan[j] && jikan[k]) {
int jisa = abs(j-k);
tmp = min(tmp, min(jisa, 24-jisa));
} else if (j == k && jikan[j] >= 2) {
tmp = 0;
}
}
}
ans = max(ans, tmp);
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.range;
import std.typecons;
import std.algorithm;
struct RMQSegmentTree(Type = int,string gstr = "a>b"){
Type[] data;
size_t height;
Type initvalue;
bool greater(Type a,Type b){
bool ret;
mixin("ret = "~gstr~";");
return ret;
}
Type greatValue(Type a,Type b){
if(greater(a,b))
return a;
return b;
}
void SetSize(size_t size){
size_t s = 1;
height = 0;
while(s<size){
s*=2;
++height;
}
data = new Type[s*2];
initvalue = Type.max - greatValue(Type.max,Type.min);
data[] = initvalue;
}
size_t GetIdx(size_t idx,size_t depth){
size_t blocksize = 1<<depth;
return blocksize-1+(idx/blocksize)*blocksize*2;
}
void SetData(size_t idx,Type value,size_t depth = 0){
if(depth > height)
return;
size_t gi = GetIdx(idx,depth);
if(greater(value,data[gi])){
data[gi] = value;
SetData(idx,value,depth+1);
}
}
Type GetRMQ(size_t begin,size_t end,size_t depth = -1){
if(depth == -1)
depth = height;
if(begin == end)
return initvalue;
size_t blocksize = 1<<depth;
size_t start = begin - begin%blocksize;
if(start == begin && start + blocksize == end)
return data[GetIdx(begin,depth)];
blocksize /= 2;
Type ret = initvalue;
if(begin < start + blocksize)
ret = greatValue(ret, GetRMQ(begin,min(start + blocksize, end), depth-1));
if(end > start + blocksize)
ret = greatValue(ret, GetRMQ(max(begin,start + blocksize), end, depth-1));
return ret;
}
}
int[] rdi(){
return readln.split.map!(to!int).array;
}
void main(){
int n= rdi[0];
int[] w;
w = rdi;
RMQSegmentTree!long st;
st.SetSize(n);
long[] dp = new long[n];
dp[] = 0;
foreach(i;0..n){
dp[i] = st.GetRMQ(0,w[i]);
dp[i] += w[i];
st.SetData(w[i]-1,dp[i]);
}
long res = 0;
foreach(i;0..n){
res += w[i];
}
long md = 0;
foreach(i;0..n){
md = max(md,dp[i]);
}
(res-md).writeln;
}
|
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 x, a, b;
scan(x, a, b);
long A = abs(x - a);
long B = abs(x - b);
if (min(A, B) == A)
{
writeln('A');
}
else
{
writeln('B');
}
}
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;
import std.string;
import std.conv;
import std.algorithm;
void main(){
auto a=readln.split.to!(int[]),x=a[0],y=a[1];
writeln(x+y/2);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto di = readln.split.map!(to!int).array;
auto si = readln.chomp;
auto dice = new Dice(di);
foreach (c; si)
dice.roll(c);
writeln(dice.top);
}
class Dice
{
int[] di;
this(int[] di)
{
this.di = di;
}
int top() { return di[0]; }
void roll(char d)
{
auto rolled = ['N': [1, 5, 2, 3, 0, 4],
'E': [3, 1, 0, 5, 4, 2],
'S': [4, 0, 2, 3, 5, 1],
'W': [2, 1, 5, 0, 4, 3]];
di = di.indexed(rolled[d]).array;
}
}
|
D
|
import std;
void main() {
auto K = readln.split[0].to!int;
auto AB = readln.split.to!(int[]);
auto A = AB[0], B = AB[1];
foreach (i; A .. B+1) {
if (i % K == 0) { writeln("OK"); return; }
}
writeln("NG");
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv;
void main() {
auto s = map!(x => x.to!int)(readln.chomp.split);
int n = s[0], a = s[1], b = s[2];
int res = 0;
foreach (i; 1 .. n + 1) {
auto t = i.to!(dchar[]);
int sum = 0;
foreach (j; 0 .. t.length) {
sum += t[j] - '0';
}
if (a <= sum && sum <= b) res += i;
}
writeln(res);
}
|
D
|
void main() {
string s = readln.chomp;
string s1 = s[0..2], s2 = s[2..$];
bool yymm, mmyy;
if ("01" <= s1 && s1 <= "12") mmyy = true;
if ("01" <= s2 && s2 <= "12") yymm = true;
if (yymm && mmyy) {
writeln("AMBIGUOUS");
} else if (yymm && !mmyy) {
writeln("YYMM");
} else if (!yymm && mmyy) {
writeln("MMYY");
} else {
writeln("NA");
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
import std.typecons;
|
D
|
import std.string;
import std.stdio;
import std.functional;
import std.conv;
import std.algorithm;
void main(){
readln.split.map!(to!int).unaryFun!(a => text(a[0] * a[1], " ", 2 * (a[0] + a[1]))).writeln;
}
|
D
|
import std;
const AB = 1;
const AC = 2;
const BA = 3;
const BC = 4;
const CA = 5;
const CB = 6;
void calc(int a, int b, int c, string[] ss) {
string move(int mv) {
switch (mv) {
case AB:
if (a == 0 && b == 0) return "X";
if (a == 0) { a++; b--; return "A";}
else { a--; b++; return "B";}
break;
case AC:
if (a == 0 && c == 0) return "X";
if (a == 0) { a++; c--; return "A";}
else { a--; c++; return "C";}
break;
case BA:
if (b == 0 && a == 0) return "X";
if (b == 0) { b++; a--; return "B";}
else { b--; a++; return "A";}
break;
case BC:
if (b == 0 && c == 0) return "X";
if (b == 0) { b++; c--; return "B";}
else { b--; c++; return "C";}
break;
case CA:
if (c == 0 && a == 0) return "X";
if (c == 0) { c++; a--; return "C";}
else { c--; a++; return "A";}
break;
case CB:
if (c == 0 && b == 0) return "X";
if (c == 0) { c++; b--; return "C";}
else { c--; b++; return "B";}
break;
default: break;
}
return "X";
}
string[] ans;
for (int i = 0; i < ss.length; i++) {
string s = ss[i];
if (i+1 == ss.length) {
int mv = AB;
if (s == "AB") mv = AB;
if (s == "AC") mv = AC;
if (s == "BC") mv = BC;
// writeln(a, " ", b, " ", c, " ", " mv ", mv);
string ch = move(mv);
if (ch == "X") {
writeln("No");
return;
}
ans ~= ch;
}
else {
string t = ss[i + 1];
int mv = AB;
if (s == "AB") {
if (t == "AB") mv = AB;
if (t == "AC") mv = BA;
if (t == "BC") mv = AB;
}
else if (s == "AC") {
if (t == "AB") mv = CA;
if (t == "AC") mv = AC;
if (t == "BC") mv = AC;
}
else if (s == "BC") {
if (t == "AB") mv = CB;
if (t == "AC") mv = BC;
if (t == "BC") mv = BC;
} else assert(false);
string ch = move(mv);
if (ch == "X") {
writeln("No");
return;
}
ans ~= ch;
}
}
writeln("Yes");
foreach (e; ans) writeln(e);
}
void main() {
int n, a, b, c; scan(n, a, b, c);
string[] ss;
foreach (_; 0..n) ss ~= read!string;
calc(a, b, c, ss);
}
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 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, M;
scan(N, M);
auto G = new long[][](N, N);
foreach (_; 0 .. M)
{
long a, b;
scan(a, b);
G[a - 1][b - 1] = G[b - 1][a - 1] = 1;
}
// foreach (g; G)
// writeln(g);
auto ID = new long[](N);
auto L = new long[](N);
auto visited = new bool[][](N, N);
ID[] = -1;
ID[0] = 0;
L[] = -1;
L[0] = 0;
long cnt;
long id;
long[] stack = [0];
loop: while (!stack.empty)
{
long x = stack.back;
// writeln(x);
// writeln(L, stack);
foreach (i; 0 .. N)
if (G[x][i] == 1 && !visited[x][i])
{
visited[x][i] = visited[i][x] = true;
if (ID[i] == -1)
{
ID[i] = L[i] = ++id;
stack ~= i;
}
L[x] = L[x].min(ID[i]).min(L[i]);
continue loop;
}
stack.popBack();
// writefln("back: %d-%d %d %d", x, stack.back, L[x], ID[x]);
if (!stack.empty)
{
L[stack.back] = L[stack.back].min(ID[x]).min(L[x]);
if (L[x] == ID[x])
{
cnt++;
}
}
}
writeln(cnt);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, m;
scan(n, m);
auto cnt = new int[](m);
foreach (i ; 0 .. n) {
auto li = readln.split.to!(int[]);
foreach (b ; li[1 .. $]) {
cnt[b - 1]++;
}
}
int ans;
foreach (i ; 0 .. m) {
ans += cnt[i] == n;
}
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.array; // split
import std.conv; // to
void main()
{
string n = chomp(readln());
int a = to!int(n);
if(a/100%10 == a/10%10 && (a/100%10 == a/1000 || a/100%10 == a%10)){
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio;
import std.range;
import std.array;
import std.algorithm;
import std.string;
import std.conv;
import std.typecons;
import std.math;
import std.numeric;
void main() {
int[] input;
foreach (i; 0 .. 5) {
input ~= readln().chomp().to!int;
}
solve(input).writeln();
}
int solve(int[] foods) {
foods.sort!((f1, f2) {
auto f1_1 = f1 % 10;
auto f2_1 = f2 % 10;
return (10 - f1_1) % 10 < (10 - f2_1) % 10;
});
int time = 0;
foreach (food; foods) {
if (time % 10 != 0) {
time = 10 * (time / 10 + 1);
}
time += food;
}
return time;
}
unittest {
assert(solve([29, 20, 7, 35, 120]) == 215, solve([29, 20, 7, 35, 120]).to!string);
assert(solve([101, 86, 119, 108, 57]) == 481, solve([101, 86, 119, 108, 57]).to!string);
assert(solve([123, 123, 123, 123, 123]) == 643, solve([123, 123, 123, 123, 123]).to!string);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto S = N.iota.map!(_ => readln.chomp).array;
long ans = 0;
foreach (i; 0..N) {
bool ok = true;
int a = 0;
int b = i;
foreach (j; 0..N) {
int c = (a + j) % N;
int d = (b + j) % N;
foreach (k; 1..N-j) {
if (S[(c+k)%N][d] != S[c][(d+k)%N]) {
ok = false;
}
}
}
ans += ok * N;
}
ans.writeln;
}
|
D
|
void main(){
string s = inelm!string();
writeln( s.lastIndexOf('Z') - s.indexOf('A')+1 );
}
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 core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
auto S = sread();
long ans;
foreach (i; 0 .. S.length / 2)
if (S[i] != S[$ - i - 1])
{
ans++;
}
writeln(ans);
}
|
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;
void main() {
while (true) {
scan(n);
if (n == 0) break;
writeln(solve(n));
}
}
int solve(int n) {
int res;
foreach (l ; 0 .. n) {
foreach (r ; l + 2 .. n) {
int x = r * (r + 1) / 2 - l * (l + 1) / 2;
if (x == n) {
debug {
writeln('[', l, ", ", r, ']');
}
res++;
}
}
}
return res;
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.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()
{
long x; readV(x);
auto ans = x/11*2, m = x%11;
if (m >= 1 && m <= 6) ++ans;
else if (m > 6) ans += 2;
writeln(ans);
}
|
D
|
import std;
void main() {
int n, k; scan(n, k);
auto s = new int[n + 1];
foreach (_; 0..k) {
readint;
foreach (e; readints) s[e]++;
}
auto ans = n - s[1..$].count!(e => e > 0);
writeln(ans);
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
|
D
|
import std.stdio;
import std.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 r = readln.chomp.to!int;
auto g = readln.chomp.to!int;
writeln(2 * g - r);
}
|
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;
const long INF = 1L << 59;
const long MOD = 10 ^^ 9 + 7;
void main() {
auto N = readln.chomp.to!int;
auto edges = new Tuple!(int, long)[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
edges[s[0] - 1] ~= tuple(s[1] - 1, s[2].to!long);
edges[s[1] - 1] ~= tuple(s[0] - 1, s[2].to!long);
}
auto dist = new long[](N);
void dfs(int n, int p, long c) {
dist[n] = c;
foreach (m; edges[n]) if (m[0] != p) dfs(m[0], n, c + m[1]);
}
auto s = readln.split.map!(to!int);
auto Q = s[0];
auto K = s[1] - 1;
dfs(K, -1, 0);
while (Q--) {
s = readln.split.map!(to!int);
writeln(dist[s[0] - 1] + dist[s[1] - 1]);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto N = readln.split[0].to!int;
auto S = readln.split[0].to!(dchar[]);
auto K = readln.split[0].to!int;
string ans = "";
auto c = S[K-1];
foreach (d; S) {
if (d == c) ans ~= d;
else ans ~= '*';
}
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
int main()
{
string[] str;
while (true) {
str = readln().split();
int num1 = str[0].to!int();
char op = str[1].to!char();
int num2 = str[2].to!int();
switch (op) {
case '+': writeln(num1 + num2); break;
case '-': writeln(num1 - num2); break;
case '*': writeln(num1 * num2); break;
case '/': writeln(num1 / num2); break;
default : break;
}
if (op == '?') break;
}
return 0;
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void 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;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
readln;
readln.chomp.uniq.count.writeln;
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto s = readln.chomp.to!ulong;
bool[1000000] memo;
foreach (i; 0..1000000) {
memo[s] = true;
auto a = s & 1 ? (3*s+1) : (s/2);
if (memo[a]) {
writeln(i+2);
return;
}
s = a;
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split, n = rd[0].to!int, k = rd[1].to!size_t;
auto d = readln.split.to!(int[]);
auto db = new bool[](10);
foreach (di; d) db[di] = true;
while (toDigits(n).any!(i => db[i])) ++n;
writeln(n);
}
auto toDigits(int n)
{
int[] r;
for (; n > 0; n /= 10) r ~= n % 10;
return r;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
double[] weight = [48, 51, 54, 57, 60, 64, 69, 75, 81, 91];
string[] names =
["light fly", "fly", "bantam", "feather",
"light", "light welter", "welter",
"light middle", "middle", "light heavy", "heavy"];
while(true) {
string line = readln.chomp;
if (stdin.eof) break;
double num = line.to!(double);
string ans;
if (weight[0] >= num) ans = names[0];
else if (weight[$ - 1] < num) ans = names[$ - 1];
else {
for(uint i = 0; i < (weight.length - 1); i++) {
if (weight[i] < num && num <= weight[i + 1]) {
ans = names[i + 1];
}
}
}
writeln(ans);
}
}
|
D
|
import std.stdio;
import std.regex;
void main(){
auto io = new IO();
auto N = io.line()[0];
auto K = io.line()[0];
auto X = io.line()[0];
auto Y = io.line()[0];
size_t fee = 0;
if( N>K ){
fee += K*X;
fee += (N-K)*Y;
}else{
fee += N*X;
}
writeln(fee);
return;
}
import std.stdio,std.conv,std.string;
import std.algorithm,std.array,std.math;
immutable PRIME = 1_000_000_007;
immutable alphaB = "abcdefghijklmnopqrstuvwxyz";
immutable alphaU = "ABCDRFGHIJKLMNOPQRSTUVWXYZ";
class IO
{
T[] line( T = size_t , string token = " " )( size_t m = 1 ){
T[] arr = [];
foreach( i ; 0..m ){
arr ~= this.read!T();
}
return arr;
}
T[][] rect( T = size_t , string token = " " )( size_t m = 1 ){
T[][] arr = new T[][](m);
foreach( i ; 0..m ){
arr[i] = this.read!T(token);
}
return arr;
}
private T[] read( T = size_t )( string token = " " ){
T[] arr;
foreach( elm ; readln().chomp().split(token) ){
arr ~= elm.to!T();
}
return arr;
}
void swap( T )( ref T x , ref T y ){
x = x^y;
y = x^y;
x = x^y;
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.to!int;
writeln(n * n - a);
}
|
D
|
/+ dub.sdl:
name "B"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.math;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n, k1, k2;
sc.read(n, k1, k2);
long[] a, b;
sc.read(a, b);
foreach (i; 0..n) a[i] = abs(a[i]-b[i]);
foreach (ph; 0..k1+k2) {
int i = a.maxIndex.to!int;
a[i] = abs(a[i]-1);
}
writeln(a.map!"a*a".sum);
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;
void main()
{
auto S = readln.chomp.to!(wchar[]);
auto T = readln.chomp.to!(wchar[]);
foreach (_; 0..101) {
if (S == T) {
writeln("Yes");
return;
}
S = [S[$-1]] ~ S[0..$-1];
}
writeln("No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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;
long ans;
foreach (i; 1..N+1)
{
auto cnt = i.to!string.length;
if (cnt % 2 == 1)
++ans;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
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()
{
long A, V;
scan(A, V);
long B, W;
scan(B, W);
long T = lread();
if (V - W <= 0)
{
writeln("NO");
return;
}
if (abs(A - B) <= T * (V - W))
{
writeln("YES");
return;
}
writeln("NO");
}
|
D
|
import std.stdio;
import std.ascii;
import std.algorithm;
import core.stdc.stdio;
int main()
{
auto t = readInt!int;
foreach(ti; 0 .. t)
{
int[4] s;
s[0] = readInt!int;
s[1] = readInt!int;
s[2] = readInt!int;
s[3] = readInt!int;
if (min(s[2], s[3]) > max(s[0], s[1]) || min(s[0], s[1]) > max(s[2], s[3]))
{
writeln("NO");
}
else writeln("YES");
}
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;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
long rep;
this(long num)
{
rep = num;
}
Z!m opBinary(string operator)(Z!m rhs)
{
static if (operator == "+")
{
long result = rhs.rep + this.rep;
if (result >= m) result -= m;
return Z!m(result);
}
else static if (operator == "-")
{
long result = this.rep - rhs.rep;
if (result < 0) result += m;
return Z!m(result);
}
else static if (operator == "*")
{
long result = this.rep * rhs.rep;
if (result >= m) result %= m;
return Z!m(result);
} else static assert(text("Operator ", operator, " not supported"));
}
Z!m opBinary(string operator)(long exponent) if (operator == "^^")
{
assert(exponent >= 0);
Z!m base = this;
Z!m result = 1;
while (exponent)
{
if (exponent & 1)
result = result * base;
base = base * base;
exponent >>= 1;
}
return result;
}
invariant
{
assert(rep >= 0 && rep < m);
}
}
|
D
|
import std.stdio, std.range, std.conv, std.string;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.setops, std.algorithm.sorting;
struct Strut{
long A;
long B;
}
void main()
{
long[] input = readln().split.to!(long[]);
long A = input[0];
long B = input[1];
writeln(f(A-1)^f(B));
}
long f(long x)
{
switch(x%4)
{
case 0:
return x;
case 1:
return 1;
case 2:
return x ^ 1;
case 3:
return 0;
default:
return 0;
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
readln.split.to!(int[]).map!(x => x-1).reduce!((a,b) => a * b).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;
void main()
{
foreach (line; stdin.byLine) {
auto x = line.split;
long cost, sum;
cost += x[1].to!long * 200 + x[2].to!long * 300;
sum = x[1].to!long + x[2].to!long;
writeln(x[0], " ", sum, " ", cost);
}
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc052/tasks/abc052_b
// simulation
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
void main() {
int n = readln.chomp.to!int;
string s = readln.chomp;
int maxima = 0;
int x = 0;
foreach(ch; s) {
if(ch == 'I')
x += 1;
else
x -= 1;
maxima = max(maxima, x);
}
maxima.writeln;
}
|
D
|
void main(){
long n = _scan!long();
for(int i;; i++){
if(i*i>n){
( (i-1)*(i-1) ).writeln();
break;
}
}
}
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
|
/+ dub.sdl:
name "B"
dependency "dunkelheit" version="1.0.1"
+/
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 a, b, c, d;
sc.read(a, b, c, d);
int ans = 0;
foreach (r; 0..3030) {
foreach (g; 0..3030) {
int rem = d - (a * r + b * g);
if (rem < 0) continue;
if (rem % c == 0) ans++;
}
}
writeln(ans);
return 0;
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /home/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
private File f;
this(File f) {
this.f = f;
}
private char[512] lineBuf;
private char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
assert(succW());
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception : enforce;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
string[] operators = ["=","(", ")", "+", "-", "*", "/"];
bool isOperator(string s) {
return operators.canFind(s);
}
int priority(string op) {
int i = 0;
while(operators[i] != op)
++i;
return (i+1)/2;
}
string nextToken(ref string s) {
string t = "";
while(s[0].isDigit()) {
t ~= s[0];
s.popFrontN(1);
}
if(t == "") {
t ~= s[0];
s.popFrontN(1);
}
return t;
}
int apply(string op, int l, int r) {
switch(op){
case "+":
return l+r;
case "-":
return l-r;
case "*":
return l*r;
case "/":
return l/r;
default:
return 0;
}
}
void apply(string op, ref int[] vstack) {
auto r = vstack.back;
vstack.popBackN(1);
auto l = vstack.back;
vstack.popBackN(1);
vstack ~= apply(op, l, r);
}
int calc(string s) {
int[] vstack;
string[] opstack;
while(!s.empty()) {
auto t = nextToken(s);
if(!t.isOperator()) {
vstack ~= to!int(t);
} else {
if(opstack.empty || t == "(" || t.priority() > opstack.back.priority())
opstack ~= t;
else {
auto op = opstack.back;
opstack.popBackN(1);
if(op == "(") {
continue;
}
apply(op, vstack);
s = t~s;
}
}
}
return vstack.front;
}
void main() {
int n = to!int(readln().strip());
foreach(i; 0..n) {
auto s = readln().strip();
writeln(calc(s));
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum DATA = q"EOS
1 H HYDROGEN
2 HE HELIUM
3 LI LITHIUM
4 BE BERYLLIUM
5 B BORON
6 C CARBON
7 N NITROGEN
8 O OXYGEN
9 F FLUORINE
10 NE NEON
11 NA SODIUM
12 MG MAGNESIUM
13 AL ALUMINUM
14 SI SILICON
15 P PHOSPHORUS
16 S SULFUR
17 CL CHLORINE
18 AR ARGON
19 K POTASSIUM
20 CA CALCIUM
21 SC SCANDIUM
22 TI TITANIUM
23 V VANADIUM
24 CR CHROMIUM
25 MN MANGANESE
26 FE IRON
27 CO COBALT
28 NI NICKEL
29 CU COPPER
30 ZN ZINC
31 GA GALLIUM
32 GE GERMANIUM
33 AS ARSENIC
34 SE SELENIUM
35 BR BROMINE
36 KR KRYPTON
37 RB RUBIDIUM
38 SR STRONTIUM
39 Y YTTRIUM
40 ZR ZIRCONIUM
41 NB NIOBIUM
42 MO MOLYBDENUM
43 TC TECHNETIUM
44 RU RUTHENIUM
45 RH RHODIUM
46 PD PALLADIUM
47 AG SILVER
48 CD CADMIUM
49 IN INDIUM
50 SN TIN
51 SB ANTIMONY
52 TE TELLURIUM
53 I IODINE
54 XE XENON
55 CS CESIUM
56 BA BARIUM
57 LA LANTHANUM
58 CE CERIUM
59 PR PRASEODYMIUM
60 ND NEODYMIUM
61 PM PROMETHIUM
62 SM SAMARIUM
63 EU EUROPIUM
64 GD GADOLINIUM
65 TB TERBIUM
66 DY DYSPROSIUM
67 HO HOLMIUM
68 ER ERBIUM
69 TM THULIUM
70 YB YTTERBIUM
71 LU LUTETIUM
72 HF HAFNIUM
73 TA TANTALUM
74 W TUNGSTEN
75 RE RHENIUM
76 OS OSMIUM
77 IR IRIDIUM
78 PT PLATINUM
79 AU GOLD
80 HG MERCURY
81 TL THALLIUM
82 PB LEAD
83 BI BISMUTH
84 PO POLONIUM
85 AT ASTATINE
86 RN RADON
87 FR FRANCIUM
88 RA RADIUM
89 AC ACTINIUM
90 TH THORIUM
91 PA PROTACTINIUM
92 U URANIUM
93 NP NEPTUNIUM
94 PU PLUTONIUM
95 AM AMERICIUM
96 CM CURIUM
97 BK BERKELIUM
98 CF CALIFORNIUM
99 ES EINSTEINIUM
100 FM FERMIUM
101 MD MENDELEVIUM
102 NO NOBELIUM
103 LR LAWRENCIUM
104 RF RUTHERFORDIUM
105 DB DUBNIUM
106 SG SEABORGIUM
107 BH BOHRIUM
108 HS HASSIUM
109 MT MEITNERIUM
110 DS DARMSTADTIUM
111 RG ROENTGENIUM
112 CN COPERNICIUM
113 NH NIHONIUM
114 FL FLEROVIUM
115 MC MOSCOVIUM
116 LV LIVERMORIUM
117 TS TENNESSINE
118 OG OGANESSON
EOS";
void main() {
bool[string] el;
const tokens = DATA.split;
for (int i = 1; i < tokens.length; i += 3) {
el[tokens[i]] = true;
}
try {
for (; ; ) {
const S = readToken();
auto L = cast(int)(S.length);
auto dp = new bool[L + 1];
dp[0] = true;
foreach (i; 0 .. L) {
if (dp[i]) {
foreach (j; [i + 1, i + 2]) {
if (j <= L) {
if (S[i .. j] in el) {
dp[j] = true;
}
}
}
}
}
writeln(dp[L] ? "YES" : "NO");
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio;
import std.algorithm;
int[] readArray(int n) {
int[] a = new int[n - 1];
int start = -1;
for (int i = 0, j = 0; i < n; i++) {
int x;
scanf("%d", &x);
if (x == 1) {
start = j;
}
if (x != 0) {
a[j++] = x;
}
}
int[] res = new int[n - 1];
for (int i = 0, j = start; i < n - 1; i++, j = (j + 1) % (n - 1))
res[i] = a[j];
return res;
}
void main()
{
int n;
scanf("%d", &n);
int[] a = readArray(n);
int[] b = readArray(n);
if (equal(a, b))
printf("YES\n");
else
printf("NO\n");
}
|
D
|
import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
bool isSquare (int n)
{
auto p = cast (int) (sqrt (n * 1.0) + 0.5);
return p * p == n;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
while (true)
{
if (n & 1)
{
writeln ("NO");
break;
}
n /= 2;
if (isSquare (n))
{
writeln ("YES");
break;
}
}
}
}
|
D
|
import std.stdio, std.algorithm, std.array, std.string, std.conv;
void main()
{
int t;
scanf("%d", &t);
foreach(_; 0..t)
{
long n;
scanf("%lld", &n);
getchar();
writeln(-1 * (n - 1), ' ', n);
}
}
|
D
|
import core.bitop;
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;
void main ()
{
string s;
while ((s = readln.strip) != "")
{
string a = ".";
string b = ".";
ubyte cur = 0;
foreach (c; s.representation)
{
while (c != cur)
{
cur -= 1;
a ~= "X.";
b ~= "..";
}
a ~= ".X..";
b ~= "XXX.";
}
writeln (a);
writeln (b);
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto a = RD;
auto b = RD;
if (a < b)
swap(a, b);
auto d = a-b;
auto cnt = min(d, b);
ans[ti] += cnt;
a -= cnt * 2;
b -= cnt;
cnt = b / 3;
ans[ti] += cnt * 2;
a -= cnt * 3;
b -= cnt * 3;
if (max(a, b) >= 2 && min(a, b) >= 1)
++ans[ti];
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
final T[] nextA(T) (int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader;
immutable int n = r.next!int;
auto a = r.nextA!int (n);
auto b = a.map!"abs(a)".array;
sort (b);
int i, j;
ulong res;
for (i = 0; i < n; ++i) {
int x = b[i];
while (j < n && b[j] - x <= x) ++j;
debug stderr.writefln ("i = %d, j = %d", i, j);
res += max (0, j - i - 1);
}
writeln (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)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
auto s = RD!string;
if (k == 0)
{
ans[ti] = 1;
continue;
}
int l, r = n-1;
bool ok = true;
while (l < r)
{
if (s[l] != s[r])
{
ok = false;
break;
}
++l; --r;
}
if (ok)
ans[ti] = 1;
else
ans[ti] = 2;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
module main;
import std.stdio;
import std.string;
int main(string[] argv)
{
int n, j = 0;
scanf("%d", &n);
int [] a = new int[n];
for(int i = 0; i < n; i++)
{
scanf("%d", &a[j]);
while(j > 0 && a[j] == a[j - 1])
{
a[j - 1]++;
a[j] = 0;
j--;
}
j++;
}
writeln(j);
for(int i = 0; i < j; i++)
write(a[i]," ");
return 0;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.