code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
int n;
string s;
int[] os;
void main(){
n = readln.chomp.to!int;
s = readln.chomp;
os = s.map!(x => (x == 'o')? 1: 0).array;
int[] sheeps;
char[] ans = cast(char[]) "-1";
sheeps = getSheeps(0, 0);
if(sheeps.isOK) ans = sheeps.map!(x => x? 'S': 'W').array;
sheeps = getSheeps(0, 1);
if(sheeps.isOK) ans = sheeps.map!(x => x? 'S': 'W').array;
sheeps = getSheeps(1, 0);
if(sheeps.isOK) ans = sheeps.map!(x => x? 'S': 'W').array;
sheeps = getSheeps(1, 1);
if(sheeps.isOK) ans = sheeps.map!(x => x? 'S': 'W').array;
ans.writeln;
}
int[] getSheeps(int a1, int a2){
int[] sheeps = [a1, a2];
sheeps.length = n;
for(int i = 2; i < n; i ++){
if((sheeps[i - 2] + sheeps[i - 1] + os[i - 1]) % 2){
sheeps[i] = 1;
}
else sheeps[i] = 0;
}
return sheeps;
}
bool isOK(int[] sheeps){
if((sheeps[n - 2] + sheeps[n - 1] + sheeps[0] + os[n - 1]) % 2)
return false;
if((sheeps[n - 1] + sheeps[0] + sheeps[1] + os[0]) % 2)
return false;
return true;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int n, m; rd(n, m);
auto x=readln.split.to!(long[]);
auto y=readln.split.to!(long[]);
int cnt=0;
for(int i=0, j=0, sx=0, sy=0; i<n; i++){
sx+=x[i];
while(j<m && (sy+=y[j])<sx) j++;
if(sy==sx) sx=sy=0, j++, cnt++;
else sy-=y[j];
}
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));
}
}
|
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 S = readln.split[0], T = readln.split[0];
int[26] s_data, t_data;
int[26] s_ord, t_ord;
int ord = 1;
foreach ( c; S ) {
if (s_ord[c-'a'] == 0) {
s_ord[c-'a'] = (ord++);
s_data[ s_ord[c-'a']-1 ]++;
}
else s_data[ s_ord[c-'a']-1 ]++;
}
ord = 1;
foreach ( c; T ) {
if (t_ord[c-'a'] == 0) {
t_ord[c-'a'] = (ord++);
t_data[ t_ord[c-'a']-1 ]++;
}
else t_data[ t_ord[c-'a']-1 ]++;
}
writeln(s_data == t_data ? "Yes" : "No");
}
|
D
|
/+ dub.sdl:
name "F"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.numeric.prime;
// import dcomp.numeric.primitive;
import std.numeric;
long powmod(long x, long n, long md) {
long r = 1;
while (n) {
if (n & 1) r = (r*x) % md;
x = (x*x) % md;
n >>= 1;
}
return r;
}
long tot(long x) {
long y = x;
for (long i = 2; i*i <= x; i++) {
if (x % i) continue;
y /= i;
y *= (i-1);
while (x % i == 0) x /= i;
}
if (x > 1) {
y /= x;
y *= (x-1);
}
return y;
}
long inv(long x, long md) {
return extGcd(x, md)[0];
}
long query(long a, long m) {
if (m == 1) {
return 10L^^9;
}
long t = tot(m);
long g = gcd(t, m);
long u = query(a, g);
long f = powmod(a, u, m);
// writefln("%d %d %d %d %d %d", a, m, t, g, u, f);
f = ((f - u) % m + m) % m;
f /= g;
f *= inv(t/g, m/g); f = (f % m + m) % m;
// writeln("last f", f);
long ans = u + f * t;
// writeln("RES ", powmod(a, ans, m), " ", ans % m);
return u + f * t;
}
int main() {
Scanner sc = new Scanner(stdin);
int q;
sc.read(q);
foreach (ph; 0..q) {
long a, m;
sc.read(a, m);
long z = query(a, m);
writeln(z);
// writeln(z, " ", powmod(a, z, m), " ", z % m);
// break;
}
return 0;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;
import std.traits;
import std.bigint;
Unqual!T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) {
return pow(x, n, T(1));
}
Unqual!T pow(T, U, V)(T x, U n, V e)
if ((isIntegral!U || is(U == BigInt)) && is(Unqual!T == Unqual!V)) {
Unqual!T b = x, v = e;
Unqual!U m = n;
while (m) {
if (m & 1) v *= b;
b *= b;
m /= 2;
}
return v;
}
T lcm(T)(in T a, in T b) {
import std.numeric : gcd;
return a / gcd(a,b) * b;
}
T[3] extGcd(T)(in T a, in T b)
if (!isIntegral!T || isSigned!T)
{
if (b==0) {
return [T(1), T(0), a];
} else {
auto e = extGcd(b, a%b);
return [e[1], e[0]-a/b*e[1], e[2]];
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/numeric/prime.d */
// module dcomp.numeric.prime;
T[] divisorList(T)(T x) {
import std.algorithm : sort;
T[] res;
for (T i = 1; i*i <= x; i++) {
if (x%i == 0) {
res ~= i;
if (i*i != x) res ~= x/i;
}
}
sort(res);
return res;
}
T[] factorList(T)(T x) {
T[] res;
for (T i = 2; i*i <= x; i++) {
while (x % i == 0) {
res ~= i;
x /= i;
}
}
if (x > 1) res ~= x;
return res;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
static if (__VERSION__ <= 2070) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
version (X86) static if (__VERSION__ < 2071) {
import core.bitop : bsf, bsr, popcnt;
int bsf(ulong v) {
foreach (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int bsr(ulong v) {
foreach_reverse (i; 0..64) {
if (v & (1UL << i)) return i;
}
return -1;
}
int popcnt(ulong v) {
int c = 0;
foreach (i; 0..64) {
if (v & (1UL << i)) c++;
}
return c;
}
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
if (f.eof) return false;
line = lineBuf[];
f.readln(line);
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else {
auto buf = line.split.map!(to!E).array;
static if (isStaticArray!T) {
assert(buf.length == T.length);
}
x = buf;
line.length = 0;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/*
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)
*/
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;
void main()
{
auto res = iota(0, 1000001).array;
foreach_reverse (i; 0..1000001) {
foreach (y; 2..1001) {
auto py = y ^^ 2;
if (i + py > 1000000) break;
res[i+py] = res[i] + y;
}
}
foreach_reverse (i; 0..1000001) {
foreach (z; 2..101) {
auto pz = z ^^ 3;
if (i + pz > 1000000) break;
res[i+pz] = min(res[i+pz], res[i] + z);
}
}
while (1) {
auto n = readln.chomp.to!int;
if (!n) break;
res[n].writeln;
}
}
|
D
|
import std.stdio, std.string, std.algorithm, std.conv, std.range, std.array;
void main() {
auto inp = readln().chomp;
int max;
for(int i; i<=(inp.length-1)/2; i++){
if(inp[0..i]==inp[i..i*2]){max=i*2;}
}
max.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto md1 = readln.split.to!(int[]);
auto md2 = readln.split.to!(int[]);
writeln(md1[0] == md2[0] ? 0 : 1);
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional;
void main() {
auto S = readln.chomp;
if(S == "Sunny") {
"Cloudy".writeln;
} else if (S=="Cloudy") {
"Rainy".writeln;
} else if(S=="Rainy") {
"Sunny".writeln;
}
}
|
D
|
void main() {
auto n = ri;
n %= 10;
switch(n) {
case 2, 4, 5, 7, 9:
writeln("hon");
break;
case 0, 1, 6, 8:
writeln("pon");
break;
case 3:
writeln("bon");
break;
default: assert(0);
}
}
// ===================================
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;
}
long rl() {
return readAs!long;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
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()
{
long A, B, K;
scan(A, B, K);
long a, b;
a = max(A - K, 0);
K = max(K - A, 0);
b = max(B - K, 0);
writeln(a, " ", b);
}
|
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(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
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; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
int x = scan!int;
int y = x;
foreach(k; 3 .. 1000){
if((y * k) % 360 == 0){
k.print;
return;
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto S = RD!string;
long ans;
long last;
long lastCnt;
foreach (i; 1..S.length)
{
if (S[i-1..i+1] == "><" || S[i-1..i+1] == "<>")
{
auto cnt = i - last;
ans += cnt * (cnt+1) / 2;
if (S[i-1..i+1] == "><")
ans -= min(cnt, lastCnt);
last = i;
lastCnt = cnt;
}
debug writeln("i:", i, " ans:", ans);
}
auto cnt = S.length - last;
ans += cnt * (cnt+1) / 2;
if (S[$-1] == '>')
ans -= min(cnt, lastCnt);
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
void solve(){
}
void main(){
string s = instr();
writeln( s==s.dup.reverse.to!string?"Yes":"No" );
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
alias instr = () => readln().chomp();
T inelm(T= int)(){ return to!(T)( readln().chomp() ); }
T[] inary(T = int)(){ return readln().chomp().split().to!(T[])(); }
|
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 A, B, M;
scan(A, B, M);
auto a = aryread();
auto b = aryread();
long ans = a.reduce!(min)() + b.reduce!(min)();
foreach (i; 0 .. M)
{
long x, y, c;
scan(x, y, c);
ans = min(ans, a[x - 1] + b[y - 1] - c);
}
writeln(ans);
}
|
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 str = line.chomp;
if (str == "#") break;
int s, cnt;
foreach (e; str) {
if ((e >= 'a' && e <= 'g') || (e >= 'q' && e <= 't') ||
(e >= 'v' && e <= 'x') || (e == 'z')) {
if (s == -1) cnt++;
s = 1;
} else {
if (s == 1) cnt++;
s = -1;
}
}
cnt.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() {
int n = readint;
auto a = new long[5];
for (int i = 0; i < n; i++) {
auto s = read!string;
switch (s[0]) {
case 'M': a[0]++; break;
case 'A': a[1]++; break;
case 'R': a[2]++; break;
case 'C': a[3]++; break;
case 'H': a[4]++; break;
default: break;
}
}
long ans = 0;
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
for (int k = j + 1; k < 5; k++) {
ans += a[i] * a[j] * a[k];
}
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string;
void main() {
readln.chomp.replace(",", " ").writeln;
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto N = readln.chomp.to!int;
auto A = readln.chomp.map!(c => cast(int)(c - '1')).array;
int solve() {
bool isOddTriangle(T)(long N, T arr) {
bool isOdd;
foreach(i, a; arr) {
if (a != 1) continue;
auto k = i;
if ((N & k) == k) isOdd ^= true;
debug [N, k, N&k, isOdd].writeln;
}
return isOdd;
}
if (isOddTriangle(N-1, A)) return 1;
if (A.any!(a => a == 1)) return 0;
return isOddTriangle(N-1, A.map!"a/2".array) ? 2 : 0;
}
solve().writeln();
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto ip = readln.split.to!(int[]), A = ip[0], B = ip[1], C = ip[2], D = ip[3];
if(A+B > C+D){
writeln("Left");
} else if(A+B < C+D){
writeln("Right");
} else {
writeln("Balanced");
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm;
void main() {
int a,b;
scan(a,b);
writeln(a*b % 2 ? "Odd" : "Even");
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import core.bitop, 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 K = lread();
long[] ans;
void f(long l, long p)
{
// writefln("%s %s", l, p);
if (l == 0)
{
ans ~= p;
return;
}
if (p == -1)
{
foreach (i; 1 .. 10)
{
f(l - 1, i);
}
return;
}
long x = p % 10;
f(l - 1, p * 10 + x);
if (x != 0)
f(l - 1, p * 10 + (x - 1));
if (x != 9)
f(l - 1, p * 10 + (x + 1));
}
foreach (i; 1 .. 11)
f(i, -1);
ans.sort();
// writeln(ans[0 .. 30], ans.length);
writeln(ans[K - 1]);
}
|
D
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
import std.range;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int A, B;
sc.scan(A, B);
if (B % A == 0)
writeln(A + B);
else
writeln(B - A);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias SugarWater = Tuple!(long, "swater", long, "sugar");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long l, r;
scan(l, r);
if(r - l >= 2019)
{
writeln(0);
return;
}
long minmod = 2019;
foreach(i; l .. r)
{
foreach(j; i + 1 .. r + 1)
{
auto mod = (i * j) % 2019;
minmod = min(mod, minmod);
}
}
minmod.writeln();
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm,
std.math, std.array, std.container, std.typecons;
int solve() {
auto s = readln.chomp;
int i=0, j=(s.length-1).to!int;
char x = 'x';
int res = 0;
while(i<j) {
if(s[i]==x&&s[j]==x) { i++; j--; continue; }
if(s[i]==x&&s[j]!=x) { i++; res++; continue; }
if(s[i]!=x&&s[j]==x) { j--; res++; continue; }
if(s[i]!=x&&s[j]!=x) {
if(s[i]==s[j]) { i++; j--; continue; }
else return -1;
}
}
return res;
}
void main() {
solve.writeln;
}
|
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;
auto d=readln.chomp.to!int;
if(a>=b&&c>=d)writeln(b+d);
else if(a>=b&&c<=d)writeln(b+c);
else if(a<=b&&c>=d)writeln(a+d);
else if(a<=b&&c<=d)writeln(a+c);
}
|
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() {
auto d = new int[](4);
foreach (i ; 0 .. 3) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
d[ai]++;
d[bi]++;
}
d.sort();
auto ans = d == [1, 1, 2, 2] ? "YES" : "NO";
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std;
alias Book = Tuple!(int, "cost", int[], "ss");
int calc(int x, Book[] books) {
auto n = books.length;
auto m = books[0].ss.length;
int ans = int.max;
for (int i = 0; i < (1 << n); i++) {
int cost = 0;
auto ss = new int[m];
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
cost += books[j].cost;
foreach (k, s; books[j].ss) {
ss[k] += s;
}
}
}
if (ss.all!(e => e >= x)) {
ans = min(ans, cost);
}
}
return ans == int.max ? -1 : ans;
}
void main() {
int n, m, x; scan(n, m, x);
Book[] books;
foreach (_; 0..n) {
auto t = readints;
books ~= Book(t[0], t[1..$]);
}
writeln(calc(x, books));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
// 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
|
void main()
{
long n = rdElem;
string s = rdStr;
s.count("ABC").writeln;
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
long P = 10^^9+7;
long[1001] 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) * F[N] % P);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
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 x = cast(long)(N / 1.08);
if (cast(long)(x * 1.08) == N)
writeln(x);
else if (cast(long)((x+1) * 1.08) == N)
writeln(x+1);
else
writeln(":(");
stdout.flush;
debug readln;
}
|
D
|
module aoj;
import std.conv;
import std.stdio;
import std.string;
int getInt() { return to!int(chomp(readln())); }
void main() {
int n;
while ((n = getInt()) != 0) {
int sum = 0;
foreach (i; 0 .. n/4) {
sum += getInt();
}
writeln(sum);
}
}
|
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; }
/*
a[i]: i番目を踏むときの最善
漸化式は
a[i] = min(j; i - k .. i)(a[j] + abs(h[i] - h[j]))
*/
void main(){
long n = read.to!long;
long k = read.to!long;
long[] hs = readln.chomp.split.map!(to!long).array;
long[] as = [0];
foreach(i; 1 .. n){
as ~= long.max;
foreach(j; max(0, i - k) .. i){
long a = as[j] + abs(hs[i] - hs[j]);
if(a < as[i]) as[i] = a;
}
}
as[n - 1].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;
enum mod = 1_000_000_007L;
void main() {
int N;
scan(N);
auto S = readln.chomp.to!(char[]);
foreach (i ; 0 .. S.length) {
S[i] = (((S[i] - 'A') + N) % 26 + 'A').to!char;
}
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);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
alias T = Tuple!(int, int);
void main() {
immutable n = readln.strip.to!int;
long res;
int[4] c;
foreach (qid; 0 .. n) {
auto s = readln.strip;
int t1 = (s.back == 'A') ? 1 : 0;
int t2 = (s.front == 'B') ? 1 : 0;
foreach (i; 1 .. s.length) {
if (s[i-1] == 'A' && s[i] == 'B') ++res;
}
c[t1 * 2 + t2]++;
}
while (c[2]) {
c[2]--;
if (c[1]) {
c[1]--;
res++;
}
if (c[3]) {
res += c[3];
c[3] = 0;
}
}
if (c[3]) {
res += c[3] - 1;
if (c[1]) {
c[1]--;
res++;
}
}
writeln (res);
}
|
D
|
void main()
{
long n, k;
rdVals(n, k);
long[] a = rdRow;
long[] b = new long[n+1];
long[] p = new long[n+1];
foreach (i, x; a)
{
b[i+1] = b[i] + x;
p[i+1] = p[i] + max(0, x);
}
long result;
foreach (i; k .. n+1)
{
long diff = b[i] - b[i-k];
long score = p[n] - p[i] + p[i-k] + max(0, diff);
result = max(result, score);
}
result.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, 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 x;
scan(x);
if (x == 3 || x == 5 || x == 7) {
writeln("YES");
}
else {
writeln("NO");
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
void main()
{
string input;
int div;
while ((input = readln().chomp()).length != 0) {
div = input.to!int();
int[] position = iota(600).filter!(a => a % div == 0).array();
int sum;
foreach (pos; position) {
sum += pos * pos * div;
}
writeln(sum);
}
}
|
D
|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
12
---+/
/+---test
5
---+/
/+---test
1000000000000000000
---+/
void main(string[] args) {
const N = readln.chomp.to!long;
if (N%2 == 1) {
0.writeln;
return;
}
size_t ans;
for (long i = 10; i <= N; i*=5) {
ans += N/i;
}
ans.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;
int calc(string s) {
int ans = int.max;
for (int i = 0; i + 3 <= s.length; i++) {
int x = s[i..i + 3].to!int;
ans = min(ans, abs(753 - x));
}
return ans;
}
void main() {
string s = read!string;
writeln(calc(s));
}
|
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 N = lread();
long cnt;
foreach (i; 0 .. N)
{
long l, r;
scan(l, r);
cnt += r - l + 1;
}
writeln(cnt);
}
|
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;
foreach (i; 0 .. 3) {
string s = cin.next!string;
write(s[i]);
}
writeln();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.range;
import std.array;
import std.algorithm;
import std.typecons;
import std.math;
int calc(int x,int r){
return x * (r+100) / 100;
}
void main() {
while(true) {
string[] cin;
cin=split(readln());
int a=to!int(cin[0]),b=to!int(cin[1]),x=to!int(cin[2]);
if(!(a||b||x)) break;
int ans=0;
for(int i=1; i<x; i++) {
for(int j=1; j<x; j++) {
if(calc(i,a)+calc(j,a)==x) {
int z=calc(i,b)+calc(j,b);
if(ans<z) ans=z;
}
}
}
writeln(ans);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
void main() {
readln.chomp.count!"a == '2'".writeln;
}
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.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 solve() {
auto s = readln.split.map!(to!long);
auto A = s[0];
auto B = s[1];
if (A > B) swap(A, B);
long ans = 2 * (A - 1);
long hi = B + 1;
long lo = A;
while (hi - lo > 1) {
long mid = (hi + lo) / 2;
long AB = mid + A;
bool ok = true;
foreach (i; -5..5) {
long a = (A + mid) / 2 + i;
long b = AB - a;
if (a <= A || a > mid) continue;
if (b < A || b >= mid) continue;
if (a * b >= A * B) ok = false;
}
if (ok) lo = mid;
else hi = mid;
}
ans += lo - A;
ans.writeln;
}
void main() {
auto T = readln.chomp.to!int;
while (T--) solve;
}
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
ulong ai, ao, at, aj, al, as, az;
{
ulong[] tmp = readln.chomp.split.map!(to!ulong).array;
ai = tmp[0], ao = tmp[1], at = tmp[2], aj = tmp[3],
al = tmp[4], as = tmp[5], az = tmp[6];
}
ulong ans;
ans += ao;
if(min(ai, aj, al) == 0){
ans += 2 * (ai / 2) + 2 * (aj / 2) + 2 * (al / 2);
}
else{
if(ai % 2 == aj % 2 && aj % 2 == al % 2){
ans += ai + aj + al;
}
else{
ans += ai + aj + al - 1;
}
}
ans.writeln;
}
/*
T, S, Z は捨てる
O はそのまま使うしかない
J, L, I は自分を2個セットで使うか、または3個で使う
使い方は2つ。
・3個でとれるところまでとり、残りを2個セットで使う
・上記より1つ少なくとり、残りを2個セットで使う
min(ai, aj, al) == 0 のとき
各自でがんばるしかない
ans = 2 * (ai / 2) + 2 * (aj / 2) + 2 * (al / 2)
その他のとき
高々1個残しにできる
if(ai % 2 == aj % 2 && aj % 2 == al % 2)
ans = ai + aj + al
else
ans = ai + aj + al - 1
*/
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto H = readln.split.to!(int[])[0];
foreach (A; readln.split.to!(int[])) H -= A;
writeln(H <= 0 ? "Yes" : "No");
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
string s = readln().chomp;
long w = readln().chomp.to!long;
for (int i = 0; i < s.length; i += w)
{
write(s[i]);
}
writeln();
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
A[] -= 1;
long ans;
foreach (i; 0 .. N)
{
if (i < A[i] && A[A[i]] == i)
ans++;
}
writeln(ans);
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto s = readln.strip;
int cur = 1;
foreach (i; 1..n)
{
cur += (s[i - 1] == s[i]);
}
writeln (cur / 2);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main()
{
auto N = to!int(split(readln())[0]);
auto input = split(readln());
auto D = to!int(input[0]), X = to!int(input[1]);
int[] A;
int n = X;
foreach (i; 0..N) {
n += (D-1) / ( to!int(split(readln())[0]) ) + 1;
}
writeln(n);
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
writeln((readln.chomp.split.join.to!int % 4) == 0? "YES" : "NO");
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
readint;
auto a = readints;
auto b = readints;
auto c = readints;
int ans = 0;
for (int i = 0; i < a.length; i++) {
ans += b[a[i] - 1];
if (i + 1 < a.length) {
int x = a[i]; // 食べた料理
int y = a[i + 1]; // 次に食べる料理
if (y == x + 1) {
ans += c[a[i] - 1];
}
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto s = readln.chomp.to!(char[]);
auto K = readln.chomp.to!int;
foreach (ref c; s) {
if (c == 'a') continue;
int i = c-'a';
if (K < 26-i) continue;
c = 'a';
K -= 26-i;
}
if (K) {
s[$-1] = ('a' + ((s[$-1] - 'a').to!int + K) % 26).to!char;
}
writeln(s);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional;
import std.algorithm, std.container;
void main()
{
long N = scanElem;
long[] list = scanArray;
long res;
long m=-1;
foreach(e; list)
{
if(m<=e){
res++;
}
m = max(e, m);
}
writeln(res);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(long[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] primes(long n)
{
if(n<2)return [];
auto table = new long[n+1];
long[] res;
for(int i = 2;i<=n;i++)
{
if(table[i]==-1) continue;
for(int a = i;a<table.length;a+=i)
{
table[a] = -1;
}
res ~= i;
}
return res;
}
bool isPrime(long n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
for (long i = 3; i ^^ 2 <= n; i += 2)
if (n % i == 0)
return false;
return true;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto abc = readln.split.to!(int[]);
auto A = abc[0];
auto B = abc[1];
auto C = abc[2];
auto K = readln.chomp.to!int;
int k;
while (B <= A) {
++k;
B *= 2;
}
while (C <= B) {
++k;
C *= 2;
}
writeln(k <= K ? "Yes" : "No");
}
|
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;
import std.ascii;
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(string s) {
if (s[0] != 'A') return false;
if (s[2..$-1].count('C') != 1) return false;
if (s.count!(c => isUpper(c)) > 2) return false;
return true;
}
void main() {
auto s = readln.chomp;
writeln(calc(s) ? "AC" : "WA");
}
|
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 mod = 10L^^9 + 7;
enum inf = 10^^9;
void main() {
int n;
scan(n);
string s;
scan(s);
int q;
scan(q);
auto k = readln.split.to!(int[]);
foreach (ki ; k) {
auto ans = solve(n, s, ki);
writeln(ans);
}
}
long solve(int n, string s, int k) {
long ans;
long cntd, cntm, dm;
foreach (i ; 0 .. n) {
if (s[i] == 'C') {
ans += dm;
}
if (i >= k - 1) {
if (s[i - (k-1)] == 'D') {
dm -= cntm;
cntd--;
}
else if (s[i - (k-1)] == 'M') {
cntm--;
}
}
if (s[i] == 'D') {
cntd++;
}
else if (s[i] == 'M') {
dm += cntd;
cntm++;
}
}
return 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.conv;
import std.numeric;
long f(long n, long c, long d) {
return n - (n / c) - (n / d) + (n / (c * d / gcd(c, d)));
}
void main() {
long a, b, c, d;
scanf("%ld %ld %ld %ld", &a, &b, &c, &d);
write(f(b, c, d) - f(a-1, c, d));
}
|
D
|
void main()
{
long n = readln.chomp.to!long;
long[] a = new long[n];
long[][] x = new long[][](n), y = new long[][](n);
foreach (i; 0 .. n)
{
a[i] = readln.chomp.to!long;
foreach (j; 0 .. a[i])
{
long[] tmp = readln.split.to!(long[]);
x[i] ~= tmp[0] - 1, y[i] ~= tmp[1];
}
}
long result;
foreach (i; 0 .. (1 << n))
{
bool ok = true;
foreach (j; 0 .. n)
{
if (!((1 << j) & i)) continue;
foreach (k; 0 .. a[j])
{
if (((i >> x[j][k]) & 1) ^ y[j][k]) ok = false;
}
}
if (ok) result = max(result, popcnt(i));
}
result.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;
import core.bitop;
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nkc = readln.split.to!(int[]);
auto N = nkc[0];
auto K = nkc[1];
auto C = nkc[2];
auto S = readln.chomp;
foreach (_; 0..N) S ~= 'x';
auto left = new int[](N);
int last = -1;
foreach (i; 0..N) {
left[i] = last;
if (S[i] == 'o') last = i;
}
auto DP1 = new int[](N*2+1);
foreach (i; 0..N*2) {
DP1[i+1] = max(DP1[i+1], DP1[i]);
if (S[i] == 'o') DP1[i+C+1] = max(DP1[i+C+1], DP1[i] + 1);
}
auto DP2 = new int[][](2, N*2+1);
foreach_reverse (i; 0..N*2) {
if (i-1 >= 0) DP2[0][i-1] = max(DP2[0][i-1], DP2[0][i], DP2[1][i]);
if (i-C-1 >= 0 && S[i-C-1] == 'o') DP2[1][i-C-1] = max(DP2[1][i-C-1], DP2[0][i]+1, DP2[1][i] + 1);
}
foreach (i; 0..N) {
if (S[i] == 'x') continue;
if (left[i] == -1) {
if (DP2[0][i] < K) writeln(i+1);
} else if (left[i] + C >= i) {
auto l = left[i];
if (DP2[1][l] + DP1[l] < K && DP2[0][i] + DP1[i] < K && DP1[i] + DP2[1][i] >= K) writeln(i+1);
} else {
if (DP2[0][i] + DP1[i] < K && DP1[i] + DP2[1][i] >= K) writeln(i+1);
}
}
}
|
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() {
long N;
scan(N);
foreach(h ; 1 .. 3501) {
foreach (n ; 1 .. 3501) {
long x = 4L * h * n;
long hn = 1L * h * n;
//writeln(x, " " , hn);
if (x % N) {
continue;
}
long denom = 4L * h * n / N - n - h;
//writeln(denom);
if (denom <= 0 || hn % denom) {
continue;
}
debug {
//writeln(hn, denom);
}
long w = hn / denom;
if (0 < w && w <= 3500) {
writeln(h, " ", n, " ", w);
return;
}
}
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.algorithm, std.array, std.conv;
void main() {
auto input = readln.chomp.split.map!(to!int).array;
auto a = input[0];
auto b = input[1];
if (a < b) {
"a < b".writeln;
} else if (a > b) {
"a > b".writeln;
} else {
"a == b".writeln;
}
}
|
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;
import std.datetime, std.bigint;
void main() {
int n, k;
int[] a;
scan(n, k);
a = readln.split.to!(int[]);
if (n == 1) {
if (a[0] == k) {
writeln("POSSIBLE");
}
else {
writeln("IMPOSSIBLE");
}
return;
}
int lim = a.reduce!max;
if (k > lim) {
writeln("IMPOSSIBLE");
return;
}
int g = gcd(a[0], a[1]);
foreach (i ; 2 .. n) {
g = gcd(g, a[i]);
}
if (k % g == 0) {
writeln("POSSIBLE");
}
else {
writeln("IMPOSSIBLE");
}
}
int gcd(int a, int b) {
return b > 0 ? gcd(b, a % b) : a;
}
unittest {
assert(gcd(24, 36) == 12);
assert(gcd(2, 5) == 1);
assert(gcd(6, 3) == 3);
assert(gcd(1, 100) == 1);
assert(gcd(12, 15) == 3);
assert(gcd(15, 12) == 3);
}
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.string,
std.array,
std.range,
std.stdio,
std.conv;
void main() {
ulong[] NK = readln.chomp.split.to!(ulong[]);
ulong N = NK[0],
K = NK[1];
writeln(K * (K-1)^^(N-1));
}
|
D
|
void main() {
(700 + rs.count!(a => a == 'o') * 100).writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std;
void main() {
int N, M;
scan(N, M);
auto to1 = new bool[](N);
auto toN = new bool[](N);
foreach (i; 0 .. M) {
int ai, bi;
scan(ai, bi);
ai--, bi--;
if (ai == 0) {
to1[bi] = true;
}
if (bi == N - 1) {
toN[ai] = true;
}
}
bool ok;
foreach (i; 0 .. N) {
if (to1[i] && toN[i]) ok = true;
}
yes(ok, "POSSIBLE", "IMPOSSIBLE");
}
void scan(T...)(ref T args) {
auto line = readln.split; // @suppress(dscanner.suspicious.unmodified)
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args)
if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
auto S = sread();
long N = S.length;
writeln(max(S[0] - '0' - 1 + 9 * (N - 1), S.map!"a-'0'"().sum()));
}
|
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;
enum mod = 1_000_000_007L;
void main() {
int N;
scan(N);
auto a = readln.split.to!(int[]);
int Q;
scan(Q);
auto m = readln.split.to!(int[]);
auto ok = new bool[](20 * 2000 + 1);
foreach (s ; 0 .. (1 << N)) {
int tot;
foreach (i ; 0 .. N) if (s & (1 << i)) tot += a[i];
ok[tot] = true;
}
foreach (i ; 0 .. Q) {
writeln(ok[m[i]] ? "yes" : "no");
}
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
void main()
{
long n = rdElem;
long[] a = rdRow;
long total;
long[long] lists;
++lists[total];
foreach (i; 0 .. n)
{
total += a[i];
++lists[total];
}
long result;
foreach (x; lists.byValue)
{
result += x * (x - 1) / 2;
}
result.writeln;
}
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 wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.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!size_t;
auto ai = readln.split;
ai.reverse();
writeln(ai.join(" "));
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n, k;
rd(n, k);
auto a = readln.split.to!(long[]);
long[] bits;
foreach (i; 0 .. n) {
long s = 0;
foreach (j; i .. n) {
s += a[j];
bits ~= s;
}
}
auto good = new bool[](bits.length);
fill(good, true);
foreach_reverse (i; 0 .. 42) {
int m = 0;
foreach (j, bit; bits) {
if (good[j]) {
if (bit & (1L << i)) {
m++;
}
}
}
if (m >= k) {
foreach (j, bit; bits) {
if (good[j]) {
if (!(bit & (1L << i))) {
good[j] = false;
}
}
}
if (m == k) {
long ans = (1L << 42) - 1;
foreach (j, bit; bits) {
if (good[j]) {
ans &= bit;
}
}
writeln(ans);
return;
}
}
}
long ans = (1L << 42) - 1;
foreach (j, bit; bits) {
if (good[j]) {
ans &= bit;
if (--k == 0) {
writeln(ans);
return;
}
}
}
import std.exception : enforce;
enforce(false);
}
void rd(T...)(ref T x) {
import std.stdio : readln;
import std.string : split;
import std.conv : to;
auto l = readln.split;
assert(l.length == x.length);
foreach (i, ref e; x)
e = l[i].to!(typeof(e));
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
ulong bignum = 1_000_000_007;
alias Cake = Tuple!(long, "beauty", long, "tasty", long, "popular");
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 = lread();
auto a = new long[](n);
foreach (ref e; a)
{
e = lread();
}
auto sorted_a = a.dup.sort!"a > b"();
// sorted_a.writeln();
auto max1 = sorted_a[0];
auto max2 = sorted_a.dropOne[0];
// sorted_a.dropOne.writeln();
foreach (e; a)
{
if(e != max1)
max1.writeln();
else
max2.writeln();
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
void main()
{
auto xab = readln.split.to!(long[]);
writeln( xab[1] - xab[2] >= 0 ? "delicious" : xab[1] + xab[0] >= xab[2] ? "safe" : "dangerous" );
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.algorithm, std.range, std.conv, std.math;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.geo.primitive, dcomp.geo.circle, dcomp.geo.polygon;
bool solve() {
alias P = Point2D!double;
alias L = Line2D!double;
alias C = Circre2D!double;
int n;
sc.read(n);
if (!n) return false;
C[] c;
foreach (i; 0..n) {
double x, y, r;
sc.read(x, y, r);
c ~= C(P(x, y), r);
}
int m;
sc.read(m);
foreach (i; 0..m) {
double tx, ty, sx, sy;
sc.read(tx, ty, sx, sy);
P t = P(tx, ty), s = P(sx, sy);
bool f = true;
foreach (d; c) {
if (crossSC(L(t, s), d)) {
f = false;
break;
}
}
if (!f) {
writeln("Safe");
} else {
writeln("Danger");
}
}
return true;
}
int main() {
EPS!double = 1e-10;
while (solve()) {}
return 0;
}
Scanner sc;
static this() {
sc = new Scanner(stdin);
}
/* 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;}
alias opDollar = length;
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/geo/polygon.d */
// module dcomp.geo.polygon;
// import dcomp.geo.primitive;
inout(Point2D!R) at(R)(inout Point2D!R[] pol, size_t i) {
return pol[i<pol.length?i:i-pol.length];
}
int contains(R)(Point2D!R[] pol, Point2D!R p) {
import std.algorithm : swap;
int res = -1;
foreach (i; 0..pol.length) {
auto a = pol.at(i) - p, b = pol.at(i+1) - p;
if (ccw(a, b, Point2D!R(0, 0)) == 0) return 1;
if (a.y > b.y) swap(a, b);
if (a.y <= 0 && 0 < b.y) {
if (cross(a, b) < 0) res *= -1;
}
}
return res+1;
}
R area2(R)(Point2D!R[] pol) {
R u = 0;
foreach (i; 0..pol.length) {
auto a = pol.at(i), b = pol.at(i+1);
u += cross(a, b);
}
return u;
}
// import dcomp.array;
Point2D!R[] convex(R)(Point2D!R[] _pol) {
import std.algorithm : sort;
import std.range : retro, array;
auto pol = _pol.dup;
pol.sort!((a, b) => robustCmp(a, b) == -1);
if (pol.length <= 2) return pol;
FastAppender!(Point2D!R[]) up;
foreach (d; pol) {
while (up.length >= 2 && ccw(up[$-2], up[$-1], d) == 1) up.removeBack();
up ~= d;
}
FastAppender!(Point2D!R[]) down;
foreach (d; pol) {
while (down.length >= 2 && ccw(down[$-2], down[$-1], d) == -1) down.removeBack();
down ~= d;
}
return up.data.retro.array[1..$-1] ~ down.data();
}
/* 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/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;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/geo/circle.d */
// module dcomp.geo.circle;
// import dcomp.geo.primitive;
struct Circre2D(R) {
Point2D!R p;
R r;
this(Point2D!R p, R r) {
this.p = p;
this.r = r;
}
}
int crossSC(R)(Line2D!R l, Circre2D!R c) {
R mi = distSP(l, c.p);
if (sgn(mi - c.r) == 1) return 0;
if (sgn(c.r - mi) == 0) return 1;
int u = 0;
if (sgn((l.x-c.p).abs - c.r) != -1) u++;
if (sgn((l.y-c.p).abs - c.r) != -1) u++;
return u;
}
/* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/geo/primitive.d */
// module dcomp.geo.primitive;
import std.traits;
template EPS(R) {
R EPS;
}
int sgn(R)(R a) {
static if (isFloatingPoint!R) {
import std.math : isNaN;
assert(!isNaN(EPS!R));
}
if (a < -EPS!R) return -1;
if (a > EPS!R) return 1;
return 0;
}
struct Point2D(T) {
T[2] d;
this(T x, T y) {this.d = [x, y];}
this(T[2] d) {this.d = d;}
@property ref inout(T) x() inout {return d[0];}
@property ref inout(T) y() inout {return d[1];}
ref inout(T) opIndex(size_t i) inout {return d[i];}
auto opOpAssign(string op)(in Point2D r) {
return mixin("this=this"~op~"r");
}
auto opBinary(string op:"+")(in Point2D r) const {return Point2D(x+r.x, y+r.y);}
auto opBinary(string op:"-")(in Point2D r) const {return Point2D(x-r.x, y-r.y);}
static if (isFloatingPoint!T) {
T abs() {
import std.math : sqrt;
return (x*x+y*y).sqrt;
}
T arg() {
import std.math : atan2;
return atan2(y, x);
}
Point2D rot(T ar) {
import std.math : cos, sin;
auto cosAr = cos(ar), sinAr = sin(ar);
return Point2D(x*cosAr - y*sinAr, x*sinAr + y*cosAr);
}
}
}
int robustCmp(T)(Point2D!T a, Point2D!T b) {
if (sgn(a.x-b.x)) return sgn(a.x-b.x);
if (sgn(a.y-b.y)) return sgn(a.y-b.y);
return 0;
}
bool near(T)(Point2D!T a, Point2D!T b) if (isIntegral!T) {
return a == b;
}
bool near(T)(Point2D!T a, Point2D!T b) if (isFloatingPoint!T) {
return !sgn((a-b).abs);
}
T dot(T)(in Point2D!T l, in Point2D!T r) {
return l[0]*r[0] + l[1]*r[1];
}
T cross(T)(in Point2D!T l, in Point2D!T r) {
return l[0]*r[1] - l[1]*r[0];
}
int ccw(R)(Point2D!R a, Point2D!R b, Point2D!R c) {
import std.stdio;
assert(!near(a, b));
if (near(a, c) || near(b, c)) return 0;
int s = sgn(cross(b-a, c-a));
if (s) return s;
if (dot(b-a, c-a) < 0) return 2;
if (dot(a-b, c-b) < 0) return -2;
return 0;
}
struct Line2D(R) {
Point2D!R x, y;
this(Point2D!R x, Point2D!R y) {
this.x = x;
this.y = y;
}
Point2D!R vec() const { return y-x; }
}
R distLP(R)(Line2D!R l, Point2D!R p) if (isFloatingPoint!R) {
import std.math : abs;
return abs(cross(l.vec, p-l.x) / l.vec.abs);
}
R distSP(R)(Line2D!R s, Point2D!R p) if (isFloatingPoint!R) {
import std.algorithm : min;
auto s2 = Point2D!R(-s.vec.y, s.vec.x);
if (ccw(s.x, s.x+s2, p) == 1) return (s.x-p).abs;
if (ccw(s.y, s.y+s2, p) == -1) return (s.y-p).abs;
return min((s.x-p).abs, (s.y-p).abs, distLP(s, p));
}
int argcmp(T)(Point2D!T l, Point2D!T r) if (isIntegral!T) {
int sgn(Point2D!T p) {
if (p[1] < 0) return -1;
if (p[1] > 0) return 1;
if (p[0] < 0) return 2;
return 0;
}
int lsgn = sgn(l);
int rsgn = sgn(r);
if (lsgn < rsgn) return -1;
if (lsgn > rsgn) return 1;
T x = cross(l, r);
if (x > 0) return -1;
if (x < 0) return 1;
return 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.stdio;
import std.conv;
import std.string;
void main(string[] args){
int n = to!int(readln().chomp());
for(int i = 0; i < n; i ++){
string[] cs = readln().chomp().split(" ");
// x1 y1 x2 y2 x3 y3 x4 y4
double dx1 = to!double(cs[0]) - to!double(cs[2]);
double dx2 = to!double(cs[4]) - to!double(cs[6]);
double dy1 = to!double(cs[1]) - to!double(cs[3]);
double dy2 = to!double(cs[5]) - to!double(cs[7]);
if(dx1 * dy2 - dx2 * dy1 < 0.00000000001 && dx1 * dy2 - dx2 * dy1 > -0.00000000001) writeln("YES"); else writeln("NO");
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto m = RD!int;
auto s = new char[][](n);
foreach (i; 0..n)
{
s[i] = RD!(char[]);
}
bool visit(int y, int x)
{
if (!inside(y, 0, n) || !inside(x, 0, m)) return true;
if (s[y][x] == 'G') return false;
if (s[y][x] == '.')
{
s[y][x] = '#';
}
return true;
}
bool ok = true;
int[][] good;
(){
foreach (int y; 0..n)
{
foreach (int x; 0..m)
{
if (s[y][x] == 'B')
{
ok &= visit(y-1, x);
ok &= visit(y+1, x);
ok &= visit(y, x-1);
ok &= visit(y, x+1);
if (!ok) return;
}
else if (s[y][x] == 'G')
{
good ~= [y, x];
}
}
}}();
if (!ok) continue;
int[][] open;
if (s[n-1][m-1] != '#')
{
open ~= [[n-1, m-1]];
}
auto dist = new int[][](n, m);
foreach (i; 0..n)
{
dist[i][] = int.max;
}
dist[n-1][m-1] = 0;
while (!open.empty)
{
auto nd = open.front; open.popFront;
auto y = nd[0];
auto x = nd[1];
if (y != 0)
{
auto ny = y-1;
auto nx = x;
if (s[ny][nx] != '#')
{
if (dist[y][x]+1 < dist[ny][nx])
{
dist[ny][nx] = dist[y][x]+1;
open ~= [ny, nx];
}
}
}
if (y != n-1)
{
auto ny = y+1;
auto nx = x;
if (s[ny][nx] != '#')
{
if (dist[y][x]+1 < dist[ny][nx])
{
dist[ny][nx] = dist[y][x]+1;
open ~= [ny, nx];
}
}
}
if (x != 0)
{
auto ny = y;
auto nx = x-1;
if (s[ny][nx] != '#')
{
if (dist[y][x]+1 < dist[ny][nx])
{
dist[ny][nx] = dist[y][x]+1;
open ~= [ny, nx];
}
}
}
if (x != m-1)
{
auto ny = y;
auto nx = x+1;
if (s[ny][nx] != '#')
{
if (dist[y][x]+1 < dist[ny][nx])
{
dist[ny][nx] = dist[y][x]+1;
open ~= [ny, nx];
}
}
}
}
foreach (e; good)
{
auto y = e[0];
auto x = e[1];
if (dist[y][x] == int.max)
{
ok = false;
break;
}
}
ans[ti] = ok;
}
foreach (e; ans)
writeln(e ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
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 K, A, B;
scan(K, A, B);
if (B < A)
{
writeln(1 + K);
}
else
{
long k = K;
k -= A - 1;
long r = A + (B - A) * (k / 2) + (k & 1);
max(1 + K, r).writeln();
}
}
|
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 m, k; readV(m, k);
if (m == 1) {
writeln(k == 0 ? "0 0 1 1" : "-1");
return;
}
if (k >= 2^^m) {
writeln(-1);
return;
}
foreach (i; 0..2^^m)
if (i != k) write(i, " ");
write(k, " ");
foreach_reverse (i; 0..2^^m)
if (i != k) write(i, " ");
writeln(k);
}
|
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, c, d; readV(a, b, c, d);
writeln(max(a*b, c*d));
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string my_readln() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto tokens = my_readln.split;
auto N = tokens[0].to!uint;
auto K = tokens[1].to!uint;
auto x = my_readln.split.to!(long[]);
long pos = N;
foreach (i, e; x)
{
if (e >= 0)
{
pos = i;
break;
}
}
long begin = max(0, pos - K), end = min(pos + 1, N - K + 1);
ulong ans = ulong.max;
foreach (i; begin..end)
{
ulong l, r;
if (x[i] < 0) l = abs(x[i]);
if (x[i+K-1] > 0) r = x[i+K-1];
ulong d = min(l, r) * 2 + max(l, r);
ans = min(ans, d);
//stderr.writeln(l, ":", r, " ", d);
}
writeln(ans);
stdout.flush();
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
int x = r.next!uint;
const n = r.next!uint;
const m = r.next!uint;
foreach (i; 0 .. n) {
const y = (x / 2) + 10;
if (y >= x) break;
x = y;
}
x -= m * 10;
writeln (x > 0 ? "NO" : "YES");
}
}
|
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;
immutable long INF = 1L << 59;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto K = s[1];
auto A = readln.split.map!(to!int).array;
int x1 = A.count(1).to!int;
int x2 = A.count(-1).to!int;
int ans = 0;
foreach (i; 0..N) {
int cnt1 = 0, cnt2 = 0;
foreach (j; 0..N) {
if (abs(j - i) % K == 0) {
if (A[j] == 1) cnt1 += 1;
else cnt2 += 1;
}
}
ans = max(ans, abs((x1 - cnt1) - (x2 - cnt2)));
}
ans.writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.stdc.stdlib;
import core.stdc.string;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }
struct R {
int x;
void set(int mask, bool res) {
if (res) x |= 1 << mask;
else x &= ~(1 << mask);
}
void set(bool a, bool b, bool c, bool d, bool res) {
int mask = (a << 3) | (b << 2) | (c << 1) | d;
set(mask, res);
}
bool get(int mask) {
return (x & (1 << mask)) != 0;
}
bool get(bool a, bool b, bool c, bool d) {
int mask = (a << 3) | (b << 2) | (c << 1) | d;
return get(mask);
}
}
bool eval(in string s, bool a, bool b, bool c, bool d) {
int i = 0;
bool expr() {
switch (s[i++]) {
case '0': return false;
case '1': return true;
case 'a': return a;
case 'b': return b;
case 'c': return c;
case 'd': return d;
case '-': return !expr();
case '(':
auto l = expr();
char op = s[i++];
assert(op == '*' || op == '^');
auto r = expr();
assert(s[i] == ')');
i++;
return (op == '*' ? l & r : l ^ r);
default:
assert(false);
}
}
return expr();
}
void main() {
auto dp = new int[1<<16];
const huge = 35;
dp[] = huge;
// 0
dp[0] = 1;
// 1
{
R r;
for (int mask = 0; mask < 16; mask++) { r.set(mask, true); }
dp[r.x] = 1;
}
// a, b, c, d
{
auto bs = [false, true];
R A, B, C, D;
foreach (a; bs) {
foreach (b; bs) {
foreach (c; bs) {
foreach (d; bs) {
A.set(a, b, c, d, a);
B.set(a, b, c, d, b);
C.set(a, b, c, d, c);
D.set(a, b, c, d, d);
}
}
}
}
dp[A.x] = dp[B.x] = dp[C.x] = dp[D.x] = 1;
}
bool updated = true;
auto idx = iota(0, 1<<16, 1).array;
while (updated) {
updated = false;
idx.sort!((i, j) => dp[i] < dp[j]);
for (int i = 0; i < (1<<16); i++) {
auto x = dp[i];
if (x == huge) continue;
if (dp[~i & (1<<16) - 1] > x + 1) {
dp[~i & (1<<16) - 1] = x + 1;
updated = true;
}
for (int k = 0; k < (1<<16); k++) {
int j = idx[k];
auto y = dp[j];
if (x + y + 3 >= huge) break;
if (dp[i ^ j] > x + y + 3) {
dp[i ^ j] = x + y + 3;
updated = true;
}
if (dp[i & j] > x + y + 3) {
dp[i & j] = x + y + 3;
updated = true;
}
}
}
}
string s;
bool input() {
s = readln.chomp;
if (s == ".") return false;
return true;
}
void solve() {
R r;
const bs = [false, true];
foreach (a; bs) {
foreach (b; bs) {
foreach (c; bs) {
foreach (d; bs) {
r.set(a, b, c, d, eval(s, a, b, c, d));
}
}
}
}
writeln(dp[r.x]);
}
while (input()) solve();
}
|
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 N = lread();
auto a = aryread();
long c;
while (a.any!(x => x != 0))
{
loop: foreach (i, v; a)
{
if (v != 0)
{
c++;
size_t j = i;
while (a[j] != 0 && j < a.length)
{
a[j]--;
j++;
}
break loop;
}
}
}
writeln(c);
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.random, std.array;
// const int n=10;
// auto rnd=Random(unpredictableSeed);
// auto a=new char[](n), b=new char[](n);
// foreach(i; 0..n){
// a[i]=(uniform!"[]"(0, 1, rnd)+'0').to!(char);
// b[i]=(uniform!"[]"(0, 1, rnd)+'0').to!(char);
// }
int n; rd(n);
auto a=readln.chomp.to!(char[]);
auto b=readln.chomp.to!(char[]);
long solve(){
long p, q, r, s;
foreach(i; 0..n){
if(a[i]=='0' && b[i]=='0') p++;
else if(a[i]=='0' && b[i]=='1') q++;
else if(a[i]=='1' && b[i]=='0') r++;
else s++;
}
return p*r+p*s+q*r;
}
long brute(){
auto _a=a.map!((e)=>(e-'0')).array, _b=b.map!((e)=>(e-'0')).array;
long ret=0;
foreach(i; 0..n)foreach(j; 0..i){
int o1=_a[i]|_b[i], o2=_a[j]|_b[j];
if(o1!=(_a[j]|_b[i]) || o2!=(_a[i]|_b[j])) ret++;
}
return ret;
}
// if (solve()!=brute()){
// writeln(a);
// writeln(b);
// writeln(solve());
// writeln(brute());
// }
writeln(solve());
}
/*
0 0 p
0 1 q
1 0 r
1 1 s
*/
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, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.ascii;
void main() {
int k;
long[] a;
scan(k);
a = readln.split.to!(long[]);
auto u = new long[](k), b = new long[](k);
if (a[k-1] == 2) {
u[k-1] = 3;
b[k-1] = 2;
}
else {
writeln(-1);
return;
}
foreach_reverse (i ; 0 .. k - 1) {
u[i] = (u[i+1]/a[i])*a[i] + a[i] - 1;
b[i] = ((b[i+1] + a[i] - 1) / a[i]) * a[i];
if (b[i] > u[i]) {
writeln(-1);
return;
}
}
debug {
writeln(u);
writeln(b);
}
writeln(b[0], " ", u[0]);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio ,std.conv , std.string;
void main(){
auto input = readLine!size_t();
solve(input[0],input[1]).writeln();
}
auto solve( size_t x, size_t y ) {
if( x < y ){
return 0;
} else {
return x-y;
}
}
unittest{
assert(1);
}
T[] readLine( T )(){
T[] ret;
foreach( val ; readln().chomp().split() ){
ret ~= to!T(val);
}
return ret;
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
int[] a = readln.chomp.split.to!(int[]);
writeln((a[0] + a[1] >= a[2]) ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main() {
auto data = readln().split();
auto M = data[1].to!long(), N = data[0].to!long();
long[] A, B;
foreach (i; 0 .. M) {
data = readln().split();
B ~= data[1].to!long(), A ~= data[0].to!long();
}
long[] list = N.iota().array;
long[] size = repeat(1L, N).array;
ulong root(ulong island) {
auto parent = list[island];
return parent == island ? island : ( list[island] = root(list[island]) ) ;
}
ulong unite(ulong island1, ulong island2) {
auto root1 = root(island1), root2 = root(island2);
if (root1 == root2) return 0;
else {
auto result = size[root1] * size[root2];
list[root1] = root2;
size[root2] += size[root1];
return result;
}
}
long[] results; results ~= N * (N-1) / 2;
foreach_reverse(bridge; 1 .. M) {
results ~= results[$-1] - unite(A[bridge]-1, B[bridge]-1);
}
foreach_reverse(num; results) { writeln(num); }
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}
T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;}
alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;}
struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}}
struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}}
//END OF TEMPLATE
void main(){
ulong n,k;
scan(n,k);
ulong sum;
foreach(i;k..n+2){
sum=(sum+i*(n+1-i)+1)%(10^^9+7);
}
sum.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
void main() {
string[] inputs = split(readln());
int x = to!int(inputs[0]);
int a = to!int(inputs[1]);
int b = to!int(inputs[2]);
if(abs(x - a) < abs(x - b)) 'A'.writeln;
else 'B'.writeln;
}
|
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 s = RD!string;
long last;
long cnt;
foreach (i; 0..s.length)
{
if (s[i] == '-')
--cnt;
else
++cnt;
if (cnt < last)
{
--last;
ans[ti] += i+1;
}
}
ans[ti] += s.length;
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
bool calc(string s) {
for (int i = 0; i < s.length; i += 2) {
switch (s[i]) {
case 'R':
case 'U':
case 'D':
break;
default:
return false;
}
}
for (int i = 1; i < s.length; i += 2) {
switch (s[i]) {
case 'L':
case 'U':
case 'D':
break;
default:
return false;
}
}
return true;
}
void main() {
string s = read!string;
writeln(calc(s) ? "Yes" : "No");
}
|
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){
int n = read.to!int;
int k = read.to!int;
long[] as;
foreach(i; 0 .. n) as ~= read.to!long;
long best = as[k] - as[0];
int bestleft = 0;
foreach(i; 0 .. n - k){
long tmp = as[i + k] - as[i];
if(tmp < best) best = tmp, bestleft = i;
}
long ans = as[bestleft] + best / 2;
ans.writeln;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto nm = readints;
int n = nm[0], m = nm[1];
auto xs = readints;
auto uf = new UnionFind(n + 1);
for (int i = 0; i < m; i++) {
auto xy = readints;
uf.unite(xs[xy[0] - 1], xs[xy[1] - 1]);
}
int ans = 0;
foreach (i; 1 .. n + 1) {
if (uf.isSame(xs[i - 1], i)) {
ans++;
}
}
writeln(ans);
}
class UnionFind {
private int[] _data;
private int[] _nexts; // 次の要素。なければ -1
private int[] _tails; // 末尾の要素
this(int n) {
_data = new int[](n + 1);
_nexts = new int[](n + 1);
_tails = new int[](n + 1);
_data[] = -1;
_nexts[] = -1;
for (int i = 0; i < _tails.length; i++)
_tails[i] = i;
}
int root(int a) {
if (_data[a] < 0) return a;
return _data[a] = root(_data[a]);
}
bool unite(int a, int b) {
int ra = root(a);
int rb = root(b);
if (ra == rb) return false;
// ra に rb を繋げる
_data[ra] += _data[rb];
_data[rb] = ra;
// ra の末尾の後ろに rb を繋げる
_nexts[_tails[ra]] = rb;
// ra の末尾が rb の末尾になる
_tails[ra] = _tails[rb];
return true;
}
bool isSame(int a, int b) {
return root(a) == root(b);
}
/// a が属する集合のサイズ
int groupSize(int a) {
return -_data[root(a)];
}
/// a が属する集合の要素全て
void doEach(int a, void delegate(int) fn) {
auto node = root(a);
while (node != -1) {
fn(node);
node = _nexts[node];
}
}
}
|
D
|
void main() {
auto A = ri, B = ri;
writeln(6-(A+B));
}
// ===================================
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.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }
void main(string[] args){
if(args.length > 1 && args[1] == "-debug"){
if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
else DEBUG_LEVEL = 1;
}
int n = read.to!int;
int a = n / 100, b = n % 100;
bool x = (a >= 1 && a <= 12), y = (b >= 1 && b <= 12);
if(x && y) writeln("AMBIGUOUS");
if(! x && y) writeln("YYMM");
if(x && ! y) writeln("MMYY");
if(! x && ! y) writeln("NA");
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
string RD() { return chomp(readln()); }
long mod = pow(10, 9) + 7;
long moda(long x, long y) { return (x + y) % mod; }
long mods(long x, long y) { return ((x + mod) - (y % mod)) % mod; }
long modm(long x, long y) { return (x * y) % mod; }
void main()
{
auto tokens = RD.split;
auto H = tokens[0].to!long;
auto W = tokens[1].to!long;
string[] s;
foreach (i; 0..H)
{
s ~= RD;
}
bool visit(long i, long j)
{
if (i < 0 || i >= H || j < 0 || j >= W) return false;
return s[i][j] == '#';
}
bool cant = false;
foreach (i; 0..H)
{
foreach (j; 0..W)
{
if (s[i][j] == '.') continue;
bool r = false;
r |= visit(i-1, j);
r |= visit(i+1, j);
r |= visit(i, j-1);
r |= visit(i, j+1);
if (!r) cant = true;
}
}
writeln(cant ? "No" : "Yes");
stdout.flush();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.conv;
import std.datetime;
import std.numeric;
import std.math;
import std.string;
string my_readln() { return chomp(readln()); }
void main()
{//try{
auto tokens = split(my_readln());
auto N = to!ulong(tokens[0]);
ulong big, result;
foreach (i; 0..N)
{
auto p = to!ulong(my_readln());
result += p;
if (p > big)
{
big = p;
}
}
result -= big / 2;
writeln(result);
stdout.flush();
/*}catch (Throwable e)
{
writeln(e.toString());
}
readln();*/
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.