code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
string A = "";
string B = "";
foreach (i; 0..N) {
if (S[i] == '1') A ~= S[i];
else B ~= S[i];
}
if (B.empty) {
A.writeln;
return;
}
int x = B.length.to!int;
foreach (i; 0..B.length.to!int) {
if (B[i] == '2') {
x = i;
break;
}
}
writeln(B[0..x] ~ A ~ B[x..$]);
}
|
D
|
// Cheese-Cracker: cheese-cracker.github.io
void theCode(){
ll l = scan;
ll r = scan;
if(l == r){
writeln(0);
return;
}
ll d = r/2 + 1;
d = max(l, d);
writeln(r % d);
}
void main(){
long tests = scan; // Toggle!
while(tests--) theCode();
}
/********* That's All Folks! *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x", long, "y");
|
D
|
import std.stdio, std.algorithm, std.conv, std.string;
void main(){
while(true){
ulong[int] sales;
int[] order;
int data=readln.chomp.to!int;
if(data==0)break;
foreach(_;0..data){
auto inp=readln.split.map!(to!int);
int id=inp[0],sale=inp[1],num=inp[2];
if(id in sales){
sales[id]+=sale*num;
}else{
order~=id;
sales[id]=sale*num;
}
}
bool na=true;
foreach(id;order){
if(sales[id]>=1_000_000){
id.writeln;
na=false;
}
}
na&&"NA".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; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }
long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void main()
{
auto a = RD;
auto b = RD;
auto d = b - a;
long h;
foreach (i; 1..d+1)
{
h += i;
}
writeln(h - b);
stdout.flush();
debug readln();
}
|
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 s = readln.to!(char[]);
s[0]++;
writeln(s);
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.algorithm;
void main(){
auto ip = readln.chomp;
if(ip[0] == 'H'){
if(ip[2] == 'H') 'H'.writeln;
else if(ip[2] == 'D') 'D'.writeln;
} else if(ip[0] == 'D') {
if(ip[2] == 'H') 'D'.writeln;
else if(ip[2] == 'D') 'H'.writeln;
}
}
|
D
|
import std;
long calc(long[] a) {
long ans = 0;
long[long] d;
for (int i = 0; i < a.length; i++) {
ans += d.get(i + 1 - a[i], 0);
d[i + 1 + a[i]]++;
}
return ans;
}
void main() {
read;
auto a = reads!long;
writeln(calc(a));
}
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;
|
D
|
import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.bitmanip;
import std.regex;
enum INF = long.max/3;
enum MOD = 10L^^9+7;
T[] scanArray(T = long)()
{
static char[] scanBuf;
readln(scanBuf);
return scanBuf.split.to!(T[]);
}
dchar scanChar()
{
import core.stdc.stdlib;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
return cast(dchar)c;
}
T scanElem(T = long)()
{
import core.stdc.stdlib;
static auto scanBuf = appender!(char[])([]);
scanBuf.clear;
int c = ' ';
while (isWhite(c) && c != -1)
{
c = getchar;
}
while (!isWhite(c) && c != -1)
{
scanBuf ~= cast(char) c;
c = getchar;
}
return scanBuf.data.to!T;
}
dchar[] scanString(){
return scanElem!(dchar[]);
}
void main()
{
auto A = scanString;
auto B = scanString;
auto C = scanString;
write(A[0], B[1], C[2], '\n');
}
|
D
|
// import chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii,
std.typecons;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {}
void scan(T, S...)(ref T x, ref S args) {
x = next!(T);
scan(args);
}
}
// }}}
void main() {
auto cin = new Scanner;
int n = cin.next!int;
auto s = cin.nextArray!(string)(n);
int m = cin.next!int;
auto t = cin.nextArray!(string)(m);
auto a = s ~ t;
int ma = 0;
foreach(i; a) {
int cnt;
foreach(j; s) {
if (j == i) cnt++;
}
foreach(j; t) {
if (j == i) cnt--;
}
ma = max(ma, cnt);
}
writeln(ma);
}
|
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, N;
scan(A, B, N);
long x = min(N, B - 1);
writeln((A * x) / B - A * (x / B));
// foreach (x; 1 .. N + 1)
// {
// writefln("%s %s", x, (A * x) / B - A * (x / B));
// }
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.functional;
void main() {
char[] s = readln.chomp.to!(char[]);
int n = s.length.to!int;
bool[] app = new bool[](26);
foreach (ch ; s) {
app[ch - 'a'] = 1;
}
debug {
writeln("app:", app);
}
if (app.sum == 1) {
writeln(0);
return;
}
int ans = 200;
foreach (alp ; 0 .. 26) {
if (!app[alp]) continue;
auto sc = s.dup;
int cnt;
debug {
writeln((alp + 'a').to!char);
}
foreach (i ; 0 .. n - 1) {
foreach (j ; 0 .. n - 1 - i) {
if (sc[j + 1] == (alp + 'a')) {
sc[j] = sc[j + 1];
}
}
debug {
writeln("sc:", sc);
}
bool flag = true;
foreach (j ; 0 .. n - 1 - i - 1) {
if (sc[j] != sc[j + 1]) {
flag = false;
}
}
cnt++;
if (flag) {
break;
}
}
ans = min(ans, cnt);
}
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);
}
|
D
|
import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
struct Input
{
T next(T)()
{
import std.conv;
return to!T(nextWord);
}
string nextWord()
{
if (_nextWords.empty)
_nextWords.insertFront(readln().split);
string word = _nextWords.front; _nextWords.removeFront;
return word;
}
import std.container;
DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
import std.traits;
static foreach(i; 0 .. T.length)
static if(isArray!(T[i]) && !is(T[i] == string))
foreach(ref e; args[i])
read(e);
else static if(isTuple!(T[i]))
static foreach(n; 0 .. T[i].Types.length)
read(args[i][n]);
else
args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
alias T = Parameters!F;
foreach(i; 0 .. k)
{
T t; get(t);
F(t);
}
}
alias get = read;
void wone(T)(T t, char end)
{
static if(isArray!T && !is(T == string))
{
foreach(i; 0 .. t.length - 1)
write(t[i], ' ');
write(t[$ - 1], end);
}
else
write(t, end);
}
void wr(T...)(T t)
{
static foreach(i; 0 .. T.length)
static if(i + 1 < T.length)
wone(t[i], ' ');
else
wone(t[i], '\n');
}
void ans(T...)(T t)
{
import core.stdc.stdlib;
wr(t);
exit(0);
}
void main()
{
int n, k; get(n, k);
string str; get(str);
bool[26] ap = false;
foreach(i; 0 .. k)
{
string r; get(r);
ap[r[0] - 'a'] = true;
}
int i = 0;
long res = 0;
for(; i < str.length;)
{
long count = 0;
while(i < str.length && ap[str[i] - 'a'])
{
count++;
i++;
}
res += count * (count + 1) / 2;
if(!count)
i++;
}
ans(res);
}
|
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;
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
alias sread = () => readln.chomp();
void main()
{
auto str = sread();
long cnt;
foreach(c;str){
cnt += (c == '2')? 1:0;
}
cnt.writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int q;
scan(q);
while (q--) {
long ai, bi;
scan(ai, bi);
solve(ai, bi);
}
}
void solve(long ai, long bi) {
long l = sqrt(ai*bi);
debug {
writefln("l:%s", l);
}
long ans;
if (l*(l+1) < ai*bi) {
ans = 2*l - 1;
}
else {
ans = 2*l - 2;
}
if (ai == bi) ans++;
writeln(ans);
}
long sqrt(long x) {
long btm = 0, top = x;
while (top - btm > 1) {
long mid = (top + btm) / 2;
if (mid < (x+mid-1)/mid) {
btm = mid;
}
else {
top = mid;
}
}
return btm;
}
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
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
import std.exception, std.typecons;
int n, m; rd(n, m);
auto edges=new Tuple!(int, int)[](m);
auto g=new int[][](n, 0);
foreach(i; 0..m){
int a, b; rd(a, b);
a--; b--;
g[a]~=b; g[b]~=a;
edges[i]=tuple(a, b);
}
auto vis=new bool[](n);
long dfs(int i){
vis[i]=true;
long ret=1;
foreach(j; g[i]){
if(vis[j]==false) ret+=dfs(j);
}
return ret;
}
long[] sz;
foreach(i; 0..n){
if(vis[i]==false) sz~=dfs(i);
}
enforce(sz.length>=2);
long cul(int v){
long num=sz[v];
foreach(i; 2..(sz.length)) num+=sz[i];
auto ret=num*(num-1)/2;
auto u=v^1;
ret+=sz[u]*(sz[u]-1)/2;
ret-=m;
return ret;
}
writeln(max(cul(0), cul(1)));
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
import std.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 = 998244353;
//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 main()
{
auto H = RD;
auto N = RD;
auto A = RDA;
foreach (e; A)
{
H -= e;
}
writeln(H <= 0 ? "Yes" : "No");
stdout.flush;
debug readln;
}
|
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;
auto w = readln.chomp.to!int;
string x = "";
foreach (i; iota(0,s.length,w)) {
x ~= s[i];
}
x.writeln;
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!long;
const M = scan!long;
const An = scan!long(cast(int)M);
long solve() {
const slack = N - An.sum;
return slack >= 0 ? slack : -1;
}
solve().writeln;
}
// ----------------------------------------------
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");
// -----------------------------------------------
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
long n;
scan(n);
int k;
while (10L^^(k+1)-1 <= 2*n - 1) k++;
debug {
writeln(k);
}
long ans;
foreach (d ; 1 .. 10) {
long x = d * 10L^^k - 1;
if (x > 2*n - 1) break;
auto low = max(1, x - n);
auto high = min(n + 1, (x + 1) / 2);
debug {
writefln("%d, %d", low, high);
}
ans += max(0, min(n + 1, high - low));
}
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.string,std.conv,std.array,std.algorithm;
void main(){
int[string] mark;
mark["S"]=0;
mark["H"]=1;
mark["C"]=2;
mark["D"]=3;
bool[13][4] card;
readln();
for(;;){
auto rcs = readln().chomp().split();
if(!rcs){break;}
card[mark[rcs[0]]][to!int(rcs[1])-1]=true;
}
foreach(m;0..4){foreach(n;0..13){
if(!card[m][n]){
final switch(m){
case 0:write("S ");break;
case 1:write("H ");break;
case 2:write("C ");break;
case 3:write("D ");break;
}
writeln(n+1);
}
}}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
int sum = 0;
foreach(i; 0..10) sum += readln.chomp.to!int;
sum.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!int;
bool[string] res;
foreach (_; 0..N) {
res[readln.chomp] = true;
}
writeln(res.length);
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
void main() {
try {
for (; ; ) {
const S = readToken();
char mn = 'z' + 1;
foreach (c; S) {
writeln((mn < c) ? "Ann" : "Mike");
chmin(mn, c);
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto N = readln.chomp.to!int;
auto S = readln.chomp;
auto cnt = new long[string][](N+1);
long ans = 0;
foreach (mask; 0..(1<<N)) {
string a = "";
string b = "";
foreach (i; 0..N) {
if ((1 << i) & mask) {
a ~= S[i];
} else {
b = S[i] ~ b;
}
}
cnt[mask.popcnt][a~b] += 1;
}
foreach (mask; 0..(1<<N)) {
string a = "";
string b = "";
foreach (i; 0..N) {
if ((1 << i) & mask) {
a ~= S[i+N];
} else {
b = S[i+N] ~ b;
}
}
if ((b ~ a) in cnt[N-mask.popcnt]) {
ans += cnt[N-mask.popcnt][b ~ a];
}
}
ans.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()
{
string s; readV(s);
writeln("ABC", s);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
void main() {
auto s = readln.strip.split.map!(to!int).array;
immutable h = s[0], w = s[1];
auto a = new string[h];
foreach (i; 0 .. h) {
a[i] = readln.strip;
}
int c;
foreach (i; 0 .. h) {
foreach (j; 0 .. w) {
if (a[i][j] == '*') ++c;
}
}
bool test() {
foreach (i; 0 .. h) {
foreach (j; 0 .. w) {
if (a[i][j] == '*') {
int go (int di, int dj) {
int x = i + di;
int y = j + dj;
int r;
while (x >= 0 && x < h && y >= 0 && y < w && a[x][y] == '*') {
++r;
x += di;
y += dj;
}
return r;
}
auto e = new int[4];
e[0] = go (0, 1);
e[1] = go (0, -1);
e[2] = go (1, 0);
e[3] = go (-1, 0);
if (e.minElement () > 0 && e.sum == c - 1) {
return true;
}
}
}
}
return false;
}
writeln (test() ? "YES" : "NO");
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, m; readV(n, m);
auto g = Graph!int(n);
foreach (_; 0..m) {
int a, b; readV(a, b); --a; --b;
g.addEdgeB(a, b);
}
foreach (i; 0..n)
writeln(g[i].length);
}
struct Graph(N = int)
{
alias Node = N;
Node n;
Node[][] g;
alias g this;
this(Node n) { this.n = n; g = new Node[][](n); }
void addEdge(Node u, Node v) { g[u] ~= v; }
void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; }
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
readln;
auto r = readln.chomp.split(" ").map!(to!uint).all!((a) => (a % 2 == 1) || (a % 3 == 0) || (a % 5 == 0));
if (r) {
writeln("APPROVED");
} else {
writeln("DENIED");
}
}
|
D
|
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum E = 18;
void main() {
try {
for (; ; ) {
const N = readInt();
auto A = new string[N];
foreach (i; 0 .. N) {
A[i] = readToken();
}
auto xs = new int[N];
auto ys = new int[N];
foreach (i; 0 .. N) {
string a = A[i];
if (a[$ - 1] != '.') {
a ~= '.';
}
auto as = a.split(".");
for (; as[1].length < 9; ) {
as[1] ~= '0';
}
long val;
val += as[0].to!long;
val *= 10^^9;
val += as[1].to!long;
debug {
writeln("val = ", val);
}
for (; val % 2 == 0; val /= 2) {
++xs[i];
}
for (; val % 5 == 0; val /= 5) {
++ys[i];
}
}
debug {
writeln("xs = ", xs);
writeln("ys = ", ys);
}
auto freq = new long[][](E + 1, E + 1);
foreach (i; 0 .. N) {
++freq[min(xs[i], E)][min(ys[i], E)];
}
long ans;
foreach (x; 0 .. E + 1) foreach (y; 0 .. E + 1) {
foreach (xx; 0 .. E + 1) foreach (yy; 0 .. E + 1) {
if (x + xx >= E && y + yy >= E) {
if ([x, y] == [xx, yy]) {
ans += freq[x][y] * (freq[x][y] - 1) / 2;
} else if ([x, y] < [xx, yy]) {
ans += freq[x][y] * freq[xx][yy];
}
}
}
}
writeln(ans);
}
} catch (EOFException e) {
}
}
|
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);
auto c = new int[](26);
foreach (si; s) c[si-'a']++;
writeln(c.all!"a <= 1" ? "yes" : "no");
}
|
D
|
import std.stdio;
import std.conv;
import std.algorithm;
import std.range;
import std.string;
import std.typecons;
import std.math;
import std.range;
void main()
{
auto count = readln().strip.to!int;
auto m = new int[(count + 1) * (count + 1)].chunks(count + 1);
auto p = new int[count + 1];
foreach (i; iota(count))
{
int x, y;
scanf("%d %d", &x, &y);
p[i] = x;
p[i + 1] = y;
}
for (int l = 2; l <= count; l++)
{
for (int i = 1; i <= count - l + 1; i++)
{
auto j = i + l - 1;
m[i][j] = int.max;
for (int k = i; k <= j - 1; k++)
{
m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
}
}
}
writeln(m[1][count]);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto N = readln.chomp;
auto DP = new long[][](N.length, 2);
foreach (ref dp; DP) dp[] = -1;
long solve(int i, bool k) {
if (i == N.length) return k == 0 ? 0 : long.max/3;
if (DP[i][k] == -1) {
long n = N[i]-'0';
if (k) {
DP[i][k] = min(solve(i+1, 0) + 10 - n, solve(i+1, 1) + 10 - n - 1);
} else {
DP[i][k] = min(solve(i+1, 0) + n, solve(i+1, 1) + n + 1);
}
}
return DP[i][k];
}
writeln(min(solve(0, 0), solve(0, 1) + 1));
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto n = readln.chomp.to!int;
auto ds = readln.chomp.split.to!(int[]);
auto m = readln.chomp.to!int;
auto ts = readln.chomp.split.to!(int[]);
int[int] cnt;
foreach (d; ds) {
cnt[d]++;
}
bool f = false;
foreach (t; ts) {
if (cnt.get(t, 0)) {
cnt[t]--;
} else {
f = true;
break;
}
}
if (f) writeln("NO");
else writeln("YES");
}
|
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() {
string w;
scan(w);
auto c = new int[](26);
foreach (ch ; w) {
c[ch - 'a']++;
}
yes(c.all!"a % 2 == 0");
}
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.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 xs = readints;
int a = xs[0], b = xs[1], t = xs[2];
int ans = t / a * b;
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
int r;
foreach (h; readln.split.to!(int[])) if (h >= K) ++r;
writeln(r);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum as = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51];
void main()
{
writeln(as[readln.chomp.to!int-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 n;
scan(n);
void solve(int i, int shu, string s) {
if (i == n) {
writeln(s);
return;
}
foreach (j ; 0 .. shu) {
solve(i + 1, shu, s ~ ('a' + j).to!char);
}
solve(i + 1, shu + 1, s ~ ('a' + shu).to!char);
}
solve(0, 0, "");
}
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
|
unittest
{
assert( [ "000" ].parse.expand.solve == 1 );
assert( [ "10010010" ].parse.expand.solve == 3 );
assert( [ "0" ].parse.expand.solve == 0 );
}
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
void main()
{
stdin.byLineCopy.parse.expand.solve.writeln;
}
auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
auto s = input.front;
return tuple( s );
}
auto solve( string s )
{
auto wb = 0L; // 白黒白黒…の順で並べるときの塗り替え要の枚数
auto bw = 0L; // 黒白黒白…の順で並べるときの塗り替え要の枚数
foreach( i, c; s )
{
if( i % 2 == 0 )
{
if( c == '0' ) bw++;
else wb++;
}
else
{
if( c == '0' ) wb++;
else bw++;
}
}
return min( wb, bw );
}
|
D
|
import std.algorithm;
import std.array;
import std.stdio;
import std.string;
void main() {
string[] blocks;
loop:
for (;;) {
auto command = readln.strip.split;
auto operation = command[0];
final switch (operation) {
case "push":
auto block = command[1];
blocks ~= block;
break;
case "pop":
auto block = blocks[$-1];
blocks = blocks[0 .. $-1];
writeln(block);
break;
case "quit":
break loop;
}
}
}
|
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() {
int p, q, r; scan(p, q, r);
int ans = min(p + q, q + r, r + p);
writeln(ans);
}
|
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 = 100_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;
foreach (i; iota(n).map!(x => x + 1).array())
{
ans += (i + i * (n / i)) * (n / i) / 2;
}
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 chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
int n = cin.next!int;
int[] ar = cin.nextArray!int(n);
int[int] chie;
foreach (i; ar) {
chie[i] = 1;
}
writeln(chie.length);
}
|
D
|
void main() {
int n = readln.chomp.to!int;
string s = readln.chomp;
int cnt;
foreach (i; 1 .. n) {
int tmp;
foreach (x; lowercase) {
if (s[0..i].canFind(x) && s[i..$].canFind(x)) {
++tmp;
}
}
cnt = max(cnt, tmp);
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto D = s[1];
auto A = iota(N).map!(_ => readln.split.map!(to!long).array).array;
long ans = 0;
foreach (i; 0..N) foreach (j; i+1..N) {
auto x = D.iota.map!(k => (A[i][k] - A[j][k])^^2).sum;
auto sq = sqrt(x.to!real).to!long;
if (sq * sq == x) ans += 1;
}
ans.writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
ulong n = readln.chomp.to!ulong;
ulong[] a = readln.chomp.split.map!(to!ulong).array;
// ulong[] a = make(50 * 10_000_000_000_000_000L);
// ulong n = a.length;
ulong k = 0;
ulong cnt = 0;
for (;;) {
ulong trg = 0;
ulong trg_index = 0;
foreach (i, v; a) {
if (trg < v) {
trg = v;
trg_index = i;
}
}
if (trg < n) {
break;
}
ulong sub = trg / n;
k += sub;
a[trg_index] -= sub * n;
foreach (i, ref v; a) {
if (i == trg_index) {
continue;
}
v += sub;
}
++cnt;
}
// stderr.writeln("loop count : ", cnt);
k.writeln;
}
ulong[] make(ulong k)
{
enum N = 50;
long v = k / N;
immutable remain = k % N;
ulong[] arr;
foreach (i; 0..N) {
if (i < remain) {
arr ~= v + N * 2 - remain;
} else {
arr ~= v + N - (remain + 1);
}
}
return arr;
}
|
D
|
import std.stdio, std.range, std.random, std.conv, std.string, std.math;
import std.algorithm.comparison, std.algorithm.iteration, std.algorithm.mutation, std.algorithm.searching, std.algorithm.sorting;
void main()
{
long[] input = readln().strip.split().to!(long[]);
long K = input[0];
long A = input[1];
long B = input[2];
if(B-A<=2) { writeln(K+1);
return; }
if(K-2-(A-1) < 0)
{
writeln(K+1);
return;
}
long bis = 1 + K;
long c = (K-(A-1))/2;
writeln((B-A)*c+(bis-c*2) + (c==0?1:0));
}
|
D
|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
string s;
s = input();
// writeln(s);
string key;
key = "keyence";
// riteln(s[0 .. 7]);
// if (s[0 .. 7] == key)
// {
// writeln("YES");
// return;
// }
// writeln(s[$ - 7 .. $]);
// if (s[$ - 7 .. $] == key)
// {
// writeln("YES");
// return;
foreach (i; 0 .. key.length)
{
string tmp = s[0 .. i] ~ s[$ - (7 - i) .. $];
if (tmp == key)
{
writeln("YES");
return;
}
}
writeln("NO");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio;
import std.array;
import std.string;
import std.container;
import std.ascii;
import std.random;
import std.algorithm;
import std.conv;
import std.range;
import std.math;
import std.format;
import std.typecons;
import std.numeric;
import std.datetime;
/*
??????????????????, ?????§??¨?????¨, ??¨?????????, ????????????????????????????????????
*/
/**
src???????????°??????G, dst???King's Graph??§??????Gemb
*/
int n, m, u, v, c;
edge[][] g;
void main() {
n = readln.chomp.to!int;
g = new edge[][](n, 0);
for (int i = 0; i < n-1; ++i) {
auto l = readln.chomp.split.map!(to!int).array;
g[l[0]] ~= edge(l[0], l[1], l[2]);
g[l[1]] ~= edge(l[1], l[0], l[2]);
}
edge e = treeDiameter(g);
writeln(e.cost);
}
/***************************************************************************/
// c++???pair??£??????
struct pair {
int first, second;
}
struct edge {
int u, v, cost;
}
class queue(T) {
T[] p;
this() {}
this(T p) {
this.p ~= p;
}
~this() {}
void push(T x) {
p ~= x;
}
void pop() {
assert(p.length > 0);
p.popFront();
}
T front() {
assert(p.length > 0);
return p[0];
}
bool empty() {
if (p.length == 0) return true;
else return false;
}
}
// pair(??´???, ??´??????????????????)
edge treeDiameter(edge[][] graph) {
int s;
pair d1, d2; // pair(distance, vertex)
bool[] used = new bool[](n);
d1 = dfs(graph, used, pair(0, s));
used[] = false;
d2 = dfs(graph, used, pair(0, d1.second));
if (d1.second > d2.second) swap(d1.second, d2.second);
return edge(d1.second, d2.second, d2.first);
}
// ?????????????????????????????????????????????????????????????????????????????¨???????????§????????¢?????????
pair dfs(edge[][] graph, bool[] used, pair p) {
used[p.second] = true;
pair ret = p;
foreach(nv; graph[p.second]) {
if (!used[nv.v]) {
pair tmp = dfs(graph, used, pair(p.first + nv.cost, nv.v));
if (ret.first < tmp.first) {
ret = tmp;
}
}
}
return ret;
}
/***************************************************************************/
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n, k;
scan(n, k);
int ans = n - k + 1;
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.math;
void main()
{
int N = readln.chomp.to!int;
int[] h = readln.split.to!(int[]);
int[] cost = new int[N];
cost[0] = 0;
cost[1] = abs(h[1] - h[0]);
foreach (i; 2..N){
int c0 = cost[i - 2] + abs(h[i] - h[i - 2]);
int c1 = cost[i - 1] + abs(h[i] - h[i - 1]);
if (c0 < c1) cost[i] = c0;
else cost[i] = c1;
}
writeln(cost[$ - 1]);
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.range;
import std.algorithm;
void main(){
foreach(i; 0..readln().chomp().to!int()){
auto input = readln().split();
(input[0].decode() + input[1].decode()).encode().writeln();
}
}
int decode(string input){
int res;
int qual = 1;
foreach(c; input){
switch(c){
case 'm':
res += qual * 1000;
qual = 1;
break;
case 'c':
res += qual * 100;
qual = 1;
break;
case 'x':
res += qual * 10;
qual = 1;
break;
case 'i':
res += qual;
qual = 1;
break;
case '2': .. case '9':
qual = c - '0';
default:
}
}
return res;
}
unittest{
assert(decode("m2c4i") == 1204);
assert(decode("5m2c3x4i") == 5234);
assert(decode("5m2c3x") == 5230);
}
auto keta = ['m', 'c', 'x', 'i'];
string encode(int input){
string res;
foreach(i; 0..4){
int qual = input / 10 ^^ (3 - i);
input -= qual * 10 ^^ (3 - i);
if(qual > 1){
res ~= text(qual, keta[i]);
}else if(qual == 1){
res ~= keta[i];
}
}
return res;
}
unittest{
assert(encode(5234) == "5m2c3x4i");
assert(encode(1204) == "m2c4i");
assert(encode(5230) == "5m2c3x");
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto a = readln.chomp.split.to!(int[]);
auto d = new int[](n+1);
auto c = new int[](n+1);
d[0] = a[0];
c[0] = abs(d[0]);
d[$-1] = 0 - a[$-1];
c[$-1] = abs(d[$-1]);
auto sum = c[0] + c[$-1];
foreach (i; 1..n) {
d[i] = a[i] - a[i-1];
c[i] = abs(d[i]);
sum += c[i];
}
foreach (i; 0..n) {
writeln(sum - c[i] - c[i+1] + abs((d[i+1] + d[i])));
}
}
|
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 n = scan!int;
int x = scan!int - 1, y = scan!int - 1;
int[] ans = new int[](n);
foreach(i; 0 .. n) foreach(j; 0 .. i){
int d = min(abs(i - j), abs(i - y) + abs(j - x) + 1, abs(i - x) + abs(j - y) + 1);
ans[d] += 1;
}
foreach(an; ans[1 .. $]) an.print;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
auto a = readln.chomp.split.map!(to!int);
writeln = max(0, a[0] - a[1]);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;
void main(){
auto ip = readln.split.to!(int[]);
((ip[0] + ip[1]) >= ip[2] ? "Yes" : "No").writeln;
}
|
D
|
void main(){
string[] s = inln!string();
string ans;
foreach(elm; s)ans ~= elm[0];
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
|
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 n = RD!int;
auto a = RDA;
auto b = new long[](n+1);
foreach (i; 0..n)
{
if (i % 2)
{
b[i+1] = b[i] - a[i];
}
else
{
b[i+1] = b[i] + a[i];
}
}
auto c0 = new long[](n+1);
auto c1 = new long[](n+1);
c0[] = long.max;
c1[] = long.max;
long tot;
foreach_reverse (i; 0..n)
{
if (i % 2)
{
c1[i] = min(c1[i+1], b[i+1]);
c0[i] = c0[i+1];
}
else
{
c0[i] = min(c0[i+1], b[i+1]);
c1[i] = c1[i+1];
tot += a[i];
}
}
debug writeln(b);
debug writeln(c0);
debug writeln(c1);
ans[ti] = tot;
foreach (i; 0..n-1)
{
long x;
if (i % 2)
{
x = -(c0[i+1] - b[i]);
debug writeln("i:", i, " ", c0[i+1], " ", b[i]);
}
else
{
x = -(c1[i+1] - b[i]);
debug writeln("i:", i, " ", c1[i+1], " ", b[i]);
}
ans[ti].chmax(tot+x);
debug writeln("i:", i, " ", ans[ti]);
}
}
foreach (e; ans)
{
writeln(e);
}
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 main()
{
int n, a, b; readV(n, a, b);
writeln(min(n*a, b));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto ab = readints;
int a = ab[0], b = ab[1];
if (a == 1) a += 13;
if (b == 1) b += 13;
if (a == b) writeln("Draw");
else if (a > b) writeln("Alice");
else writeln("Bob");
}
|
D
|
import std;
auto input()
{
return readln().chomp();
}
alias sread = () => readln.chomp();
void main()
{
long h, m;
scan(h, m);
auto s = new string[](h);
foreach (i; 0 .. h)
{
s[i] = sread();
}
//writeln(s);
foreach (i; 0 .. h)
{
foreach (j; 0 .. m)
{
if (s[i][j] == '#')
{
if ((i != 0 && s[i - 1][j] == '#') || (i != h - 1
&& s[i + 1][j] == '#') || (j != 0 && s[i][j - 1] == '#')
|| (j != m - 1 && s[i][j + 1] == '#'))
{
continue;
}
writeln("No");
return;
}
}
}
writeln("Yes");
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
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+b)%24);
}
|
D
|
import std.stdio, std.conv, std.string;
import std.algorithm, std.array, std.container;
import std.numeric, std.math;
import core.bitop;
T RD(T)() { static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
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 N = RD!long;
auto M = RD!long;
writeln((N == 1 ? 1 : max(0, N-2)) * (M == 1 ? 1 : max(0, M-2)));
stdout.flush();
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
auto Px = [-1, 0, 1, 0];
auto Py = [ 0, 1, 0,-1];
void main()
{
auto line = split(readln()).map!(a => to!int(a));
int z = 0;
auto ret = solve(line[z++], line[z++], line[z++], line[z++], line[z++]);
writeln(ret ? "Yes" : "No");
}
bool solve(int W, int H, int x, int y, int r)
{
for (int i = 0; i < 4; i++) {
auto px = x - Px[i] * r;
auto py = y - Py[i] * r;
if (!(0 <= py && py <= H && 0 <= px && px <= W)) {
return false;
}
}
return true;
}
|
D
|
// url: http://abc075.contest.atcoder.jp/tasks/abc075_c
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd1 = readln.split.to!(size_t[]), n = rd1[0], m = rd1[1];
struct Edge { size_t a, b; }
auto e = new Edge[](m);
foreach (i; 0..m) {
auto rd2 = readln.split.to!(size_t[]), ai = rd2[0]-1, bi = rd2[1]-1;
e[i] = Edge(ai, bi);
}
auto ans = 0;
foreach (i; 0..m) {
auto uf = UnionFind(n);
foreach (j; 0..m)
if (i != j) uf.unite(e[j].a, e[j].b);
if (uf.countForests > 1) ++ans;
}
writeln(ans);
}
struct UnionFind
{
import std.algorithm, std.range;
size_t[] p; // parent
const size_t s; // sentinel
const size_t n;
size_t countForests; // number of forests
size_t[] countNodes; // number of nodes in forests
this(size_t n)
{
this.n = n;
s = n;
p = new size_t[](n);
p[] = s;
countForests = n;
countNodes = new size_t[](n);
countNodes[] = 1;
}
size_t opIndex(size_t i)
{
if (p[i] == s) {
return i;
} else {
p[i] = this[p[i]];
return p[i];
}
}
bool unite(size_t i, size_t j)
{
auto pi = this[i], pj = this[j];
if (pi != pj) {
p[pj] = pi;
--countForests;
countNodes[pi] += countNodes[pj];
return true;
} else {
return false;
}
}
auto countNodesOf(size_t i) { return countNodes[this[i]]; }
bool isSame(size_t i, size_t j) { return this[i] == this[j]; }
auto groups()
{
auto g = new size_t[][](n);
foreach (i; 0..n) g[this[i]] ~= i;
return g.filter!(l => !l.empty);
}
}
|
D
|
import std.stdio, std.array, std.conv, std.typecons, std.algorithm;
T diff(T)(const ref T a, const ref T b) { return a > b ? a - b : b - a; }
void main() {
immutable ip = readln.split.to!(ulong[]);
immutable n = ip[0], x = ip[1];
immutable ls = readln.split.to!(ulong[]);
ulong cnt = 0;
ulong xi = 0;
while(xi <= x && cnt <= n) {
xi += ls[cnt++];
}
writeln(cnt);
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan(T...)(ref T args) {
foreach (ref arg; args) {
arg = next!(typeof(arg));
}
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType)) {
str ~= s.to!(char[]).strip.split;
}
}
// }}}
// alias {{{
alias Heap(T, alias less = "a < b") = BinaryHeap!(Array!T, less);
alias MinHeap(T) = Heap!(T, "a > b");
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(Array!T, "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
int n, h, w;
int cnt;
cin.scan(n, h, w);
foreach (i; 0 .. n) {
int a, b;
cin.scan(a, b);
if (h <= a && w <= b) cnt++;
}
writeln(cnt);
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
long a, b, x, y, z;
scan(a, b);
scan(x, y, z);
writeln(max(0, 2*x + y - a) + max(0, y + 3*z - b));
}
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.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
int[100] WS, VS;
void main()
{
auto nw = readln.split.to!(int[]);
auto N = nw[0];
auto W = nw[1];
long[][] MEMO;
MEMO.length = N;
foreach (i; 0..N) {
auto wv = readln.split.to!(int[]);
WS[i] = wv[0];
VS[i] = wv[1];
MEMO[i].length = 10^^5+1;
}
long solve(int i, int w) {
if (i == N) return 0;
if (MEMO[i][w]) return MEMO[i][w];
return MEMO[i][w] = max(
solve(i+1, w),
w >= WS[i] ? solve(i+1, w - WS[i]) + VS[i] : 0
);
}
writeln(solve(0, W));
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long x, y; rd(x, y);
long count(long st, long ed){
if(st>ed) return 0;
long ret=1;
while(st*2<=ed) st*=2, ret++;
return ret;
}
writeln(max(count(x, y), count(x+1, y)));
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
void wr(T...)(T x){
import std.stdio;
foreach(e; x) write(e, " ");
writeln();
}
|
D
|
void main() {
import std.stdio, std.string, std.conv, std.algorithm;
int n;
rd(n);
struct P {
int y, x;
}
auto xs = new int[](n), ys = new int[](n);
bool[P] seen;
foreach (i; 0 .. n) {
rd(xs[i], ys[i]);
if (i > 0) {
seen[P(xs[i], ys[i])] = true;
}
}
auto vy = new int[](n), vx = new int[](n);
foreach (i; 0 .. n) {
rd(vx[i], vy[i]);
}
foreach (i; 0 .. n) {
auto tx = xs[0] + vx[i], ty = ys[0] + vy[i];
foreach (j; 0 .. n) {
if (i != j) {
auto p = P(tx - vx[j], ty - vy[j]);
if ((p in seen) == null || !seen[p]) {
goto hell;
}
seen[p] = false;
}
}
writeln(tx, " ", ty);
return;
hell:
// revert
foreach (j; 1 .. n) {
seen[P(xs[j], ys[j])] = true;
}
}
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 std.stdio;
import std.conv;
import std.array;
import std.algorithm;
void main()
{
int n = readln.split[0].to!int;
int[] v = readln.split.to!(int[]);
int[][] counter = new int[][](2, 100000);
foreach (i; 0..n) counter[i % 2][v[i] - 1]++;
ulong[][] max_i = new ulong[][](2, 2);
foreach (i; 0..counter.length){
if (counter[i][0] > counter[i][1]){
max_i[i][1] = 1;
} else {
max_i[i][0] = 1;
}
foreach (j; 2..counter[i].length){
if (counter[i][j] > counter[i][max_i[i][1]]){
max_i[i][1] = j;
if (counter[i][j] > counter[i][max_i[i][0]]){
max_i[i][1] = max_i[i][0];
max_i[i][0] = j;
}
}
}
}
int ans;
if (max_i[0][0] != max_i[1][0]){
ans = n - counter[0][max_i[0][0]] - counter[1][max_i[1][0]];
} else {
ans = n - max(counter[0][max_i[0][0]] + counter[1][max_i[1][1]], counter[0][max_i[0][1]] + counter[1][max_i[1][0]]);
}
writeln(ans);
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
int solve() {
auto s = readln.split.map!(to!int);
int M = s[0];
int N = s[1];
if (M == 0) return -1;
auto A = new int[](M);
auto B = new int[](N);
int a, b;
while (a < M) {
auto T = readln.split.map!(to!int);
foreach (t; T) A[a++] = t;
}
while (b < N) {
auto T = readln.split.map!(to!int);
foreach (t; T) B[b++] = t;
}
auto source = M + N;
auto sink = M + N + 1;
auto ff = new FordFulkerson(M+N+2, source, sink);
foreach (i; 0..M) ff.add_edge(source, i, 1);
foreach (i; 0..N) ff.add_edge(i+M, sink, 1);
foreach (i; 0..M)
foreach (j; 0..N)
if (gcd(A[i], B[j]) > 1)
ff.add_edge(i, j+M, 1);
return ff.run;
}
void main() {
while (true) {
auto ans = solve;
if (ans == -1) break;
ans.writeln;
}
}
class FordFulkerson {
int N, source, sink;
int[][] adj;
int[][] flow;
bool[] used;
this(int n, int s, int t) {
N = n;
source = s;
sink = t;
assert (s >= 0 && s < N && t >= 0 && t < N);
adj = new int[][](N);
flow = new int[][](N, N);
used = new bool[](N);
}
void add_edge(int from, int to, int cap) {
adj[from] ~= to;
adj[to] ~= from;
flow[from][to] = cap;
}
int dfs(int v, int min_cap) {
if (v == sink)
return min_cap;
if (used[v])
return 0;
used[v] = true;
foreach (to; adj[v]) {
if (!used[to] && flow[v][to] > 0) {
auto bottleneck = dfs(to, min(min_cap, flow[v][to]));
if (bottleneck == 0) continue;
flow[v][to] -= bottleneck;
flow[to][v] += bottleneck;
return bottleneck;
}
}
return 0;
}
int run() {
int ret = 0;
while (true) {
foreach (i; 0..N) used[i] = false;
int f = dfs(source, int.max);
if (f > 0)
ret += f;
else
return ret;
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
void main() {
string INF = "|";
string[] input;
while(true) {
input = readln.split;
if (input == ["0", "0", "0", "0"]) break;
int n = input[0].to!int;
int a = input[1].to!int;
int s = input[2].to!int;
int g = input[3].to!int;
string[] vertices = new string[n];
vertices[] = INF;
Edge[] edges = new Edge[a];
foreach(int i; 0..a) {
input = readln.split;
int from = input[1].to!int;
int to = input[0].to!int;
string cost = input[2];
if (from == to) {
edges[i] = Edge(from, vertices.length.to!int, cost);
edges ~= Edge(vertices.length.to!int, to, "");
vertices ~= INF;
} else {
edges[i] = Edge(from, to, cost);
}
}
vertices[g] = "";
bool flg = false;
for (int count=0; count<6*vertices.length; count++) {
for (int i=0; i<edges.length; i++) {
string value = edges[i].cost~vertices[edges[i].from];
if (value < vertices[edges[i].to] && vertices[edges[i].from]!=INF) {
vertices[edges[i].to] = value;
if (count>=vertices.length-1 && edges[i].to==s) {
flg = true;
}
}
}
}
writeln(vertices[s] == INF || flg ? "NO":vertices[s]);
}
}
struct Edge {
int from, to;
string cost;
this(int from, int to, string cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
}
|
D
|
import std.stdio, std.conv, std.string, std.range, std.algorithm, std.array, std.functional;
void main() {
auto input = readln.split.to!(int[]);
auto N = input[0], Q = input[1];
auto tree = new int[][N];
auto cnt = new int[N];
foreach(i; 0..(N-1)) {
input = readln.split.to!(int[]);
auto a = input[0], b = input[1];
tree[a-1] ~= b-1;
tree[b-1] ~= a-1;
}
foreach(i; 0..Q) {
input = readln.split.to!(int[]);
auto p = input[0], x = input[1];
cnt[p-1] += x;
}
auto seen = new bool[N];
void dfs(int n, int parCnt) {
seen[n] = true;
cnt[n] += parCnt;
foreach(c; tree[n]) {
if(!seen[c]) {
dfs(c, cnt[n]);
}
}
}
dfs(0, 0);
foreach(c; cnt) {
write(c, " ");
}
}
|
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 ();
const n = r.next!uint;
const p = r.next!uint;
const k = r.next!uint;
auto a = r.nextA!uint(n);
auto b = new uint[][n];
foreach (i; 0 .. n) {
b[i] = r.nextA!uint (p);
}
auto idx = iota (0, n).array;
idx.sort! ((i, j) => a[i] > a[j]);
const int m = 1 << p;
auto c = new ulong[m];
auto pc = new int[m];
foreach (i; 1 .. m) pc[i] = pc[i & (i-1)] + 1;
auto e = new int[][m];
foreach (i; 0 .. m) {
foreach (j; 0 .. p) {
if (!(i & (1 << j))) {
e[i] ~= j;
}
}
}
auto d = new ulong[m];
foreach (pos, x; idx) {
d[] = c[];
foreach (j; 0 .. m) {
foreach (u; e[j]) {
int o = j + (1 << u);
ulong w = b[x][u] + c[j];
if (d[o] < w) d[o] = w;
}
if (pos - pc[j] < k) {
ulong w = a[x] + c[j];
if (d[j] < w) d[j] = w;
}
}
swap (c, d);
}
writeln (c.back);
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
int a,b;
scanf("%d %d",&a, &b);
writeln(2*b-a);
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
void main() {
readln;
auto a = readln.chomp.dup;
int count;
int left, right = cast(int)a.length - 1;
for (;;) {
while (left < a.length) {
if (a[left] == 'W') {
break;
}
++left;
}
while (right >= 0) {
if (a[right] == 'R') {
break;
}
--right;
}
if (right < left) {
break;
}
++left;
--right;
++count;
}
writeln(count);
}
|
D
|
import std.stdio;
import std.string;
import std.range;
import std.conv;
void main()
{
auto X = readln.chomp.to!int;
auto A = readln.chomp.to!int;
auto B = readln.chomp.to!int;
writeln((X-A)%B);
}
|
D
|
import std.stdio, std.conv, std.string, std.math, std.regex, std.range, std.ascii, std.algorithm;
void main(){
auto N = readln.chomp.to!int;
auto K = readln.chomp.to!int;
auto X = readln.chomp.to!int;
auto Y = readln.chomp.to!int;
if(N-K <= 0){
writeln(N*X);
}else{
writeln(K*X + (N-K)*Y);
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ab = readln.split.to!(int[]);
auto A = ab[0];
auto B = ab[1];
writeln(max(0, A - B*2));
}
|
D
|
void main() {
writeln(readln.chomp.to!int ^^ 2 - readln.chomp.to!int);
}
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.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int A, B, C, D, E, F;
scan(A, B, C, D, E, F);
int ans_w = 100*A, ans_s = 0;
foreach (x ; 0 .. 31) {
foreach (y ; 0 .. 31) {
int water = 100 * (A * x + B * y);
if (water == 0) continue;
int L = min((A*x + B*y) * E, F - 100*(A*x + B*y));
if (L < 0) break;
foreach (w ; 0 .. 3001) {
if (L - C*w < 0) break;
int z = (L - C*w) / D;
//if (z < 0) break;
int sugar = C*w + D*z;
if (ans_s * (water + sugar) < sugar * (ans_w + ans_s)) {
ans_w = water;
ans_s = sugar;
debug {
writeln(ans_w, " ", ans_s);
}
}
}
}
}
writeln(ans_w + ans_s, " ", ans_s);
}
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.conv,std.algorithm;
void main(){
while(1){
auto rd = readln().chomp();
if(rd){
auto v = to!double( rd );
auto t = v/9.8;
auto y = 4.9*t*t;
for(int i=1;i<100000;i++){
if( y <= 5*i-5 ){
writeln(i);
break;
}
}
}else{
break;
}
}
}
|
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,std.mathspecial;
void main(){
auto n=readln.chomp.to!int;
auto as=readln.split.map!(to!long).array;
long fromNega;
long fromPosi;
long currentNega;
long currentPosi;
foreach(i,a;as){
currentNega+=a;
currentPosi+=a;
if(i%2){
if(currentNega>=0){
fromNega+=currentNega+1;
currentNega=-1;
}
if(currentPosi<=0){
fromPosi+=-currentPosi+1;
currentPosi=+1;
}
}else{
if(currentNega<=0){
fromNega+=-currentNega+1;
currentNega=+1;
}
if(currentPosi>=0){
fromPosi+=currentPosi+1;
currentPosi=-1;
}
}
}
writeln(min(fromNega,fromPosi));
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
import std.string;
import std.algorithm;
int main(string[] argv)
{
while (1)
{
auto s = readln().chomp().split().map!(to!int);
int mf = s[0] + s[1];
if (s[0] + s[1] + s[2] == -3) break;
if (s[0] == -1 || s[1] == -1) writeln("F");
else if (80 <= mf) writeln("A");
else if (65 <= mf) writeln("B");
else if (50 <= mf) writeln("C");
else if (30 <= mf)
if (50 <= s[2]) writeln("C");
else writeln("D");
else writeln("F");
}
return 0;
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; long x; readV(n, x);
auto h = new long[](n), p = new long[](n);
h[0] = 1; p[0] = 1;
foreach (i; 0..n) {
h[i+1] = h[i]*2+3;
p[i+1] = p[i]*2+1;
}
long pat(int i, long x)
{
if (i == 0) return 1;
if (x == 1) return 0;
else if (x <= h[i-1]+1) return pat(i-1, x-1);
else if (x == h[i-1]+2) return p[i-1]+1;
else if (x <= h[i-1]*2+2) return p[i-1]+1+pat(i-1, x-h[i-1]-2);
else return p[i-1]*2+1;
}
writeln(pat(n, x));
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp;
long solve(size_t i, long sum) {
if (i == S.length) return sum;
long r;
foreach (j; i+1..S.length+1) {
r += solve(j, sum + S[i..j].to!long);
}
return r;
}
writeln(solve(0, 0));
}
|
D
|
/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons;
import std.bigint;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.container.deque;
int main() {
auto sc = new Scanner(stdin);
int n, m, k;
sc.read(n, m, k); m++;
int[] a = new int[n];
a.each!((ref x) => sc.read(x));
long[] dp = a.map!(to!long).array;
long[] ndp = new long[n];
auto deq = Deque!(int, true).make();
auto p = deq.p;
foreach (int ph; 2..k+1) {
ndp[] = -(10L^^18);
deq.clear();
foreach (int i; 0..n) {
if (p.length && (*p)[0] == i-m) {
p.removeFront();
}
if (p.length) {
ndp[i] = dp[(*p)[0]] + 1L * ph * a[i];
}
while (p.length && dp[(*p)[p.length-1]] <= dp[i]) {
p.removeBack();
}
p.insertBack(i);
}
swap(dp, ndp);
}
writeln(dp.fold!max);
return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
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);
}
}
}
unittest {
import std.stdio;
auto l = [1, 2, 3, 4, 5];
assert(l.fold!"a+b"(10) == 25);
}
}
version (X86) static if (__VERSION__ < 2071) {
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 /home/yosupo/Program/dcomp/source/dcomp/container/deque.d */
// module dcomp.container.deque;
struct Deque(T, bool hasNull = true) {
import core.exception : RangeError;
import core.memory : GC;
import std.range : ElementType, isInputRange;
import std.traits : isImplicitlyConvertible;
struct Payload {
T *d;
size_t st, length, cap;
@property bool empty() const { return length == 0; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {
version(assert) if (length <= i) throw new RangeError();
return d[(st+i >= cap) ? (st+i-cap) : st+i];
}
private void expand() {
import std.algorithm : max;
assert(length == cap);
auto nc = max(size_t(4), 2*cap);
T* nd = cast(T*)GC.malloc(nc * T.sizeof);
foreach (i; 0..length) {
nd[i] = this[i];
}
d = nd; st = 0; cap = nc;
}
void clear() {
st = length = 0;
}
void insertFront(T v) {
if (length == cap) expand();
if (st == 0) st += cap;
st--; length++;
this[0] = v;
}
void insertBack(T v) {
if (length == cap) expand();
length++;
this[length-1] = v;
}
void removeFront() {
assert(!empty, "Deque.removeFront: Deque is empty");
st++; length--;
if (st == cap) st = 0;
}
void removeBack() {
assert(!empty, "Deque.removeBack: Deque is empty");
length--;
}
}
struct RangeT(A) {
alias T = typeof(*(A.p));
alias E = typeof(A.p.d[0]);
T *p;
size_t a, b;
@property bool empty() const { return b <= a; }
@property size_t length() const { return b-a; }
@property RangeT save() { return RangeT(p, a, b); }
@property RangeT!(const A) save() const {
return typeof(return)(p, a, b);
}
alias opDollar = length;
@property ref inout(E) front() inout { return (*p)[a]; }
@property ref inout(E) back() inout { return (*p)[b-1]; }
void popFront() {
version(assert) if (empty) throw new RangeError();
a++;
}
void popBack() {
version(assert) if (empty) throw new RangeError();
b--;
}
ref inout(E) opIndex(size_t i) inout { return (*p)[i]; }
RangeT opSlice() { return this.save; }
RangeT opSlice(size_t i, size_t j) {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
RangeT!(const A) opSlice() const { return this.save; }
RangeT!(const A) opSlice(size_t i, size_t j) const {
version(assert) if (i > j || a + j > b) throw new RangeError();
return typeof(return)(p, a+i, a+j);
}
}
alias Range = RangeT!Deque;
alias ConstRange = RangeT!(const Deque);
alias ImmutableRange = RangeT!(immutable Deque);
Payload *p;
private void I() { if (hasNull && !p) p = new Payload(); }
private void C() const { version(assert) if (!p) throw new RangeError(); }
//some value
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) {I;
p = new Payload();
foreach (v; values) {
insertBack(v);
}
}
//range
this(Range)(Range r)
if (isInputRange!Range &&
isImplicitlyConvertible!(ElementType!Range, T) &&
!is(Range == T[])) {I;
p = new Payload();
foreach (v; r) {
insertBack(v);
}
}
static Deque make() {
Deque que;
que.p = new Payload();
return que;
}
@property private bool hasPayload() const { return (!hasNull || p); }
@property bool empty() const { return (!hasPayload || p.empty); }
@property size_t length() const { return (hasPayload ? p.length : 0); }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; }
ref inout(T) front() inout {C; return (*p)[0]; }
ref inout(T) back() inout {C; return (*p)[$-1]; }
void clear() { if (p) p.clear(); }
void insertFront(T v) {I; p.insertFront(v); }
void insertBack(T v) {I; p.insertBack(v); }
void removeFront() {C; p.removeFront(); }
void removeBack() {C; p.removeBack(); }
Range opSlice() {I; return Range(p, 0, length); }
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
assert(isRandomAccessRange!(typeof(q[])));
//insert,remove
assert(equal(q[], new int[](0)));
q.insertBack(1);
assert(equal(q[], [1]));
q.insertBack(2);
assert(equal(q[], [1, 2]));
q.insertFront(3);
assert(equal(q[], [3, 1, 2]) && q.front == 3);
q.removeFront;
assert(equal(q[], [1, 2]) && q.length == 2);
q.insertBack(4);
assert(equal(q[], [1, 2, 4]) && q.front == 1 && q.back == 4 && q[$-1] == 4);
q.insertFront(5);
assert(equal(q[], [5, 1, 2, 4]));
//range
assert(equal(q[][1..3], [1, 2]));
assert(equal(q[][][][], q[]));
//const range
const auto rng = q[];
assert(rng.front == 5 && rng.back == 4);
//reference type
auto q2 = q;
q2.insertBack(6);
q2.insertFront(7);
assert(equal(q[], q2[]) && q.length == q2.length);
//construct with make
auto a = make!(Deque!int)(1, 2, 3);
auto b = make!(Deque!int)([1, 2, 3]);
assert(equal(a[], b[]));
}
unittest {
import std.algorithm : equal;
import std.range.primitives : isRandomAccessRange;
import std.container.util : make;
auto q = make!(Deque!int);
q.clear();
assert(equal(q[], new int[0]));
foreach (i; 0..100) {
q.insertBack(1);
q.insertBack(2);
q.insertBack(3);
q.insertBack(4);
q.insertBack(5);
assert(equal(q[], [1,2,3,4,5]));
q.clear();
assert(equal(q[], new int[0]));
}
}
unittest {
Deque!int a;
Deque!int b;
a.insertFront(2);
assert(b.length == 0);
}
unittest {
import std.algorithm : equal;
import std.range : iota;
Deque!int a;
foreach (i; 0..100) {
a.insertBack(i);
}
assert(equal(a[], iota(100)));
}
/* IMPORT /home/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) {
//string or char[10] etc
//todo optimize
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) {
//static
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);
}
}
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
fout.writeln("1 2 3");
fout.writeln("ab cde");
fout.writeln("1.0 1.0 2.0");
fout.close;
Scanner sc = new Scanner(File(fileName, "r"));
int a;
int[2] b;
char[2] c;
string d;
double e;
double[] f;
sc.read(a, b, c, d, e, f);
assert(a == 1);
assert(equal(b[], [2, 3]));
assert(equal(c[], "ab"));
assert(equal(d, "cde"));
assert(e == 1.0);
assert(equal(f, [1.0, 2.0]));
}
unittest {
import std.path : buildPath;
import std.file : tempDir;
import std.algorithm : equal;
import std.stdio : File, writeln;
import std.datetime;
string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
auto fout = File(fileName, "w");
foreach (i; 0..1_000_000) {
fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
}
fout.close;
writeln("Scanner Speed Test(3*1,000,000 int)");
StopWatch sw;
sw.start;
Scanner sc = new Scanner(File(fileName, "r"));
foreach (i; 0..500_000) {
int a, b, c;
sc.read(a, b, c);
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 500_000..700_000) {
int[3] d;
sc.read(d);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
foreach (i; 700_000..1_000_000) {
int[] d;
sc.read(d);
assert(d.length == 3);
int a = d[0], b = d[1], c = d[2];
assert(a == 3*i);
assert(b == 3*i+1);
assert(c == 3*i+2);
}
writeln(sw.peek.msecs, "ms");
}
|
D
|
import std.stdio : writeln;
void main()
{
foreach(i; 1..10){
foreach(j; 1..10){
writeln(i, "x", j, "=", i*j);
}
}
}
|
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;
if (N == 3) {
writeln("2 5 63");
} else if (N == 4) {
writeln("2 5 20 63");
} else if (N == 5) {
writeln("2 3 5 8 12");
} else {
int[] r;
r.length = N;
int i, n = 2, m;
while (i < N) {
r[i++] = n;
m = (m + n) % 6;
do {
++n;
}
while (n%2 != 0 && n%3 != 0);
}
switch (m) {
case 2:
i = 4;
while (n%6 != 0) ++n;
break;
case 3:
i = 5;
while (n%6 != 0) ++n;
break;
case 5:
i = 5;
while (n%6 != 4) ++n;
break;
default:
i = 0;
n = r[i];
}
r[i] = n;
writeln(r.to!(string[]).join(" "));
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
alias T = Tuple!(long, long, long);
long[] vs;
long[T] memo;
long n, k;
long f(long l, long r, long m) {
auto key = tuple(l, r, m);
if (key in memo) return memo[key];
if (m < 0 || n-r-1 < l) return 0;
long[] cands = [0];
cands ~= vs[l] + f(l+1, r , m-1);
cands ~= vs[$-r-1] + f(l , r+1, m-1);
if (m > 1) {
cands ~= f(l+1, r , m-2);
cands ~= f(l , r+1, m-2);
}
memo[key] = cands.reduce!max;
return memo[key];
}
void main() {
scanf("%ld %ld\n", &n, &k);
vs = readln.chomp.split.to!(long[]);
f(0, 0, k-1).write;
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format, std.datetime;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long X = lread();
long ans;
ans += (X / 500) * 1000;
ans += ((X % 500) / 5) * 5;
writeln(ans);
}
|
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;
int n;
void main(){
n = readln.chomp.to!int;
bool[][] xss;
foreach(i; 0 .. n){
xss ~= readln.chomp.map!(x => (x == '#')).array;
}
int donecolumncount = 0;
foreach(j; 0 .. n){
bool f = 1;
foreach(i; 0 .. n){
if(!xss[i][j]) f = 0;
}
if(f) donecolumncount += 1;
}
int cmin = n + 1;
foreach(i; 0 .. n){
int c = 0;
foreach(j; 0 .. n){
if(!xss[i][j]){
c += 1;
if(i == j){
int f = 1;
foreach(ii; 0 .. n){
if(xss[ii][j]) f = 0;
}
c += f;
}
}
}
if(c < cmin) cmin = c;
}
int ans;
if(cmin > n) ans = -1;
else ans = cmin + n - donecolumncount;
ans.writeln;
}
|
D
|
import std.algorithm;
import std.range;
import std.math;
import std.regex;
import std.array;
import std.container;
import std.stdio;
import std.string;
import std.conv;
string[] readStrs() { return readln.chomp.split(' '); }
T[] readInts(T)() { return readln.chomp.split(' ').map!(to!T).array(); }
void main()
{
auto ss = readStrs();
if(ss[1] == "+")
writeln(ss[0].to!long + ss[2].to!long);
else
writeln(ss[0].to!long - ss[2].to!long);
}
|
D
|
import std.stdio,std.string,std.conv,std.array;
void main(){
//for(;;){
auto rc = readln().chomp();
auto transTable1 = makeTrans("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
writeln( translate(rc, transTable1) );
//}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
long[] make_lucas()
{
long[] ls = [2,1];
for (int i = 2; i < 87; ++i) {
ls ~= ls[i-1] + ls[i-2];
}
return ls;
}
enum LS = make_lucas();
void main()
{
writeln(LS[readln.chomp.to!int]);
}
|
D
|
void main()
{
long n, k;
rdVals(n, k);
long[] a = rdRow;
long b = bsr(k << 1);
long[] cnt = new long[b];
foreach (x; a)
{
foreach (i; 0 .. b)
{
if ((x >> i) & 1) ++cnt[i];
}
}
long x;
foreach_reverse (i, c; cnt)
{
if ((x | (1L << i)) > k) continue;
if (c <= (n >> 1)) x |= (1L << i);
}
long f;
foreach (y; a)
{
f += x ^ y;
}
f.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 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.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto S = readln.chomp;
auto T = readln.chomp;
long hatena = 0;
foreach (s; S) if (s == '?') hatena += 1;
auto cnt2 = new long[](26);
foreach (s; S) if (s != '?') cnt2[s - 'a'] += 1;
auto cnt = new long[](26);
foreach (t; T) cnt[t - 'a'] += 1;
long hi = 10^^7;
long lo = 0;
auto hoge = new long[](26);
long h;
while (hi - lo > 1) {
long mid = (hi + lo) / 2;
foreach (i; 0..26) hoge[i] = cnt[i] * mid - cnt2[i];
h = hatena;
foreach (i; 0..26) h -= max(0, hoge[i]);
if (h >= 0) lo = mid;
else hi = mid;
}
foreach (i; 0..26) cnt[i] = cnt[i] * lo - cnt2[i];
int p = 0;
while (p < 26 && cnt[p] <= 0) p += 1;
auto ans = new char[](S.length);
foreach (i; 0..S.length) {
if (S[i] == '?' && p < 26) {
ans[i] = (p.to!char + 'a').to!char;
cnt[p] -= 1;
while (p < 26 && cnt[p] <= 0) p += 1;
} else if (S[i] == '?' && p >= 26){
ans[i] = 'z';
} else {
ans[i] = S[i];
}
}
ans.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)); }
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);
auto arr = new long[]((10^^5) * 3);
foreach (i; 1..(10^^5) * 3)
{
arr[i] = arr[i-1] ^ i;
}
foreach (ti; 0..t)
{
auto a = RD!int;
auto b = RD!int;
auto x = arr[a-1] ^ b;
if (x == 0)
ans[ti] = a;
else if (x == a)
ans[ti] = a + 2;
else
ans[ti] = a + 1;
}
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.