code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
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 h1 = scan!int, m1 = scan!int, h2 = scan!int, m2 = scan!int, k = scan!int;
int x1 = h1 * 60 + m1, x2 = h2 * 60 + m2;
int ans = x2 - k - x1;
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
immutable inf = 10^^9 + 7;
void main() {
int n;
scan(n);
bool[] p = [true, true, false];
foreach (i ; 0 .. n) {
int win;
scan(win);
win--;
if (!p[win]) {
writeln("NO");
return;
}
foreach (j ; 0 .. 3) {
if (j != win) {
p[j] ^= 1;
}
}
}
writeln("YES");
}
struct UnionFind {
private {
int N;
int[] p;
int[] rank;
}
this (int n) {
N = n;
p = iota(N).array;
rank = new int[](N);
}
int find_root(int x) {
if (p[x] != x) {
p[x] = find_root(p[x]);
}
return p[x];
}
bool same(int x, int y) {
return find_root(x) == find_root(y);
}
void unite(int x, int y) {
int u = find_root(x), v = find_root(y);
if (u == v) return;
if (rank[u] < rank[v]) {
p[u] = v;
}
else {
p[v] = u;
if (rank[u] == rank[v]) {
rank[u]++;
}
}
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
struct Queue(T) {
private {
int N, head, tail;
T[] data;
}
this(int n) {
N = n + 1;
data = new T[](N);
}
bool empty() {
return head == tail;
}
bool full() {
return (tail + 1) % N == head;
}
T front() {
return data[head];
}
void push(T x) {
assert(!full);
data[tail++] = x;
tail %= N;
}
void pop() {
assert(!empty);
head = (head + 1) % N;
}
void clear() {
head = tail = 0;
}
}
|
D
|
//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 N, M, C;
sc.scan(N, M, C);
int[]B = new int[M];
foreach (i; 0 .. M) {
sc.scan(B[i]);
}
int res = 0;
foreach (i; 0 .. N) {
int tmp = 0;
int A;
foreach (j; 0 .. M) {
sc.scan(A);
tmp += A * B[j];
}
tmp += C;
if (tmp > 0)
res++;
}
writeln(res);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
struct UnionFind(T)
{
void init(T n) { par = new T[](n); foreach (i; 0..n) par[i] = i; cnt = new T[](n); fill(cnt, 1); }
T root(T i) { return par[i] == i ? i : (par[i] = root(par[i])); }
bool same(T i, T j) { return root(i) == root(j); }
void unite(T i, T j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }
T size(T i) { return cnt[root(i)]; }
T[] par, cnt;
}
void main()
{
auto N = RD;
auto Q = RD;
UnionFind!long uf;
uf.init(N);
foreach (i; 0..Q)
{
auto t = RD;
auto u = RD;
auto v = RD;
if (t == 0)
{
uf.unite(u, v);
}
else
{
auto r = uf.same(u, v);
writeln(r ? 1 : 0);
}
}
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!T;r.popFront;}}
void main()
{
int n; readV(n);
int k; readV(k);
int[] x; readA(n, x);
auto r = 0;
foreach (xi; x) r += min(xi, k-xi);
writeln(r*2);
}
|
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;
string s;
void main() {
scan(s);
if (s.canFind('N') == s.canFind('S') && s.canFind('W') == s.canFind('E')) {
writeln("Yes");
}
else {
writeln("No");
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.numeric;
import std.string;
void main() {
auto s = readln.chomp;
auto t = new int[4];
foreach (i, j; s) {
t[i] = j.to!int - '0'.to!int;
}
foreach(i; 0..2) {
foreach(j; 0..2) {
foreach(k; 0..2) {
int a = t[0], b, c, d;
if (i == 0) b = t[1];
else b = -t[1];
if (j == 0) c = t[2];
else c = -t[2];
if (k == 0) d = t[3];
else d = -t[3];
if (a + b + c + d == 7) {
char op1, op2, op3;
if (i == 0) op1 = '+';
else op1 = '-';
if (j == 0) op2 = '+';
else op2 = '-';
if (k == 0) op3 = '+';
else op3 = '-';
writeln(t[0], op1, t[1], op2, t[2], op3, t[3], "=7");
return;
}
}
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto a = readln.split.to!(long[]);
auto dp = new long[][](N, 3);
fillAll(dp, -infl);
dp[N - 1][2] = a[N - 1];
dp[N - 2][0] = max(a[N - 1], a[N - 2]);
foreach_reverse (i ; 0 .. N - 2) {
if ((N - i) % 2 == 0) {
dp[i][0] = max(a[i] + dp[i + 2][0],
a[i + 1] + (i + 3 < N ? dp[i + 3][2] : 0));
}
else {
dp[i][1] = max(a[i] + dp[i + 2][1],
a[i + 1] + (i + 3 < N ? dp[i + 3][0] : 0),
a[i + 2] + (i + 4 < N ? dp[i + 4][2] : 0));
}
dp[i][2] = a[i] + dp[i + 2][2];
}
debug {
writefln("%(%s\n%)", dp);
}
long ans;
if (N % 2 == 0) {
ans = dp[0][0];
}
else {
ans = dp[0][1];
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ks = readln.split.to!(int[]);
auto K = ks[0];
auto S = ks[1];
int r;
foreach (i; 0..K+1) {
foreach (j; 0..K+1) {
auto k = S - i - j;
if (0 <= k && k <= K) ++r;
}
}
writeln(r);
}
|
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() {
long n;
scan(n);
long ans = 100000;
while (n > 0) {
ans = min(ans, abs(753 - n % 1000));
n /= 10;
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int N;
scan(N);
int ans;
foreach (i ; 0 .. N) {
int li, ri;
scan(li, ri);
ans += ri - li + 1;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
long[string] b, r;
foreach (i; 0..N)
{
++b[RD!string];
}
auto M = RD;
foreach (i; 0..M)
{
++r[RD!string];
}
long ans;
foreach (key; b.keys)
{
ans = max(ans, max(0, b[key] - r.get(key, 0)));
}
writeln(ans);
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;
import std.container;
enum MAX = 1_000_100;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias Pass = Tuple!(long, "x", long, "y");
void main()
{
auto n = lread();
auto a = aryread();
long flag, iter, ans;
while (flag < n)
{
flag = 0;
long cntzero, cntone;
foreach (e; a)
{
e >>= iter;
auto tmp = e & 1;
if (e < 1)
flag++;
if (tmp)
cntone++;
else
cntzero++;
}
ans += powmod(2, iter, MOD) * ((cntone * cntzero) % MOD) % MOD;
ans %= MOD;
iter++;
}
ans.writeln();
}
auto powmod(T)(T a, T b, long mod = MOD)
{
auto ret = 1;
while (b--)
{
ret *= a;
ret %= mod;
}
return ret % mod;
}
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.uni; // unicode
void main()
{
auto s = readln.split;
writeln(s.map!"a[0]".array.toUpper);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
bool check(const char[] arr) {
auto p = arr[0];
foreach (e; arr[1..$]) {
if (p != e) return false;
}
return true;
}
void main()
{
auto s = readln.chomp;
if (check(s)) {
writeln(0);
return;
}
auto cs = s.uniq();
auto min = int.max;
foreach (c; cs) {
int cnt;
auto ss = s.dup;
while (!check(ss)) {
++cnt;
foreach (i, sc; ss) {
if (i == ss.length - 1) {
ss.length -= 1;
} else {
ss[i] = sc == c ? sc : ss[i+1];
}
}
}
if (cnt < min) min = cnt;
}
writeln(min);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp.split.to!(int[]);
auto ab = s[0] * s[1];
auto f = false;
foreach (i; 1..4) {
if (ab * i % 2) {
f = true;
}
}
if (f) {
writeln("Yes");
} else {
writeln("No");
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string s = readln.chomp;
int w = readln.chomp.to!(int);
string ans = "";
for (int i = 0; i < s.length; i += w) {
ans ~= s[i];
}
writeln(ans);
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// ModInt {{{
struct ModInt(ulong modulus) {
import std.traits : isIntegral, isBoolean;
import std.exception : enforce;
private {
ulong val;
}
this(const ulong n) {
val = n % modulus;
}
this(const ModInt n) {
val = n.value;
}
@property {
ref inout(ulong) value() inout {
return val;
}
}
T opCast(T)() {
static if (isIntegral!T) {
return cast(T)(val);
} else if (isBoolean!T) {
return val != 0;
} else {
enforce(false, "cannot cast from " ~ this.stringof ~ " to " ~ T.stringof ~ ".");
}
}
ModInt opAssign(const ulong n) {
val = n % modulus;
return this;
}
ModInt opOpAssign(string op)(ModInt rhs) {
static if (op == "+") {
val += rhs.value;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs.value) {
val += modulus;
}
val -= rhs.value;
} else if (op == "*") {
val = val * rhs.value % modulus;
} else if (op == "/") {
this *= rhs.inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs.value > 0) {
if (rhs.value % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opOpAssign(string op)(ulong rhs) {
static if (op == "+") {
val += rhs;
if (val >= modulus) {
val -= modulus;
}
} else if (op == "-") {
if (val < rhs) {
val += modulus;
}
val -= rhs;
} else if (op == "*") {
val = val * rhs % modulus;
} else if (op == "/") {
this *= ModInt(rhs).inv;
} else if (op == "^^") {
ModInt res = 1;
ModInt t = this;
while (rhs > 0) {
if (rhs % 2 != 0) {
res *= t;
}
t *= t;
rhs /= 2;
}
this = res;
} else {
enforce(false, op ~ "= is not implemented.");
}
return this;
}
ModInt opUnary(string op)() {
static if (op == "++") {
this += 1;
} else if (op == "--") {
this -= 1;
} else {
enforce(false, op ~ " is not implemented.");
}
return this;
}
ModInt opBinary(string op)(const ulong rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(ref const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinary(string op)(const ModInt rhs) const {
mixin("return ModInt(this) " ~ op ~ "= rhs;");
}
ModInt opBinaryRight(string op)(const ulong lhs) const {
mixin("return ModInt(this) " ~ op ~ "= lhs;");
}
long opCmp(ref const ModInt rhs) const {
return cast(long)value - cast(long)rhs.value;
}
bool opEquals(const ulong rhs) const {
return value == ModInt(rhs).value;
}
ModInt inv() const {
ModInt ret = this;
ret ^^= modulus - 2;
return ret;
}
string toString() const {
import std.format : format;
return format("%s", val);
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n, m;
cin.scan(n, m);
int[] a = cin.nextArray!int(m);
auto s = a.sum;
if (n < s) {
writeln(-1);
} else {
writeln(n - s);
}
}
|
D
|
import std.conv;
import std.stdio;
import std.array;
import std.range;
import std.string;
import std.algorithm;
void main()
{
int sum = 0;
foreach(_; 0..10) {
sum += readln().chomp().to!int;
}
writeln(sum);
}
|
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();
auto B = aryread();
auto C = aryread();
auto ans = B.sum();
foreach (i; 0 .. N - 1)
{
ans += (A[i + 1] - A[i] == 1) ? C[A[i] - 1] : 0;
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }
void main()
{
auto X = RD;
long a = 100;
long ans;
foreach (i; 1..10^^7)
{
a += a / 100;
if (a >= X)
{
ans = i;
break;
}
}
writeln(ans);
stdout.flush;
debug readln;
}
|
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 n = read.to!int;
long[] as, bs;
foreach(i; 0 .. n){
as ~= read.to!long;
bs ~= read.to!long;
}
long sum;
foreach(a; as) sum += a;
long best = sum;
foreach(i; 0 .. n){
long a = as[i], b = bs[i];
if(a <= b) continue;
if(b < best) best = b;
}
(sum - best).writeln;
}
/*
A君はできるだけ粘りたい。
B君よりも高い山を1つ残しておけばいくらでも粘れるので、
最終的には1つの山だけが残る形にするまで粘ることができる。
その際A君にとっては、残る山の高さは低いほうがよい。
つまり
「A君の方が高い山のうちでB君の高さが最も低いもの」
が残る山の高さ。
※これが無いときは初期状態で全ての山のABが一致しているときで、答えは0。
*/
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto n = readln.chomp.to!int;
writeln("ABC", n);
}
|
D
|
void main() {
problem();
}
void problem() {
auto A = scan!long;
auto B = scan!long;
auto C = scan!long;
auto K = scan!int;
bool solve() {
foreach(p; [0,1,2].permutationsWithRepetitions(K)) {
auto a = A;
auto b = B;
auto c = C;
foreach(_; 0..p.count(0)) a *= 2;
foreach(_; 0..p.count(1)) b *= 2;
foreach(_; 0..p.count(2)) c *= 2;
[a,b,c].deb;
if (a < b && b < c) return true;
}
return false;
}
writeln(solve() ? "Yes" : "No");
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
import std.concurrency;
Generator!(T[]) permutationsWithRepetitions(T)(T[] data, in uint n)
in {
assert(!data.empty && n > 0);
} body {
return new typeof(return)({
if (n == 1) {
foreach (el; data)
yield([el]);
} else {
foreach (el; data)
foreach (perm; permutationsWithRepetitions(data, n - 1))
yield(el ~ perm);
}
});
}
// -----------------------------------------------
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional;
void main() {
auto S = readln.chomp.dup;
bool po() {
if (S[0] != 'A') return false;
if (S[2..$-1].count('C') != 1) return false;
foreach (i; 2..S.length-1) {
if (S[i] == 'C') S[i] = 'c';
}
return S[1..$].toLower == S[1..$];
}
writeln(po() ? "AC" : "WA");
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto Q = readln.chomp.to!int;
auto ks = readln.split.to!(int[]);
foreach (k; ks) {
long res;
auto ms = new long[](N);
long d, m, dm;
foreach (i, c; S) {
if (i >= k) {
if (S[i-k] == 'D') {
--d;
dm -= ms[i-1] - ms[i-k];
}
}
switch (c) {
case 'D':
++d;
break;
case 'M':
++m;
dm += d;
break;
case 'C':
res += dm;
break;
default:
}
ms[i] = m;
}
writeln(res);
}
}
|
D
|
import std.stdio, std.array, std.conv, std.typecons, std.algorithm, std.numeric, std.bigint;
T diff(T)(const T a, const T b) { return a > b ? a - b : b - a; }
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
T[] readToArray(T)() {
return readln.split.to!(T[]);
}
void main() {
const ip = readToArray!(ulong);
const auto a = ip[0], b = ip[1], c = ip[2], d = ip[3];
const auto all_cnt = b - a + 1;
if (c == 1 || d == 1) {
writeln(0);
} else { // c != 1 && d != 1
ulong lcm = c*d/gcd(c, d);
//writefln("!!%d %d %d\n", c, d, lcm);
ulong c_div = b/c - (a-1)/c;
ulong d_div = b/d - (a-1)/d;
ulong lcm_div = b/lcm - (a-1)/lcm;
//writefln("!!%d %d %d\n", c_div, d_div, lcm_div);
writeln(all_cnt-(c_div+d_div-lcm_div));
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y", long, "cost");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
long ans;
auto infomat = new long[][](n, n);
foreach (ref e; infomat)
e[] = -1;
foreach (i; iota(n))
{
auto a = lread();
foreach (_; iota(a))
{
long x, y;
scan(x, y);
infomat[i][x - 1] = y;
}
}
// 右から1スタートで扱う
foreach (mask; iota(1 << n))
{
auto state = new long[](n);
bool contradict;
// writefln("%02b", mask);
foreach (shift; iota(n))
state[shift] = (mask & (1 << shift)) ? 1 : 0;
foreach (i; iota(n))
{
if (state[i])
{
foreach (j; iota(n))
{
if (infomat[i][j] == -1)
continue;
contradict |= (state[j] != infomat[i][j]);
}
}
}
if (!contradict)
ans = max(mask.popcnt(), ans);
}
ans.writeln();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
import std.stdio, std.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;
int[] ls, rs;
ls.length = N;
ls[] = -1;
rs.length = N;
rs[] = -1;
int i, c;
foreach (_; 0..K) {
while (S[i] == 'x') ++i;
ls[i] = c++;
i += C+1;
}
i = N-1;
c = 0;
foreach (_; 0..K) {
while (S[i] == 'x') --i;
rs[i] = c++;
i -= C+1;
}
foreach (j; 0..N) {
if (ls[j] == K-rs[j]-1) writeln(j+1);
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n; readV(n);
int[] s; readC(n, s);
auto t = s.sum;
if (t%10 != 0) {
writeln(t);
} else {
auto f = s.filter!"a%10 != 0";
if (f.empty) {
writeln(0);
} else {
writeln(t-f.reduce!min);
}
}
}
|
D
|
void main(){
char alph = _scan!char();
( ('a'<=alph && alph<='z') ? 'a' : 'A').writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(char[]);
int X = 700;
if (S[0] == 'o') X += 100;
if (S[1] == 'o') X += 100;
if (S[2] == 'o') X += 100;
writeln(X);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
long disit_sum(long a)
{
long sum,disit_num;
foreach(disit;0 .. 9)
{
disit_num = a / pow(10, disit);
sum += disit_num % 10;
}
return sum;
}
void main()
{
long a,b,t;
scan(a,b,t);
long sum,i = 1,now = a;
while(now <= t)
{
sum += b;
i++;
now = i * a;
}
writeln(sum);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto T = readln.chomp.to!int;
auto A = readln.split.map!(to!long).array;
foreach (a; A) {
auto m = a % 14;
if (a <= 14) {
writeln("NO");
} else if (m != 0 && m <= 6) {
writeln("YES");
} else {
writeln("NO");
}
}
}
|
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;
int p;
bool ok(int s) {
int i = s / 50 % 475;
foreach (_; 0..25) {
i = (i * 96 + 42) % 475;
if (26 + i == p) return true;
}
return false;
}
void main() {
auto s = readln.split.map!(to!int);
p = s[0];
int x = s[1];
int y = s[2];
int ox = x;
while (x - 50 >= y) x -= 50;
while (!ok(x)) x += 50;
writeln(max(0, (x - ox + 50) / 100));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.string;
enum arrow { L,R,U,D };
void main(){
int n = readln.chomp.to!int();
const arrow[dchar] toArrow = [ 'L':arrow.L, 'R':arrow.R, 'U':arrow.U, 'D':arrow.D ];
foreach(i; 0..n){
auto score = readln.chomp.map!(a=>toArrow[a])();
writeln(score.isNatural? "Yes": "No");
}
}
bool isNatural(T)(T score){
arrow prev = cast(arrow)-1;
bool startLeft = true;
bool startRight = true;
bool startFoot = true;
foreach(s; score){
if(prev == s) return false;
if(s == arrow.L){
startLeft = startLeft && startFoot;
startRight = startRight && !startFoot;
}else if(s == arrow.R){
startLeft = startLeft && !startFoot;
startRight = startRight && startFoot;
}
if(!(startLeft || startRight)) return false;
prev = s;
startFoot ^= true;
}
return true;
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;
void main() {
auto N = readln.chomp.to!int;
auto G = new int[][](N);
foreach (_; 0..N-1) {
auto s = readln.split.map!(to!int);
G[s[0]-1] ~= s[1]-1;
G[s[1]-1] ~= s[0]-1;
}
if (N % 2 == 1) {
writeln(-1);
return;
}
auto d = new int[](N);
int dfs(int n, int p) {
d[n] = 1;
foreach (m; G[n]) if (m != p) d[n] += dfs(m, n);
return d[n];
}
dfs(0, -1);
auto ans = d.map!(a => 1 - a % 2).sum;
writeln(ans - 1);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
string s; readV(s);
writeln((s[0] == s[$-1]) ^ (s.length%2 != 0) ? "First" : "Second");
}
|
D
|
import std.stdio, std.conv, std.math, std.string, std.range, std.array,
std.algorithm;
void main(){
auto buf = readln().strip().split().map!(to!int)();
immutable N = buf[0];
auto a = readln().strip().split().map!(to!int)();
long[long] m;
long ans;
long shift;
foreach(ea; a) {
shift += ea;
m[ea-shift] += 1;
ans += m.get(-shift, 0);
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
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;
writeln(180 * (N - 2));
stdout.flush();
debug readln();
}
|
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;
bool[int] d;
for (int i = 0; i < n; i++) {
int x = readint;
d[x] = true;
}
writeln(d.keys.length);
}
|
D
|
void main() {
auto W = rs;
ulong cnt;
while(true) {
auto s = rs;
if(s == "END_OF_TEXT") break;
foreach(v; s.split) {
if(v.toLower == W.toLower) cnt++;
}
}
cnt.writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int a, b; readV(a, b);
writeln(a - (a > b));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abck = readln.split.to!(long[]);
auto A = abck[0];
auto B = abck[1];
auto C = abck[2];
auto K = abck[3];
long r;
if (K > 0) {
r += min(K, A);
K -= min(K, A);
}
if (K > 0) {
K -= min(K, B);
}
if (K > 0) {
r -= min(K, C);
}
writeln(r);
}
|
D
|
void main()
{
long n = rdElem;
long[] a = n.rdCol;
writeln(a.any!"a & 1" ? "first" : "second");
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
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; }
void main(){
auto s = readln.chomp;
auto x = "keyence";
auto ans = "NO";
for(int i = 0; i <= s.length; i ++){
for(int j = i; j <= s.length; j ++){
if(s[0 .. i] ~ s[j .. $] == x) ans = "YES";
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp.to!long;
auto A = readln.chomp.to!long;
auto B = readln.chomp.to!long;
auto C = readln.chomp.to!long;
auto D = readln.chomp.to!long;
auto E = readln.chomp.to!long;
auto x = min(A, B, C, D, E);
writeln(4 + (N+x-1)/x);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), n = rd[0], c = rd[1];
struct Program { int s, t, c; }
auto p = new Program[](n);
bool[int][int] h;
foreach (i; 0..n) {
auto rd2 = readln.splitter;
auto si = rd2.front.to!int; rd2.popFront();
auto ti = rd2.front.to!int; rd2.popFront();
auto ci = rd2.front.to!int;
p[i] = Program(si, ti, ci);
h[ci][ti] = true;
}
auto m = 10^^5*2+1;
auto s = new int[](m);
foreach (pi; p) {
s[pi.s*2-(pi.c in h && pi.s in h[pi.c] ? 0 : 1)]++;
s[pi.t*2]--;
}
foreach (i; 1..m) s[i] += s[i-1];
writeln(s.reduce!max);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
enum P = 998244353L;
void main()
{
auto abcd = readln.split.to!(long[]);
auto A = abcd[0];
auto B = abcd[1];
auto C = abcd[2];
auto D = abcd[3];
auto DP = new long[][][](C+1, D+1, 2);
DP[A][B][0] = 1;
foreach (i; A..C+1) {
foreach (j; B..D+1) {
if (i == A && j == B) continue;
DP[i][j][0] = (DP[i][j-1][0] * i % P + DP[i][j-1][1]) % P;
DP[i][j][1] = (DP[i-1][j][0] + DP[i-1][j][1]) * j % P;
}
}
writeln((DP[C][D][0] + DP[C][D][1]) % P);
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/dp/tasks/dp_a
import std.algorithm;
import std.math;
import std.array;
import std.conv;
import std.stdio;
import std.string;
void main() {
const int n = readln.chomp.to!int;
const int[] h = readln.split.map!(to!int).array;
int[] dp = new int[n];
dp[1] = abs(h[1]-h[0]);
for(int i = 2; i < n; ++i)
dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2]));
dp[$-1].writeln;
}
|
D
|
import std.algorithm;
import std.concurrency;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.meta;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;
void main() {
auto input = readln.chomp.split.map!(to!long);
auto r = input.front;
input.popFront;
auto D = input.front;
input.popFront;
auto x = input.front;
input.popFront;
iota(10).each!(_ => writeln(x = r * x - D));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main(){
int x = readln().chomp().to!int();
writeln(x * x * x);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n, k;
scan(n, k);
auto w = new int[][][](2, k, k);
foreach (i ; 0 .. n) {
int xi, yi;
char ci;
scan(xi, yi, ci);
if (ci == 'B') yi += k;
int e = (xi / k % 2 + yi / k % 2) % 2;
w[e][xi % k][yi % k]++;
}
foreach (e ; 0 .. 2) {
foreach (i ; 0 .. k) {
foreach (j ; 1 .. k) {
w[e][i][j] += w[e][i][j-1];
}
}
foreach (j ; 0 .. k) {
foreach (i ; 1 .. k) {
w[e][i][j] += w[e][i-1][j];
}
}
}
int ans;
foreach (i ; 0 .. k) {
foreach (j ; 0 .. k) {
int t = w[0][k-1][k-1] - w[0][k-1][j] - w[0][i][k-1] + 2*w[0][i][j];
int s = w[1][i][k-1] + w[1][k-1][j] - 2*w[1][i][j];
ans = max(ans, t + s);
ans = max(ans, n - t - s);
}
}
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.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd = readln.split.to!(int[]), x = rd[0], t = rd[1];
writeln(max(x-t, 0));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(int a, int b) {
if (a <= 5) return 0;
if (6 <= a && a <= 12) return b / 2;
return b;
}
void main() {
int a, b; scan(a, b);
writeln(calc(a, b));
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;
void main() {
auto N = readln.chomp.to!int;
auto edges = new int[][](N);
foreach (i; 0..N-1) {
auto s = readln.split.map!(to!int);
edges[s[0] - 1] ~= s[1] - 1;
edges[s[1] - 1] ~= s[0] - 1;
}
int[] path;
bool dfs(int n, int p) {
if (n == N - 1) {
path ~= N - 1;
return true;
}
foreach (m; edges[n]) if (m != p && dfs(m, n)) {
path ~= n;
return true;
}
return false;
}
dfs(0, -1);
path.reverse();
auto colors = new int[](N);
DList!(int) A;
DList!(int) B;
int M = path.length.to!int;
foreach (i; 0..M/2+M%2) colors[path[i]] = 1, A.insertBack(path[i]);
foreach (i; M/2+M%2..M) colors[path[i]] = 2, B.insertBack(path[i]);
void bfs(DList!int q, int c) {
while (!q.empty) {
int n = q.front;
q.removeFront;
colors[n] = c;
foreach (m; edges[n]) if (colors[m] == 0) q.insertBack(m);
}
}
bfs(A, 1);
bfs(B, 2);
int a = 0, b = 0;
foreach (c; colors) if (c == 1) a += 1; else b += 1;
writeln( a > b ? "Fennec" : "Snuke" );
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
void main(){
int n = readln().chomp().to!int;
int[101][101] mat;
int[][] num;
num ~= new int[n+1];
for(int i=0;i<n;i++){
num ~= (0 ~ split(readln()).to!(int[]));
}
for(int i=0;i<num.length;i++){
for(int j=0;j<num[i].length;j++){
for(int k=0;k<=i;k++){
for(int l=0;l<=j;l++){
mat[i][j] += num[k][l];
}
}
}
}
long ans = -10000000001;
for(int i=1;i<num.length;i++){
for(int j=1;j<num[i].length;j++){
for(int k=0;k<i;k++){
for(int l=0;l<j;l++){
ans = max(ans,mat[i][j] - mat[i][l] - mat[k][j] + mat[k][l]);
}
}
}
}
writeln(ans);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
auto a = aryread!string();
// writeln(a);
writeln(a[0][0], a[1][0], a[2][0]);
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int n, a, b; readV(n, a, b);
writeln(min(n*a, b));
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto D = RD;
auto S = RD;
auto T = RD;
writeln(T*S >= D ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import core.stdc.stdio;
import std.algorithm;
void main(){
int[] map = new int[100000];
int[] t = new int[10000];
while(1){
int r,c;
scanf("%d%d",&r,&c);
if(r==0&&c==0)
break;
t[] = 0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&map[i*c+j]);
t[j] += 1-map[i*c+j];
}
}
int ans=0;
for(int i=0;i<(1<<r);i++){
int a=0;
for(int j=0;j<c;j++){
int s = t[j];
for(int k=0;k<r;k++){
if(i&(1<<k)){
if(map[k*c+j]==0)
s--;
else
s++;
}
}
a += max(s,r-s);
}
ans = max(a,ans);
}
printf("%d\n",ans);
}
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = (tmp[0] + 13) % 15, b = (tmp[1] + 13) % 15;
if (a > b) {
"Alice".writeln;
} else if (a < b) {
"Bob".writeln;
} else {
"Draw".writeln;
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, m; readV(n, m);
int[] a; readA(n, a);
auto p = new int[][](m+1);
foreach (int i, ai; a) p[ai] ~= i;
auto k = 0, c = 0L;
foreach (i; 0..n-1)
if (a[i] > a[i+1]) {
++k;
c += a[i+1];
} else {
c += a[i+1]-a[i];
}
auto ans = c;
foreach (i; 1..m) {
c -= k;
foreach (j; p[i]) {
if (j > 0) {
--k;
c += a[j-1] > a[j] ? a[j]+m-a[j-1] : a[j]-a[j-1];
}
if (j < n-1)
++k;
}
ans = min(ans, c);
}
writeln(ans);
}
|
D
|
void main(){
int a, b;
scanf("%d %d", &a, &b);
int ans = (a + b)%24;
ans.writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
void main()
{
long n = rdElem;
Edge[][] edge = new Edge[][](n);
foreach (i; 0 .. n-1)
{
long a, b;
rdVals(a, b);
--a, --b;
edge[a] ~= Edge(b, i), edge[b] ~= Edge(a, i);
}
long[] list = new long[n-1];
list[] = -1;
void dfs(long x, long from, long color)
{
long c = 1;
foreach (e; edge[x])
{
if (e.to == from) continue;
if (c == color) ++c;
list[e.idx] = c;
dfs(e.to, x, c);
++c;
}
}
dfs(0, -1, -1);
list.reduce!max.writeln;
foreach (l; list)
{
l.writeln;
}
}
struct Edge
{
long to, idx;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
void main(){
long n, l;
{
long[] tmp = readln.chomp.split.map!(to!long).array;
n = tmp[0], l = tmp[1];
}
long[] as = readln.chomp.split.map!(to!long).array;
string ans;
int[] ans2;
long center = -1;
for(int i = 0; i < as.length - 1; i ++){
if(as[i] + as[i + 1] >= l){
center = i;
break;
}
}
if(center == -1) ans = "Impossible";
else{
ans = "Possible";
for(int i = 0; i < center; i ++){
ans2 ~= i + 1;
}
for(int i = cast(int) (as.length - 2); i >= center; i --){
ans2 ~= i + 1;
}
}
ans.writeln;
foreach(i; ans2) i.writeln;
}
|
D
|
import std.stdio;
import std.array;
import std.conv;
import std.algorithm;
import std.string;
void main(){
int totalTime = 0;
for(int i = 0; i < 4; i++){
totalTime += readln().chomp().to!int();
}
writeln(totalTime / 60);
writeln(totalTime % 60);
}
|
D
|
import std.functional,
std.algorithm,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv;
class UnionFind {
long[] data;
this (size_t size) {
data.length = size;
foreach (ref e; data) {
e = -1;
}
}
void unite(long x, long y) {
x = root(x);
y = root(y);
if (x != y) {
if (size(x) < size(y)) { swap(x, y); }
data[x] += data[y];
data[y] = x;
}
}
bool same(long x, long y) {
return root(x) == root(y);
}
long root(long x) {
return data[x] < 0 ? x : (data[x] = root(data[x]));
}
size_t size(long x) {
return -data[root(x)];
}
}
void main() {
int[] NM = readln.chomp.split.to!(int[]);
int N = NM[0],
M = NM[1];
int ans;
int[][] es;
foreach (_; 0..M) {
int[] ab = readln.chomp.split.to!(int[]);
int a = ab[0],
b = ab[1];
a--;
b--;
es ~= [a, b];
}
foreach (i, e; es) {
UnionFind uf = new UnionFind(N);
foreach (j, e2; es) {
if (i == j) { continue; }
uf.unite(e2[0], e2[1]);
}
if (uf.size(0) != N) {
ans++;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA;
while (true)
{
bool ok = true;
foreach (i; 0..n)
{
if (a[i] != i+1)
{
ok = false;
break;
}
}
if (ok) break;
foreach (i; 0..n-1)
{
if (i % 2 != ans[ti] % 2) continue;
if (a[i] > a[i+1])
swap(a[i], a[i+1]);
}
++ans[ti];
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
writeln(s[1] == s[2] && (s[0] == s[1] || s[2] == s[3]) ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.format;
import std.algorithm;
//string lines = q"[
//abcdeffg]";
void main()
{
string lines;
string buf;
while (!stdin.eof) {
buf = stdin.readln();
lines ~= buf;
}
string[] array = splitLines(lines);
int N = array[0].split(" ")[0].to!int;
int W = array[0].split(" ")[1].to!int;
long[] Wi = new long[](N+1);
long[] Vi = new long[](N+1);
Wi[0] = 0L;
Vi[0] = 0L;
for (int i=1; i<array.length; i++) {
string s = array[i];
Wi[i] = s.split(" ")[0].to!int;
Vi[i] = s.split(" ")[1].to!int;
}
long[][] dp = new long[][](N+1,W+1);
for (int i = 1; i < N+1; i++) {
for (int j = 1; j < W+1; j++) {
if (j < Wi[i]) {
dp[i][j] = dp[i-1][j];
} else {
dp[i][j] = max(dp[i-1][j], dp[i-1][j-Wi[i]] + Vi[i]);
}
}
}
writeln(dp.reduce!(max).reduce!(max));
}
|
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 y, b, r;
scan(y, b, r);
int ans = 0;
foreach (i ; 1 .. y + 1) {
foreach (j ; 1 .. b + 1) {
foreach (k ; 1 .. r + 1) {
if (j == i + 1 && k == j + 1) {
ans = max(ans, i + j + k);
}
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
auto tmp = readln.split.to!(int[]);
writeln(tmp[0] * tmp[1] / 2);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
enum P = 10L^^9+7;
void main()
{
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(long[]);
long x, r;
foreach (a; AS) {
(r += x * a % P) %= P;
(x += a) %= P;
}
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii;
import std.typecons, std.functional, std.traits;
import std.algorithm, std.container;
import core.stdc.stdlib;
void main()
{
auto N = scanElem;
auto X = scanElem;
auto a = X;
auto b = N-X;
long res = a+b;
if(a>b)swap(a,b);
while(b%a!=0){
res += ((b/a)*2)*a;
auto tmp=min(a,b);
a=b%a;
b=tmp;
}
res += ((b/a)*2-1)*a;
writeln(res);
}
long gcd(long a, long b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
class UnionFind{
UnionFind parent = null;
void merge(UnionFind a)
{
if(same(a)) return;
a.root.parent = this.root;
}
UnionFind root()
{
if(parent is null)return this;
return parent = parent.root;
}
bool same(UnionFind a)
{
return this.root == a.root;
}
}
void scanValues(TList...)(ref TList list)
{
auto lit = readln.splitter;
foreach (ref e; list)
{
e = lit.fornt.to!(typeof(e));
lit.popFront;
}
}
T[] scanArray(T = long)()
{
return readln.split.to!(T[]);
}
void scanStructs(T)(ref T[] t, size_t n)
{
t.length = n;
foreach (ref e; t)
{
auto line = readln.split;
foreach (i, ref v; e.tupleof)
{
v = line[i].to!(typeof(v));
}
}
}
long scanULong(){
long x;
while(true){
const c = getchar;
if(c<'0'||c>'9'){
break;
}
x = x*10+c-'0';
}
return x;
}
T scanElem(T = long)()
{
char[] res;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
res ~= cast(char) c;
c = getchar;
}
return res.strip.to!T;
}
template fold(fun...) if (fun.length >= 1)
{
auto fold(R, S...)(R r, S seed)
{
static if (S.length < 2)
{
return reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
struct Factor
{
long n;
long c;
}
Factor[] factors(long n)
{
Factor[] res;
for (long i = 2; i ^^ 2 <= n; i++)
{
if (n % i != 0)
continue;
int c;
while (n % i == 0)
{
n = n / i;
c++;
}
res ~= Factor(i, c);
}
if (n != 1)
res ~= Factor(n, 1);
return res;
}
long[] 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;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
writeln(s[0..4], " ", s[4..$]);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
const long MOD = 998244353;
const int INF = 1 << 29;
void main() {
auto N = readln.chomp.to!int;
auto S = N.iota.map!(_ => readln.chomp).array;
auto dp = new int[](26);
fill(dp, INF);
dp[S.front.front - 'a'] = 1;
foreach (i; 1..N) {
dp[S[i].front - 'a'] = min(dp[S[i].front - 'a'],
dp[S[i-1].front - 'a'] + 1);
}
dp[S.back.front - 'a'].writeln;
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto N = RD;
auto p = RDA;
long cnt;
foreach (i; 0..N)
{
if (p[i] != i+1)
++cnt;
}
writeln(cnt <= 2 ? "YES" : "NO");
stdout.flush();
debug readln();
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}
void main()
{
int n, k; readV(n, k);
auto c = 0L;
foreach (b; k+1..n+1) {
c += n/b * (b-k);
if (k > 0)
c += max(0, n%b-k+1);
else
c += max(0, n%b-k);
}
writeln(c);
}
|
D
|
void main()
{
long n = rdElem;
Edge[][] tree = new Edge[][](n);
foreach (i; 0 .. n-1)
{
long a, b, c;
rdVals(a, b, c);
--a, --b;
tree[a] ~= Edge(b, c), tree[b] ~= Edge(a, c);
}
long q, k;
rdVals(q, k);
--k;
long[] dist = new long[n];
void DFS(long u, long v, long w)
{
dist[u] = w;
foreach (t; tree[u])
{
if (t.to == v) continue;
DFS(t.to, u, w+t.cost);
}
}
DFS(k, -1, 0);
foreach (i; 0 .. q)
{
long x, y;
rdVals(x, y);
--x, --y;
writeln(dist[x] + dist[y]);
}
}
struct Edge
{
long to, cost;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm, std.typecons, std.range, std.container, std.math, std.algorithm.searching, std.functional;
void main(){
(readln().chomp.isHaiku?"YES":"NO").writeln();
}
auto isHaiku(string str){
return ["5 7 5", "7 5 5", "5 5 7"].canFind(str);
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const N = readln.chomp.to!long;
const S = readln.chomp;
const K = readln.chomp.to!long;
S.map!(c => c != S[K-1] ? "*" : [c]).join.writeln;
}
|
D
|
void main(){
int n = _scan();
string s = readln().chomp();
writeln( s[0..n/2] == s[n/2..$]? "Yes": "No" );
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main(){
// input
int[] input;
for(auto i = 0; i < 3; i++){
input ~= to!int(readln().chomp());
}
immutable int start_money = input[0];
immutable int a_price = input[1];
immutable int b_price = input[2];
immutable int answer = (start_money - a_price) % b_price;
writeln(answer);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, x; readV(n, x);
int[] m; readC(n, m);
x -= m.sum;
writeln(n + x/m.reduce!min);
}
|
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 Pair = Tuple!(long, "begin", long, "end");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
auto n = lread();
auto a = aryread();
long cost_odd, cost_even; // head one grater than 0
long c_sum_odd, c_sum_even;
foreach (i, e; a)
{
if (!(i % 2))
{
if (c_sum_odd + e > 0)
c_sum_odd += e;
else
{
cost_odd += 1 - (c_sum_odd + e);
c_sum_odd = 1;
// writefln("cost_odd = %s, i = %s", cost_odd, i);
}
}
else
{
if(c_sum_odd + e < 0)
c_sum_odd += e;
else
{
cost_odd += 1 + c_sum_odd + e;
c_sum_odd = -1;
// writefln("cost_odd = %s, i = %s", cost_odd, i);
}
}
}
foreach (i, e; a)
{
if (!(i % 2))
{
if (c_sum_even + e < 0)
c_sum_even += e;
else
{
cost_even += 1 + c_sum_even + e;
c_sum_even = -1;
// writefln("cost_even = %s, i = %s", cost_even, i);
}
}
else
{
if(c_sum_even + e > 0)
c_sum_even += e;
else
{
cost_even += 1 - (c_sum_even + e);
c_sum_even = 1;
// writefln("cost_even = %s, i = %s", cost_even, i);
}
}
}
min(cost_even, cost_odd).writeln();
}
|
D
|
import std.stdio: readln, writeln;
import std.string: chomp, split;
import std.conv: to;
void main()
{
auto dataSet = chomp(readln()).split(" ");
int m = to!int(dataSet[0]),
f = to!int(dataSet[1]),
r = to!int(dataSet[2]);
while(1 + m || 1 + f || 1 + r)
{
if(0 == (1 + m) * (1 + f) || 30 > m + f) writeln("F");
else if(80 <= m + f) writeln("A");
else if(65 <= m + f) writeln("B");
else if(50 <= m + f) writeln("C");
else if(50 <= r) writeln("C");
else writeln("D");
dataSet = chomp(readln()).split(" ");
m = to!int(dataSet[0]),
f = to!int(dataSet[1]),
r = to!int(dataSet[2]);
}
}
|
D
|
void main()
{
long n, m;
ipElems(n, m);
long[] burger = new long[n+1], patty = new long[n+1];
burger[0] = patty[0] = 1;
foreach (i; 0 .. n)
{
burger[i+1] = 2 * burger[i] + 3;
patty[i+1] = 2 * patty[i] + 1;
}
long countPatty(long l, long y)
{
if (l == 0) return 1;
if (y <= 1) return 0;
if (y == burger[l-1] + 2) return patty[l-1] + 1;
if (y < burger[l-1] + 2) return countPatty(l-1, y-1);
return patty[l-1] + 1 + countPatty(l-1, y-2-burger[l-1]);
}
countPatty(n, m).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 ipElems(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.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;
auto readInts() {
return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
return readInts()[0];
}
auto readLongs() {
return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
return readLongs()[0];
}
const real eps = 1e-10;
int ans(int n, int k, int m) {
if(n == 2) {
return m == 2 ? 1: 2;
}
if(n == m) {
return ans(n-1, k, (k-1)%(n-1) + 1);
} else {
return ((ans(n, k, n) + m-1) % n) + 1;
}
}
void main(){
while(true) {
auto nkm = readInts();
auto n = nkm[0];
auto k = nkm[1];
auto m = nkm[2];
if((n|k|m) == 0) return;
writeln(ans(n, k, m));
}
}
|
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;
}
// ------ ここまでテンプレ ------ //
long a = read.to!long;
long b = read.to!long;
long k = read.to!long;
long ans;
long cnt;
foreach_reverse(i; 1 .. 200){
if(a % i == 0 && b % i == 0){
cnt += 1;
if(cnt == k) ans = i;
}
}
ans.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
void main() {
int[] tmp = readln.split.to!(int[]);
int n = tmp[0], k = tmp[1];
long mod = 1_000_000_007;
long[] fac = new long[n+1];
long[] finv = new long[n+1];
long[] inv = new long[n+1];
fac[0] = 1, fac[1] = 1;
finv[0] = 1, finv[1] = 1;
inv[1] = 1;
foreach (i; 2 .. n+1) {
fac[i] = fac[i-1] * i % mod;
inv[i] = mod - inv[mod%i] * (mod / i) % mod;
finv[i] = finv[i-1] * inv[i] % mod;
}
long comb(int x, int y) {
if (x < y) return 0;
if (x < 0 || y < 0) return 0;
return fac[x] * (finv[y] * finv[x-y] % mod) % mod;
}
foreach (i; 0 .. k) {
writeln((comb(n-k+1, i+1) * comb(k-1, i)) % mod);
}
}
|
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 data = readln.split;
auto A = data[0].to!long, B = data[1].to!long;
long f(long n) { switch(n%4) {
case 0: return n;
case 1: return 1;
case 2: return 1+n;
case 3: return 0;
default: return 0;
} }
if (A == 1) writeln(f(B));
else { writeln(f(A-1) ^ f(B)); }
}
|
D
|
import std.stdio;
int main(string[] argv)
{
for(int i = 1;i <= 9;i++){
for(int j = 1;j <= 9;j++){
writeln(i,"x",j,"=",i*j);
}
}
return 0;
}
|
D
|
import std.stdio;
void main(string[] args)
{
foreach(int i ; 1..10)
{
foreach(int j; 1..10)
{
writeln(i,"x",j,"=",i*j);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import std.regex;
void main(){
auto z=readln.split.to!(int[]),a=z[0],b=z[1];
writeln(a*b-(a+b-1));
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int x;
scan(x);
auto ans = (x / 500) * 1000 + (x % 500) / 5 * 5;
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto A = RD;
auto op = RD!string;
auto B = RD;
long ans;
if (op == "+")
ans = A + B;
else
ans = A - B;
writeln(ans);
stdout.flush();
debug readln();
}
|
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() {
readint;
string s = read!string;
int ans = 0;
int x = 0;
foreach (c; s) {
switch (c) {
case 'I': x++; break;
case 'D': x--; break;
default: break;
}
ans = max(ans, x);
}
writeln(ans);
}
|
D
|
import std.functional,
std.algorithm,
std.container,
std.typetuple,
std.typecons,
std.bigint,
std.string,
std.traits,
std.array,
std.range,
std.stdio,
std.conv,
std.format,
std.math;
void main()
{
auto n = readln.chomp.to!int;
int i = 1;
while(true){
if(n < i^^2) break;
else i++;
}
writeln((i-1)^^2);
}
|
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;
ans[ti] = long.max;
foreach (i; 0..s.length)
{
long cnt1;
foreach (j; 0..i)
{
if (s[j] == '0')
++cnt1;
}
long cnt2;
foreach (j; i..s.length)
{
if (s[j] == '0')
++cnt2;
}
debug writeln(cnt1, ":", cnt2);
ans[ti].chmin(min(cnt1, i-cnt1) + min(cnt2, s.length-i-cnt2));
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.