code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container;
void main()
{
auto nw = readln.split.to!(int[]);
auto N = nw[0];
long W = nw[1];
int[] vs;
long[] ws;
foreach (_; 0..N) {
auto vw = readln.split.to!(int[]);
vs ~= vw[0];
ws ~= vw[1];
}
auto DP = new long[][](N+1, 100*N+1);
foreach (ref dp; DP) dp[] = long.max/3;
DP[0][0] = 0;
foreach (i; 0..N) {
foreach (v; 0..100*N+1) {
DP[i+1][v] = min(DP[i+1][v], DP[i][v]);
if (v >= vs[i]) DP[i+1][v] = min(DP[i+1][v], DP[i][v-vs[i]] + ws[i]);
}
}
foreach_reverse (v; 0..100*N+1) if (DP[N][v] <= W) {
writeln(v);
return;
}
}
|
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, m = scan!int;
auto uf = new UnionFind(n);
foreach(j; 0 .. m){
int a = scan!int - 1, b = scan!int - 1;
uf.unite(a, b);
}
int ans = 0;
foreach(i; 0 .. n){
ans.raiseTo(uf.getSize(i));
}
ans.writeln;
}
import std.conv;
class UnionFind{
int[] rootOf;
int[][] kidsOf;
this(int n){
rootOf = new int[](n);
kidsOf = new int[][](n);
foreach(i; 0 .. n) rootOf[i] = i, kidsOf[i] = [i];
}
void unite(int a, int b){
int ra = rootOf[a], rb = rootOf[b];
if(ra == rb) return;
if(kidsOf[ra].length < kidsOf[rb].length) unite(b, a);
else foreach(k; kidsOf[rb]) rootOf[k] = ra, kidsOf[ra] ~= k;
}
int find(int a){
return rootOf[a];
}
int getSize(int a){
return kidsOf[rootOf[a]].length.to!int;
}
}
|
D
|
void main() {
writeln(canFind(rs, "AC") ? "Yes" : "No");
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
enum PS = [3, 13, 23, 43, 53, 73, 83, 103, 113, 163, 173, 193, 223, 233, 263, 283, 293, 313, 353, 373, 383, 433, 443, 463, 503, 523, 563, 593, 613, 643, 653, 673, 683, 733, 743, 773, 823, 853, 863, 883, 953, 983, 1013, 1033, 1063, 1093, 1103, 1123, 1153, 1163, 1193, 1213, 1223, 1283, 1303];
void main()
{
writeln(PS[0..readln.chomp.to!int].to!(string[]).join(" "));
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}
void main()
{
int n, m; readV(n, m);
long[] x, y, z; readC(n, x, y, z);
auto r = -10L^^18;
foreach (p; 0..1<<3) {
auto sx = p.bitTest(0) ? 1 : -1;
auto sy = p.bitTest(1) ? 1 : -1;
auto sz = p.bitTest(2) ? 1 : -1;
auto dp = new long[][](n+1, m+1);
foreach (dpi; dp) dpi[] = -10L^^18;
dp[0][0] = 0;
foreach (i; 0..n)
foreach (j; 0..m+1) {
dp[i+1][j] = dp[i][j];
if (j > 0)
dp[i+1][j] = max(dp[i+1][j], dp[i][j-1]+x[i]*sx+y[i]*sy+z[i]*sz);
}
r = max(r, dp[n][m]);
}
writeln(r);
}
pragma(inline) {
pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
import core.bitop;
pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
void main() {
int N;
scan(N);
auto cnt = new int[](26);
cnt[] = inf;
foreach (i ; 0 .. N) {
string s;
scan(s);
auto c = new int[](26);
foreach (ch ; s) {
c[ch - 'a']++;
}
foreach (j ; 0 .. 26) {
chmin(cnt[j], c[j]);
}
}
string ans;
foreach (i ; 0 .. 26) {
foreach (j ; 0 .. cnt[i]) {
ans ~= (i + 'a').to!char;
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
auto solve(string s_) {
auto NT = s_.split.map!(to!int)();
immutable N=NT[0], T=NT[1];
auto A = readln.split.map!(to!int).array();
int m=0,d=0,nl=0, nr=0, n=0, nm=0;
foreach_reverse(v;A) {
if(v>m) m=v,++n,nm=1;
else if(v==m) ++n,++nm;
else if(m-v>d) d=m-v,nl=1,nr=nm,n=nm;
else if(m-v==d) ++nl,nr=n;
}
return min(nl,nr);
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
|
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 n;
scan(n);
if (n <= 999) {
writeln("ABC");
}
else {
writeln("ABD");
}
}
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.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 s;
scan(s);
auto ok = iota(3).all!(i => s[i] == s[0]);
yes(!ok);
}
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.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
string s;
do{
char[][] m;
while((s=readln()).chomp().length) m~=s.chomp().to!(char[]);
void update(int x, int y)
{
if(x<0 || m.length<=x || y<0 || m[x].length<=y)
return;
if(m[x][y]=='0') return;
m[x][y]='0';
foreach(a;[[1,0],[-1,0],[0,1],[0,-1]])
update(x+a[0],y+a[1]);
}
int c=0;
foreach(x;0..cast(int)(m.length))
foreach(y;0..cast(int)(m[x].length))
if(m[x][y]=='1') ++c,update(x,y);
writeln(c);
}while(s.length);
}
|
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;
auto ts = readints;
int sum = ts.sum;
int m = readint;
for (int i = 0; i < m; i++) {
auto px = readints;
int p = px[0] - 1;
int x = px[1];
writeln(sum - ts[p] + x);
}
}
|
D
|
/+ dub.sdl:
name "C"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
scope(exit) assert(!sc.hasNext);
int n, m;
sc.read(n, m);
int a, b; //a : <>, b : ^v
sc.read(a, b);
char[][] g = new char[][](n, m);
foreach (i; 0..n) foreach (j; 0..m) g[i][j] = '.';
if (n % 2) {
foreach (i; 0..m/2) {
if (!a) break;
a--;
g[n-1][2*i] = '<';
g[n-1][2*i+1] = '>';
}
}
if (m % 2) {
foreach (i; 0..n/2) {
if (!b) break;
b--;
g[2*i + (n%2)][m-1] = '^';
g[2*i+1 + (n%2)][m-1] = 'v';
}
}
if (a % 2 && b % 2 && n % 2 && m % 2 && n != 1 && m != 1) {
a--; b--;
g[0][m-3] = '^';
g[1][m-3] = 'v';
g[0][m-2] = '<';
g[0][m-1] = '>';
}
foreach (i; 0..n/2) {
foreach (j; 0..m/2) {
if (g[2*i][2*j] != '.') continue;
if (a) {
a--;
g[2*i][2*j] = '<';
g[2*i][2*j+1] = '>';
if (a) {
a--;
g[2*i+1][2*j] = '<';
g[2*i+1][2*j+1] = '>';
}
} else if (b) {
b--;
g[2*i][2*j] = '^';
g[2*i+1][2*j] = 'v';
if (b) {
b--;
g[2*i][2*j+1] = '^';
g[2*i+1][2*j+1] = 'v';
}
}
}
}
if (a || b) {
writeln("NO");
return 0;
}
writeln("YES");
foreach (s; g) {
writeln(s);
}
return 0;
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
import 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, "begin", long, "end");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
long n, r;
scan(n, r);
if(n < 10)
writeln(r + 100 * (10 - n));
else
writeln(r);
}
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;
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 = new string[](3);
foreach (i; 0..3) {
s[i] = readln.chomp;
}
auto idx = new int[](3);
int i;
while (idx[i] < s[i].length) {
idx[i]++;
i = s[i][idx[i]-1]-'a';
}
writeln(('A'+i).to!char);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto ap = readln.split.to!(int[]);
auto A = ap[0];
auto P = ap[1];
writeln((P + A*3)/2);
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;
int[100001] dp;
int solve(int n) {
if (n == 0)
return 0;
if (dp[n] != -1)
return dp[n];
int ret = solve(n - 1) + 1;
for (int d = 6; n - d >= 0; d *= 6)
ret = min(solve(n - d) + 1, ret);
for (int d = 9; n - d >= 0; d *= 9)
ret = min(solve(n - d) + 1, ret);
return dp[n] = ret;
}
void main() {
int N; readlnTo(N);
foreach(i; 0..N+1)
dp[i] = -1;
writeln(solve(N));
}
// helpers
void readlnTo(T...)(ref T t) {
auto s = readln.split;
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
|
D
|
void main() {
auto H = readAs!long;
ulong cnt;
long[] queue = [H];
long[long] m;
m[1] = 1;
long dfs(long n) {
if(n in m) return m[n];
return m[n] = 2*dfs(n/2) + 1;
}
dfs(H).writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto abc = readln.split.to!(int[]);
auto A = abc[0];
auto B = abc[1];
auto C = abc[2];
writeln(min(B / A, C));
}
|
D
|
import std.stdio : writeln;
import std.array;
import std.range;
import std.typecons;
import std.algorithm : max, min;
immutable INF = 1 << 27;
immutable N = 100;
int n, m;
int[ N + 1 ][ N + 1 ] adj;
int[ N + 1 ][ N + 1 ] two_link;
void init(){
init_d_cost();
init_two_link();
}
void init_two_link(){
for( int i = 1; i <= n ; ++i ){
for( int j = i + 1; j <= n ; ++j ){
for( int k = 1; k <= n ; ++k ){
if( adj[ i ][ k ] * adj[ k ][ j ] ){
two_link[ i ][ j ] = two_link[ j ][ i ] = k;
}
}
}
}
}
int[ N + 1 ] d_cost;
void init_d_cost(){
d_cost = repeat( INF ).take( N + 1 ).array;
d_cost[ n ] = 0;
int e = n;
bool[ N + 1 ] done;
while( e ){
done[ e ] = true;
for( int i = 1; i <= n; ++i ){
if( !done[ i ] && adj[ e ][ i ] ){
int res = d_cost[ e ] + adj[ e ][ i ];
if( d_cost[ i ] > res ){
d_cost[ i ] = res;
}
}
}
int min_ = INF;
int min_idx = 0;
for( int i = 1; i <= n; ++i ){
if( !done[ i ] && min_ > d_cost[ i ] ){
min_ = d_cost[ i ];
min_idx = i;
}
}
e = min_idx;
}
}
int ans = INF;
bool[ N + 1 ] s_done;
int[ int ] s_note;
void search( int e, int sum ){
if( e == n ){
ans = min( ans, sum );
return;
}
if( sum >= ans ){
return;
}
if( ( e in s_note ) != null ){
int res = s_note[ e ];
if( sum >= res ){
return;
}
}
s_note[ e ] = sum;
s_done[ e ] = true;
for( int i = 1; i <= n; ++i ){
if( two_link[ e ][ i ] ){
int res = sum + d_cost[ i ];
ans = min( ans, res );
}
}
for( int i = 1; i <= n; ++i ){
if( adj[ e ][ i ] ){
if( !s_done[ i ] ){
search( i, sum + adj[ e ][ i ] );
}
}
}
s_done[ e ] = false;
}
void main(){
while( 1 ){
n = next!int;
m = next!int;
if( ( n | m ) == 0 ){
break;
}
ans = INF;
adj = adj.init;
two_link = two_link.init;
s_done = s_done.init;
s_note = s_note.init;
foreach( i; m.iota ){
int a = next!int;
int b = next!int;
int c = next!int;
adj[ a ][ b ] = adj[ b ][ a ] = c;
}
init();
search( 1, 0 );
ans.writeln;
}
}
import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
assert(hasNext());
}
out
{
input.popFront;
}
body
{
return input.front.to!T;
}
bool hasNext(){
if(input.length > 0){
return true;
}
string str = readln;
if(str.length > 0){
input ~= str.chomp.split(delim);
return true;
}else{
return false;
}
}
void dbg(T...)(T vs)
{
import std.stdio : stderr;
foreach(v; vs)
stderr.write(v.to!string ~ " ");
stderr.write("\n");
}
T clone(T)(T v){
T v_;
static if(isInputRange!(T)){
foreach(ite; v){
v_ ~= ite.clone;
}
}else{
v_ = v;
}
return v_;
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
class InputReader {
private:
ubyte[] p;
ubyte[] buffer;
size_t cur;
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
p = stdin.rawRead (buffer);
}
final ubyte skipByte (ubyte lo) {
while (true) {
auto a = p[cur .. $];
auto r = a.find! (c => c >= lo);
if (!r.empty) {
cur += a.length - r.length;
return p[cur++];
}
p = stdin.rawRead (buffer);
cur = 0;
if (p.empty) return 0;
}
}
final ubyte nextByte () {
if (cur < p.length) {
return p[cur++];
}
p = stdin.rawRead (buffer);
if (p.empty) return 0;
cur = 1;
return p[0];
}
template next(T) if (isSigned!T) {
final T next () {
T res;
ubyte b = skipByte (45);
if (b == 45) {
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
final T next () {
T res = skipByte (48) - 48;
while (true) {
ubyte b = nextByte ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
}
bool check (int[][] a, int[][] b, int h, int w) {
auto r = new int[h];
foreach (i; 0 .. h) {
int t;
foreach (j; 0 .. w) {
t += a[i][j] ^ b[i][j];
}
if (t & 1) return false;
}
foreach (j; 0 .. w) {
int t;
foreach (i; 0 .. h) {
t += a[i][j] ^ b[i][j];
}
if (t & 1) return false;
}
return true;
}
void main() {
auto r = new InputReader;
immutable h = r.next!uint;
immutable w = r.next!uint;
auto a = uninitializedArray!(int[][]) (h, w);
auto b = uninitializedArray!(int[][]) (h, w);
foreach (i; 0 .. h) foreach (j; 0 .. w) a[i][j] = r.next!uint;
foreach (i; 0 .. h) foreach (j; 0 .. w) b[i][j] = r.next!uint;
writeln (check (a, b, h, w) ? "Yes" : "No");
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std;
// dfmt on
void main()
{
long N = lread();
auto ans = "";
while (N != 0)
{
ans = cast(char)('a' + (N - 1) % 26) ~ ans;
N--;
N /= 26;
}
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;
void main(){
int n = readln.chomp.to!int;
string s = readln.chomp;
ulong mod = 1_000_000_007;
ulong[][] dp; // dp[j][i] := # of ways to make (any) string of length i by pressing j times
foreach(j; 0 .. n + 2){
dp ~= [0];
dp[j].length = n + 2;
foreach(i; 0 .. n + 2){
if(j == 0){
if(i == 0) dp[j][i] = 1;
else dp[j][i] = 0;
}
else{
if(i == 0) dp[j][i] = dp[j - 1][i] + dp[j - 1][i + 1];
else if(i < n - 1)
dp[j][i] = dp[j - 1][i - 1] * 2 + dp[j - 1][i + 1];
else
dp[j][i] = dp[j - 1][i - 1] * 2;
}
dp[j][i] %= mod;
}
}
ulong ans = dp[n][s.length];
foreach(k; s){
if(ans % 2 == 0) ans /= 2;
else ans = (ans + mod) / 2;
}
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)); }
//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 c0 = RD!int;
auto c1 = RD!int;
auto h = RD!int;
auto s = RD!string;
auto c = c0 < c1 ? '1' : '0';
auto d = abs(c0 - c1) - h;
foreach (i; 0..n)
{
if (s[i] == '0')
ans[ti] += c0;
else
ans[ti] += c1;
if (s[i] == c)
{
ans[ti] -= max(0, d);
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;
import std.range;
T readNum(T)(){
return readStr.to!T;
}
T[] readNums(T)(){
return readStr.split.to!(T[]);
}
string readStr(){
return readln.chomp;
}
void main(){
auto nl = readNums!int;
auto n = nl[0], l = nl[1];
auto r = iota(l, l+n);
auto sum = r.sum;
int min = 1000;
foreach(i; r){
if(min.abs > i.abs) min = i;
}
writeln(sum - min);
}
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
void main(){
int n = read.to!int;
int[] ps;
foreach(i; 0 .. n) ps ~= read.to!int - 1;
debug writeln("ps:", ps);
bool[] xs = new bool[](n); // xs[i]: number i has already appeard
bool[] ys = new bool[](n); // ys[i]: number (i + 1) appears later than number i in ps
foreach(p; ps){
xs[p] = 1;
ys[p] = (p < n - 1 && !xs[p + 1]);
}
debug writeln("xs:", xs);
debug writeln("ys:", ys);
int ans = 0;
int temp = 0;
foreach(y; ys){
temp += 1;
if(y == 0){
if(temp > ans) ans = temp;
temp = 0;
}
debug writeln("y:", y, " ans:", ans, " temp:", temp);
}
if(temp > ans) ans = temp;
(n - ans).writeln;
}
|
D
|
import std;
enum inf(T)()if(__traits(isArithmetic,T)){return T.max/4;}
T scan(T=long)(){return readln.chomp.to!T;}
void scan(T...)(ref T args){auto input=readln.chomp.split;foreach(i,t;T)args[i]=input[i].to!t;}T[] scanarr(T=long)(){return readln.chomp.split.to!(T[]);}
alias Queue=DList;auto enq(T)(ref Queue!T q,T e){q.insertBack(e);}T deq(T)(ref Queue!T q){T e=q.front;q.removeFront;return e;}
alias Stack=SList;auto push(T)(ref Stack!T s,T e){s.insert(e);}T pop(T)(ref Stack!T s){T e=s.front;s.removeFront;return e;}
struct UnionFind(T){T[T]u;ulong[T] rank;@property{bool inc(T e){return e in u;}auto size(){return u.keys.length;}auto dup(){T[] child=u.keys;T[] parent=u.values;auto res=UnionFind!T(child);child.each!(e=>res.add(e));size.iota.each!(i=>res.unite(child[i],parent[i]));return res;}}this(T e){e.add;}this(T[] es){es.each!(e=>e.add);}auto add(T a,T b=a){assert(b.inc);u[a]=a;rank[a];if(a!=b)unite(a,b);}auto find(T e){if(u[e]==e)return e;return u[e]=find(u[e]);}auto same(T a,T b){return a.find==b.find;}auto unite(T a,T b){a=a.find;b=b.find;if(a==b)return;if(rank[a]<rank[b])u[a]=b;else{u[b]=a;if(rank[a]==rank[b])rank[a]++;}}}
struct PriorityQueue(T,alias less="a<b"){BinaryHeap!(T[],less) heap;@property{bool empty(){return heap.empty;}auto length(){return heap.length;}auto dup(){return PriorityQueue!(T,less)(array);}T[] array(){T[] res;auto tp=heap.dup;foreach(i;0..length){res~=tp.front;tp.removeFront;}return res;}void push(T e){heap.insert(e);}void push(T[] es){es.each!(e=>heap.insert(e));}}T look(){return heap.front;}T pop(){T tp=look;heap.removeFront;return tp;}this(T e){ heap=heapify!(less,T[])([e]);} this(T[] e){ heap=heapify!(less,T[])(e);}}
//END OF TEMPLATE
void main(){
auto c=scan!string;
(c=="ABC"?"ARC":"ABC").writeln;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
ulong MOD = 1_000_000_007;
ulong INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias TOWN = Tuple!(long, "x", long, "y");
void main()
{
auto n = lread();
auto a = aryread();
long cost = INF;
foreach(i; iota(-200, 200))
{
cost = min(cost, getcost(a, i));
}
cost.writeln();
}
auto getcost(long[] a, long change)
{
long cost;
foreach(e; a)
{
cost += (e - change) ^^ 2;
}
return cost;
}
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.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto md = readln.split.to!(int[]);
auto M = md[0];
auto D = md[1];
int r;
foreach (m; 0..M) {
++m;
foreach (d; 21..D) {
++d;
if (d%10 >= 2 && d/10 >= 2 && (d%10)*(d/10) == m) {
++r;
}
}
}
writeln(r);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
void main()
{
auto n = lread();
auto a = aryread();
long cnt;
while (func(a))
{
foreach (i; 0 .. n)
{
a[i] = a[i] / 2;
}
cnt += 1;
}
writeln(cnt);
}
auto func(long[] x)
{
long cnt;
foreach (i; 0 .. x.length)
{
if (x[i] % 2 == 0)
{
cnt += 1;
}
}
if (cnt == x.length)
{
return true;
}
else
{
return false;
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
auto S = readln.chomp.to!(dchar[]);
auto Q = readln.chomp.to!int;
dchar[] as, bs;
int f;
foreach (_; 0..Q) {
auto q = readln;
if (q[0] == '1') {
++f;
} else {
auto c = q[4].to!dchar;
if ((q[2] == '1' && f%2 == 0) || (q[2] == '2' && f%2 == 1)) {
as ~= c;
} else {
bs ~= c;
}
}
}
if (f%2 == 1) {
S.reverse();
bs.reverse();
writeln(bs ~ S ~ as);
} else {
as.reverse();
writeln(as ~ S ~ bs);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
void main(){
auto c=readln.chomp.to!char;
if(c=='a'||c=='i'||c=='u'||c=='e'||c=='o')writeln("vowel");
else writeln("consonant");
}
|
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;
long[Tuple!(long, long)] mem;
immutable long MOD = 10^^9 + 7;
long dp(long s, long x) {
Tuple!(long, long) t = tuple(s, x);
if (t in mem)
return mem[t];
if (s == 0)
return 1;
else if (s == 1) {
mem[t] = (dp(s/2, x/2) + dp((s-1)/2, (x-1)/2)) % MOD;
return mem[t];
}
mem[t] = (dp(s/2, x/2) + dp((s-1)/2, (x-1)/2) + dp((s-2)/2, x/2)) % MOD;
return mem[t];
}
void main() {
long N = readln.chomp.to!long;
dp(N, N).writeln;
}
|
D
|
void main()
{
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
int[] odd = new int[n];
foreach (i, x; a)
{
if (x & 1)
{
odd[i] = 1;
}
else
{
odd[i] = 2;
}
}
writeln(3 ^^ n - odd.reduce!((x, y) => x * y));
}
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;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
void main(){
auto Q=readln.split.to!(int[]),A=Q[0],B=Q[1],C=Q[2];
if(C>=A&&C<=B)writeln("Yes");
else writeln("No");
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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!int;
auto k = RD!int;
auto cnt = new long[](k);
foreach (i; 0..n)
{
auto a = RD!int-1;
++cnt[a];
}
long ans;
long used;
foreach (i; 0..k)
{
auto c = cnt[i] / 2;
ans += c * 2;
used += c;
cnt[i] -= c * 2;
}
auto remain = n - used * 2 + n % 2;
ans += remain / 2;
writeln(ans);
stdout.flush();
debug readln();
}
|
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() {
int h, w;
scan(h, w);
auto c = new int[][](10, 10);
auto d = new int[][](10, 10);
foreach (i ; 0 .. 10) {
c[i] = readln.split.to!(int[]);
d[i] = c[i].dup();
}
foreach (k ; 0 .. 10) {
foreach (i ; 0 .. 10) {
foreach (j ; 0 .. 10) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
long ans;
foreach (i ; 0 .. h) {
auto ai = readln.split.to!(int[]);
foreach (aij ; ai) {
if (aij != -1) ans += d[aij][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);
}
}
}
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
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
writeln((readln.chomp.to!int+1)/2);
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.bigint;
import std.typecons;
import std.algorithm;
import std.array;
void main() {
auto tmp = readln.split.to!(int[]);
auto N = tmp[0];
auto M = tmp[1];
writeln((M * 1900 + (N-M) * 100) * 2^^M);
}
|
D
|
void main() {
problem();
}
void problem() {
const N = scan!int;
const M = scan!int;
const A = scan!int(N);
string solve() {
const votesCount = A.sum();
int items;
foreach(a; A) if (a*4*M >= votesCount) items++;
return items >= M ? "Yes" : "No";
}
solve().writeln;
}
// ----------------------------------------------
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
// -----------------------------------------------
|
D
|
import std.stdio, std.string, std.conv;
void main(){
string str;
string[] s;
int q1, b, c1, c2, q2, a1, a2, t;
while ((str = readln()) != "0\n") {
s = split(str);
q1 = s[0].to!int;
b = s[1].to!int;
c1 = s[2].to!int;
c2 = s[3].to!int;
q2 = s[4].to!int;
foreach (i; 0..q2+1) {
a1 = q2 - i;
t = b - (a1 * c1);
if (t < 0) continue;
a2 = t / c2;
if (a1 + a2 >= q1) break;
}
if (a1 == 0) writeln("NA");
else writeln(a1, " ", a2);
}
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n, k; readV(n, k);
writeln(n%k == 0 ? 0 : 1);
}
|
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)); }
void main() {
/*
debug {
enum lim = 21;
for (int m = 1; m <= lim; m = m * 2 + 1) for (int n = 1; m * n <= lim; n = n * 2 + 1) {
int mx = -1;
int[] pms;
foreach (p; 0 .. 1 << (m * n)) {
auto sum = new int[][](m + 1, n + 1);
foreach (x; 0 .. m) foreach (y; 0 .. n) {
sum[x + 1][y + 1] = sum[x + 1][y] + sum[x][y + 1] - sum[x][y] + ((p >> (x * n + y)) & 1);
}
int cnt;
foreach (xa; 0 .. m) foreach (xb; xa + 1 .. m + 1) foreach (ya; 0 .. n) foreach (yb; ya + 1 .. n + 1) {
if ((sum[xa][ya] - sum[xa][yb] - sum[xb][ya] + sum[xb][yb]) & 1) {
++cnt;
}
}
if (chmax(mx, cnt)) {
pms = [];
}
if (mx == cnt) {
pms ~= p;
}
}
writeln(m, " ", n, ": ", mx, " ", pms.length);
{
const p = pms[0];
foreach (x; 0 .. m) {
foreach (y; 0 .. n) {
write((p >> (x * n + y)) & 1);
}
writeln();
}
}
stdout.flush;
}
}
//*/
try {
for (; ; ) {
const A = readInt();
const B = readInt();
foreach (x; 1 .. 2^^A) {
foreach (y; 1 .. 2^^B) {
write((bsf(x) + bsf(y) == max(A, B) - 1) ? 1 : 0);
}
writeln();
}
}
} catch (EOFException e) {
}
}
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int read() { return readln.chomp.to!int; }
int[] reads() { return readln.split.to!(int[]); }
int s(string n) {
int z;
foreach (c; n) z += c - '0';
return z;
}
void solve() {
string n = readln.chomp;
debug {
writeln(n, s(n));
}
writeln(n.to!int % s(n) == 0 ? "Yes" : "No");
}
void main() {
solve();
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 n = readln.chomp.to!int;
auto s = readln.chomp;
int m;
foreach (i; 1..n) {
if (s[i] == 'E') {
m++;
}
}
auto lm = m;
foreach (i; 1..n) {
if (s[i-1] == 'W') {
lm++;
}
if (s[i] == 'E') {
lm--;
}
m = min(m, lm);
}
m.writeln;
}
|
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[]);
foreach (ref c; S) c = 'x';
writeln(S);
}
|
D
|
unittest
{
assert( [ "axyb", "abyxb" ].parse.expand.solve == "axb" );
assert( [ "aa", "xayaz" ].parse.expand.solve == "aa" );
assert( [ "a", "z" ].parse.expand.solve == "" );
assert( [ "abracadabra", "avadakedavra" ].parse.expand.solve == "aaadara" );
}
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;
input.popFront;
auto t = input.front;
return tuple( s, t );
}
auto solve( string s, string t )
{
// dp[ si ][ ti ] = s[ 0 .. si ]とt[ 0 .. ti ]の最長共通部分列の長さ
auto dp = new long[][]( 1 + s.length, 1 + t.length );
for( auto si = 0L; si < s.length; si++ )
{
for( auto ti = 0L; ti < t.length; ti++ )
{
if( s[ si ] == t[ ti ] )
{
// 文字が一致する場合、長さを+1する
dp[ si + 1 ][ ti + 1 ] = dp[ si ][ ti ] + 1;
}
else
{
// 文字が一致しない場合、一つ前の長いほうを使う
dp[ si + 1 ][ ti + 1 ] = max( dp[ si ][ ti + 1 ], dp[ si + 1 ][ ti ] );
}
}
}
// dpを逆に辿って共通部分列を復元
auto ans = "";
auto si = s.length;
auto ti = t.length;
while( 0 < si && 0 < ti )
{
if( dp[ si ][ ti ] == dp[ si - 1 ][ ti ] )
{
si--;
continue;
}
if( dp[ si ][ ti ] == dp[ si ][ ti - 1 ] )
{
ti--;
continue;
}
si--;
ti--;
ans = s[ si ] ~ ans;
}
return ans;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
import core.bitop;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto as = readln.split.to!(long[]);
auto c = long.max;
foreach (x; 0..(1<<N)) {
if (popcnt(x) < K) continue;
long cc, last;
foreach (i, a; as) {
if (a > last) {
last = a;
} else if (x & (1<<i)) {
cc += last - a + 1;
last += 1;
}
}
c = min(c, cc);
}
writeln(c);
}
|
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.stdlib;
void main() {
auto N = readln.chomp.to!int;
long ans = 0;
foreach (i; 1..N+1) foreach (j; 1..N+1) foreach(k; 1..N+1) {
ans += gcd(gcd(i, j), k);
}
ans.writeln;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
import std.numeric;
void main()
{
auto n = readln.chomp.to!int;
auto res = new int[](n);
auto a = readln.chomp.split.to!(int[]);
foreach (i; 0..n-1) {
res[a[i]-1]++;
}
foreach (i; 0..n) {
res[i].writeln;
}
}
|
D
|
void main() {
auto a = ri;
writeln(a + a*a + a*a*a);
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
long rl() {
return readAs!long;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
int k; rd(k);
int n=1;
while((1<<n)<=k) n++;
struct E{int u, v, w;}
E[] edges;
int w=0;
for(int i=1; i<n; i++){
edges~=E(i, i+1, 0);
edges~=E(i, i+1, 1<<(n-i-1));
w+=(1<<(n-i-1));
}
if(w<(k-1)){
w=0;
for(int i=2; i<=n; i++){
if(k&(1<<(n-i))){
edges~=E(1, i, (1<<(n-1))+w);
w+=(1<<(n-i));
}
}
}
writeln(n, " ", edges.length);
foreach(e; edges){
writeln(e.u, " ", e.v, " ", e.w);
}
}
void rd(T...)(ref T x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x) e=l[i].to!(typeof(e));
}
|
D
|
import std.stdio,
std.string,
std.conv,
std.algorithm,
std.range,
std.math;
void main()
{
while(true){
auto ip = readln.split.to!(int[]), h = ip[0], w = ip[1];
if(h == 0 && w == 0) break;
else{
string W;
foreach(j; 0..w){
W ~= "#";
}
foreach(i; 0..h){
W.writeln;
}
writeln;
}
}
}
|
D
|
import std.algorithm,
std.string,
std.range,
std.stdio,
std.conv;
void chmax(T)(ref T a, ref T b) {
if (a < b) {
a = b;
}
}
void main() {
int N = readln.chomp.to!int;
int[] s = N.iota.map!(_ => readln.chomp.to!int).array;
bool[10010][110] dp;
dp[0][0] = true;
dp[0][s[0]] = true;
foreach (i; 1..N) {
foreach (j; 10010.iota) {
if (dp[i - 1][j]) {
dp[i][j+s[i]] = true;
dp[i][j] = true;
}
}
}
int ans;
foreach (i; 10010.iota) {
if (dp[N-1][i]) {
if (i % 10) {
chmax(ans, i);
}
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9+7;
long pow(long x, long n) {
long y = 1;
while (n) {
if (n%2 == 1) y = (y * x) % P;
x = x^^2 % P;
n /= 2;
}
return y;
}
long inv(long x)
{
return pow(x, P-2);
}
void main()
{
auto N = readln.chomp.to!long;
int[][] T;
T.length = N;
foreach (_; 0..N-1) {
auto ab = readln.split.to!(int[]);
auto A = ab[0]-1;
auto B = ab[1]-1;
T[A] ~= B;
T[B] ~= A;
}
long[][] TT;
TT.length = N;
long run(int i, int p) {
int s;
foreach (j; T[i]) if (p != j) {
auto n = run(j, i);
TT[i] ~= n;
s += n;
}
if (N-(s+1) > 0) TT[i] ~= N-(s+1);
return s+1;
}
run(0, -1);
long r, all = pow(2L, N);
long all_h = all * inv(2) % P, iall = inv(all);
foreach (i; 0..N) {
if (TT[i].length == 1) continue;
long rr = 1;
foreach (n; TT[i]) {
rr = (rr + (pow(2, n) - 1 + P) % P) % P;
}
r = (r + (all_h - rr + P) % P * iall % P) % P;
}
writeln(r);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.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;
int[][] ans;
foreach (ti; 0..t)
{
auto n = RD!int;
auto a = RDA!int;
debug writeln("n:", n);
int cnt;
int x = -1;
foreach (i; 0..n*2)
{
if (cnt == n-1) break;
if (a[i] % 2)
{
if (x == -1)
{
x = i;
}
else
{
ans ~= [x, i];
x = -1;
++cnt;
}
}
}
debug writeln("ti:", ti, " cnt:", cnt);
x = -1;
foreach (i; 0..n*2)
{
if (cnt == n-1) break;
debug writeln("i:", i);
if ((a[i] % 2) == 0)
{
if (x == -1)
{
x = i;
}
else
{
ans ~= [x, i];
x = -1;
++cnt;
}
}
}
debug writeln("ti:", ti, " cnt:", cnt);
}
debug writeln(ans);
foreach (e; ans)
{
writeln(e[0]+1, " ", e[1]+1);
}
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
void main()
{
writeln(reduce!"a + b"(0, readln.split.to!(int[])) >= 22 ? "bust" : "win");
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln((readln.split.to!(int[]).sum+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.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!string;
long cnt;
foreach (c; N)
{
cnt += [c].to!long;
}
writeln(N.to!long % cnt == 0 ? "Yes" : "No");
stdout.flush();
debug readln();
}
|
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, A, B;
scan(N, A, B);
long a = A * (N / (A + B));
long b = min((N % (A + B)), A);
writeln(a + b);
}
|
D
|
void main(){
string s1, s2;
s1 = readln().chomp().dup().reverse();
s2 = readln().chomp();
if(s1 == s2)writeln("YES");
else writeln("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, 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 int[](t);
foreach (ti; 0..t)
{
auto a = RD!int;
auto b = RD!int;
auto s = RD!string;
auto n = cast(int)s.length;
int l = -1, r;
foreach (i; 0..n)
{
if (s[i] == '1')
{
l = i;
break;
}
}
foreach_reverse (i; 0..n)
{
if (s[i] == '1')
{
r = i+1;
break;
}
}
if (l == -1) continue;
ans[ti] = a;
int cnt;
foreach (i; l..r)
{
debug writeln("ans:", ans[ti]);
if (s[i] == '0')
++cnt;
else
{
ans[ti] += min(a, b*cnt);
cnt = 0;
}
}
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
long P = 10^^9+7;
long[10^^5+50] F, RF;
void init()
{
F[0] = F[1] = 1;
foreach (i, ref x; F[2..$]) x = (F[i+1] * (i+2)) % P;
{
RF[$-1] = 1;
auto x = F[$-1];
auto k = P-2;
while (k) {
if (k%2 == 1) RF[$-1] = (RF[$-1] * x) % P;
x = x^^2 % P;
k /= 2;
}
}
foreach_reverse(i, ref x; RF[0..$-1]) x = (RF[i+1] * (i+1)) % P;
}
long[10^^5+1] MEMO, RN;
void main()
{
init();
auto N = readln.chomp.to!int;
auto AS = readln.split.to!(long[]);
foreach (i; 0..N) RN[i+1] = (RF[i+1] * F[i]) % P;
foreach (i; 0..N) MEMO[0] = (MEMO[0] + RN[i+1]) % P;
for (int i = 2, j = N; i <= N; ++i, --j) {
MEMO[i-1] = (MEMO[i-2] - RN[j] + RN[i]) % P;
if (MEMO[i-1] < 0) MEMO[i-1] += P;
}
long r;
foreach (i, a; AS) {
r = (r + ((a * F[N]) % P * MEMO[i]) % P) % P;
}
writeln(r);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto s = readln.chomp;
auto a = ["dream", "dreamer", "erase", "eraser"];
loop: while (!s.empty) {
foreach (ai; a) {
if (s.length >= ai.length && s[$-ai.length..$] == ai) {
s = s[0..$-ai.length];
continue loop;
}
}
writeln("NO");
return;
}
writeln("YES");
}
|
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 = RD;
auto b = RD;
auto s = RD!string;
if (b >= 0)
ans[ti] = (a+b) * n;
else
{
long cnt;
foreach (i; 1..n)
{
if (s[i] != s[i-1])
++cnt;
}
cnt = (cnt+1) / 2 + 1;
ans[ti] = a * n + b * cnt;
}
}
foreach (e; ans)
{
writeln(e);
}
stdout.flush;
debug readln;
}
|
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;
scan(A, B);
writeln((A < 10 && B < 10) ? A * B : -1);
}
|
D
|
void main()
{
long n, t;
rdVals(n, t);
long[] a = rdRow;
long[] amax = new long[n+1];
foreach_reverse (i, x; a)
{
amax[i] = max(amax[i+1], x);
}
long key, cnt;
foreach (i, x; a)
{
long diff = max(0, amax[i]-x);
if (key < diff)
{
key = diff;
cnt = 1;
}
else if (key == diff) ++cnt;
}
cnt.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.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
/+ dub.sdl:
name "C"
dependency "dunkelheit" version=">=0.9.0"
+/
import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;
int main() {
Scanner sc = new Scanner(stdin);
int n;
sc.read(n);
int[][] g = new int[][n];
foreach (i; 0..n-1) {
int a, b;
sc.read(a, b); a--; b--;
g[a] ~= b; g[b] ~= a;
}
if (g.count!"a.length>=3" >= 2) {
writeln("No");
return 0;
}
int[] leafs;
void dfs(int p, int b) {
bool haveCh = false;
foreach (d; g[p]) {
if (d == b) continue;
haveCh = true;
dfs(d, p);
}
if (!haveCh) leafs ~= p;
}
foreach (i; 0..n) {
if (i == n-1 || g[i].length >= 3) {
dfs(i, -1);
writeln("Yes");
writeln(leafs.length);
foreach (d; leafs) {
writeln(i+1, " ", d+1);
}
break;
}
}
return 0;
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;
struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
import core.exception : RangeError;
private T* _data;
private uint len, cap;
@property bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }
ref inout(T) opIndex(size_t i) inout {
version(assert) if (len <= i) throw new RangeError();
return _data[i];
}
ref inout(T) front() inout { return this[0]; }
ref inout(T) back() inout { return this[$-1]; }
void reserve(size_t newCap) {
import core.memory : GC;
import core.stdc.string : memcpy;
import std.conv : to;
if (newCap <= cap) return;
void* newData = GC.malloc(newCap * T.sizeof);
cap = newCap.to!uint;
if (len) memcpy(newData, _data, len * T.sizeof);
_data = cast(T*)(newData);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void clear() {
len = 0;
}
void insertBack(T item) {
import std.algorithm : max;
if (len == cap) reserve(max(cap * 2, MINCAP));
_data[len++] = item;
}
alias opOpAssign(string op : "~") = insertBack;
void removeBack() {
assert(!empty, "StackPayload.removeBack: Stack is empty");
len--;
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;
// import dkh.container.stackpayload;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
void read(Args...)(auto ref Args args) {
import std.exception;
static if (args.length != 0) {
enforce(readSingle(args[0]));
read(args[1..$]);
}
}
bool hasNext() {
return succ();
}
}
/* IMPORT /mnt/c/Users/yosupo/Programs/dunkelheit/source/dkh/foundation.d */
// module dkh.foundation;
static if (__VERSION__ <= 2070) {
/*
Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
Copyright: Andrei Alexandrescu 2008-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
import std.algorithm : reduce;
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
import std.typecons : tuple;
return reduce!fun(tuple(seed), r);
}
}
}
}
/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/
|
D
|
//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;
string N;
sc.scan(N);
char[] n = new char[3];
foreach (i; 0 .. 3)
n[i] = to!char(N[i]);
foreach (i; 0 .. 3) {
if (n[i] == '1')
n[i] = '9';
else
n[i] = '1';
}
writeln(n);
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
int n; readV(n);
int[] l; readA(n, l);
auto ml = l.reduce!max;
writeln(l.sum-ml > ml ? "Yes" : "No");
}
|
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 NAB=readln.split.map!(to!int).array;
auto Xs=readln.split.map!(to!long).array;
auto res=0L;
foreach(i;0..NAB[0]-1){
res+=min(NAB[1]*(Xs[i+1]-Xs[i]),NAB[2]);
}
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.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 S = RD!string;
auto T = S.idup;
long ans = 1;
auto last = [S[0]];
S.popFront;
while (!S.empty)
{
if (last.length == 2)
{
last = [S[0]]; S.popFront;
++ans;
}
else if (S[0] == last[0])
{
if (S.length == 1)
{
break;
}
last = S[0..2];
S = S[2..$];
++ans;
}
else
{
last = [S[0]]; S.popFront;
++ans;
}
}
long ans2 = 1;
last = [T[0], T[1]];
T = T[2..$];
while (!T.empty)
{
if (last.length == 2)
{
last = [T[0]]; T.popFront;
++ans2;
}
else if (T[0] == last[0])
{
if (T.length == 1)
{
break;
}
last = T[0..2];
T = T[2..$];
++ans2;
}
else
{
last = [T[0]]; T.popFront;
++ans2;
}
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;
void main() {
int N; readlnTo(N);
if (N == 1) "Hello World".writeln;
else {
int A, B;
readlnTo(A);
readlnTo(B);
(A + B).writeln;
}
}
// helpers
void readlnTo(T...)(ref T t) {
auto s = readln.split;
assert(s.length == t.length);
foreach(ref ti; t) {
ti = s[0].to!(typeof(ti));
s = s[1..$];
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
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[] uniq(int[] a) {
int[] ret;
foreach (x; a) {
if (ret.length > 0 && ret[$-1] == x) continue;
ret ~= x;
}
return ret;
}
int sign(int n) {
if (n == 0) return 0;
if (n > 0) return 1;
return -1;
}
int calc(int[] a) {
a = uniq(a);
int ans = 0;
int p = 0;
int[] buf;
while (p < a.length) {
switch (buf.length) {
case 0:
ans++;
buf ~= a[p++];
break;
case 1:
buf ~= a[p++];
break;
default:
if (sign(buf[$-1] - buf[$-2]) == sign(a[p] - buf[$-1])) {
buf ~= a[p++];
}
else {
buf = null;
}
break;
}
}
return ans;
}
void main() {
readint;
auto a = readints;
writeln(calc(a));
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int digits(int n) {
if (n == 0) return 0;
int x = 0;
while (n > 0) {
x += n % 10;
n /= 10;
}
return x;
}
int calc(int n) {
int ans = int.max;
for (int i = 1; i < n; i++) {
int a = i;
int b = n - a;
ans = min(ans, digits(a) + digits(b));
}
return ans;
}
void main() {
int n = readint;
auto ans = calc(n);
writeln(ans);
}
|
D
|
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
void main()
{
int n = to!int(chomp(readln()));
writeln(to!string((1+n)*n/2));
}
int[] stringText2IntArray(string text)
{
return map!(to!int)(split(readln())).array;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array;
void main() {
int n, m;
scan(n, m);
auto uf = WeightedUnionFind!(int)(n + 1);
bool ok = 1;
foreach (i ; 0 .. m) {
int l, r, d;
scan(l, r, d);
if (uf.same(l, r)) {
if (uf.diff(l, r) != d) {
ok = 0;
}
}
else {
uf.merge(l, r, d);
}
}
writeln(ok ? "Yes" : "No");
}
struct WeightedUnionFind(T) {
private {
int[] _parent, _rank;
T[] _diff;
}
this(int N) {
_parent = new int[](N);
_rank = new int[](N);
_diff = new T[](N);
foreach (i ; 0 .. N) {
_parent[i] = i;
}
}
int findRoot(int x) {
if (_parent[x] != x) {
int r = findRoot(_parent[x]);
_diff[x] += _diff[_parent[x]];
_parent[x] = findRoot(r);
}
return _parent[x];
}
bool same(int x, int y) {
return findRoot(x) == findRoot(y);
}
T weight(int x) {
findRoot(x);
return _diff[x];
}
// x と y が異なるグループに属すならinf(十分大きい値)を返す
T diff(int x, int y) {
return same(x, y) ? weight(y) - weight(x) : T.max;
}
import std.algorithm : swap;
// weight(y) = weight(x) + w
bool merge(int x, int y, T w) {
w += weight(x) - weight(y);
int u = findRoot(x);
int v = findRoot(y);
// x と y が既に同じグループであるなら距離の更新はしない
if (u == v) return false;
if (_rank[u] < _rank[v]) {
swap(u, v);
w = -w;
}
if (_rank[u] == _rank[v]) {
_rank[u]++;
}
_parent[v] = u;
_diff[v] = w;
return true;
}
}
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.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;
auto AB = new long[2][](N);
foreach (i; 0..N)
{
AB[i] = [RD, RD];
}
long ans;
foreach_reverse (i; 0..N)
{
auto a = AB[i][0] + ans;
auto b = AB[i][1];
auto x = b * ((a / b) + (a % b != 0 ? 1 : 0));
ans += x - a;
}
writeln(ans);
stdout.flush();
debug readln();
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
long h, w;
scan(h, w);
auto s = new string[](h);
foreach (i; 0 .. h)
{
s[i] = sread();
}
// writeln(s);
auto ans = new long[][](h, w);
// writeln(ans);
long[] moves = [-1, 0, 1];
foreach (i; 0 .. h)
{
foreach (j; 0 .. w)
{
//8方向見て#のとき+1する
foreach (y; moves)
{
foreach (x; moves)
{
if ((i + y < 0) || (i + y > h - 1) || (j + x < 0) || (j + x > w - 1))
{
continue;
}
if (s[i + y][j + x] == '#')
{
ans[i][j] += 1;
}
}
}
}
}
// writeln(ans);
foreach (i; 0 .. h)
{
foreach (j; 0 .. w)
{
if (s[i][j] == '.')
{
write(ans[i][j]);
}
else
{
write('#');
}
}
writeln();
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;
import std.range;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xs = readints();
int a = xs[0], b = xs[1], c = xs[2], d = xs[3];
int start = max(a, c);
int end = min(b, d);
writeln(max(0, end - start));
}
|
D
|
import std.stdio, std.conv, std.string, std.algorithm, std.range;
void main() {
auto N = readln.split[0].to!int;
(N ^^ 3).writeln;
}
|
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;
}
}
enum MOD = (10 ^^ 9) + 7;
void main()
{
long N, M;
scan(N, M);
if (M <= N * 2)
{
writeln(M / 2);
return;
}
writeln(N + ((M - N * 2) / 4));
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
void main() {
string[] inputs = split(readln());
int a = to!int(inputs[0]);
int b = to!int(inputs[1]);
int c = to!int(inputs[2]);
min(a + b, b + c, c + a).writeln;
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons;
void main()
{
auto R = readln.chomp.to!int;
if (R < 1200) {
writeln("ABC");
} else if (R < 2800) {
writeln("ARC");
} else {
writeln("AGC");
}
}
|
D
|
void main()
{
long[] tmp = rdRow;
long n = tmp[0], m = tmp[1];
string s = rdStr;
string t = rdStr;
long g = gcd(n, m);
long l = n * m / g;
long sl = n / g, tl = m / g;
foreach (i; 0 .. g)
{
if (s[sl*i] != t[tl*i])
{
writeln(-1);
return;
}
}
l.writeln;
}
T rdElem(T = long)()
{
//import std.stdio : readln;
//import std.string : chomp;
//import std.conv : to;
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
dchar[] rdDchar()
{
//import std.conv : to;
return rdStr.to!(dchar[]);
}
T[] rdRow(T = long)()
{
//import std.stdio : readln;
//import std.array : split;
//import std.conv : to;
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
//import std.range : iota;
//import std.algorithm : map;
//import std.array : array;
return iota(col).map!(x => rdRow!T).array;
}
void wrMat(T = long)(T[][] mat)
{
//import std.stdio : write, writeln;
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "l ", long, "r");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto n = lread();
auto a = aryread();
long ans = a[0];
foreach (e; a)
{
ans = gcd(ans, e);
}
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.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
writeln(48 - readln.chomp.to!int);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int k;
scan(k);
auto ans = (k / 2) * ((k + 1) / 2);
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);
}
}
}
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,std.string;
import std.conv,std.math;
void main(){
int [] n = readln.chomp.split.to!(int[]);
if((abs(n[0]-n[1])<=n[3] &&abs(n[1]-n[2])<=n[3])
||abs(n[0]-n[2])<=n[3]){
writeln("Yes");
}
else{
writeln("No");
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
std.typecons, std.container, std.format;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
string S, T;
scan(S, T);
writeln(T, S);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
void main()
{
auto n = lread();
auto a = aryread();
// writeln(a);
long cost;
auto new_a = new long[](n + 2);
foreach (i; 0 .. n)
{
new_a[i + 1] = a[i];
}
// writeln(new_a);
foreach (i; 0 .. (new_a.length - 1))
{
cost += abs(new_a[i] - new_a[i + 1]);
}
// writeln(cost);
foreach (i; 1 .. (new_a.length) - 1)
{
// writeln(i - 1, i, i + 1);
writeln(cost - (abs(new_a[i - 1] - new_a[i]) + abs(new_a[i] - new_a[i + 1])) + abs(
new_a[i - 1] - new_a[i + 1]));
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string;
int[10^^5] BS;
bool[10^^5] HS;
void main()
{
auto n = readln.chomp.to!int;
foreach (i; 0..n) {
BS[i] = readln.chomp.to!int - 1;
}
int p, cnt;
for (;;) {
++cnt;
HS[p] = true;
p = BS[p];
if (p == 1) {
writeln(cnt);
return;
}
if (HS[p]) {
writeln(-1);
return;
}
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;
immutable long mod = 10^^9 + 7;
immutable long inf = mod;
void main(){
long m, n;
readVars(m, n);
long ans = modpow(m, n, mod);
writeln(ans);
}
T modpow(T)(T x, T y, T mod){
return y ? modpow(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1;
}
void readVars(T...)(auto ref T args){
auto line = readln.split;
foreach(ref arg ; args){
arg = line.front.to!(typeof(arg));
line.popFront;
}
if(!line.empty){
throw new Exception("args num < input num");
}
}
|
D
|
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;
enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int a, b, c, x, y;
scan(a, b, c, x, y);
long ans = infl;
foreach (i ; 0 .. max(x, y) + 1) {
long t = 1L * i * 2 * c;
t += 1L * max(0, x - i) * a;
t += 1L * max(0, y - i) * b;
chmin(ans, t);
}
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);
}
}
}
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;
void main() {
int N = to!int(chomp(readln()));
int x = N * 800;
int y = (N / 15) * 200;
(x - y).writeln;
}
|
D
|
void main() {
int n = readln.chomp.to!int;
int[] a = readln.split.to!(int[]);
int cnt, tmp;
foreach (i; 0 .. n) {
if (i == n - 1 && a[i] == n) {
tmp = a[i];
a[i] = a[i-1];
a[i-1] = tmp;
++cnt;
} else if (a[i] == i + 1) {
tmp = a[i];
a[i] = a[i+1];
a[i+1] = tmp;
++cnt;
}
}
cnt.writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
void main() {
"square1001".writeln;
}
// ===================================
import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;
T readAs(T)() if (isBasicType!T) {
return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
return readln.split.to!T;
}
T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
res[i] = readAs!(T[]);
}
return res;
}
T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
auto res = new T[][](height, width);
foreach(i; 0..height) {
auto s = rs;
foreach(j; 0..width) res[i][j] = s[j].to!T;
}
return res;
}
int ri() {
return readAs!int;
}
double rd() {
return readAs!double;
}
string rs() {
return readln.chomp;
}
|
D
|
import std.stdio, std.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;
immutable long MAX = 10^^5 * 2;
immutable long MOD = 10^^9 + 7;
long[] F;
long[] G;
void main() {
F = new long[](MAX);
G = new long[](MAX);
F[0] = F[1] = 1;
foreach (i; 2..MAX) F[i] = F[i-1] * i % MOD;
foreach (i; 0..MAX) G[i] = powmod(F[i], MOD-2, MOD);
auto s = readln.split.map!(to!long);
auto N = s[0].to!int;
auto M = s[1];
if (M == 1) {
writeln(1);
return;
}
long ans = 1;
int[long] P;
for (long i = 2; i * i <= M; ++i) {
while (M % i == 0) P[i] += 1, M /= i;
}
if (M > 1) P[M] += 1;
foreach (p; P.keys) {
ans = ans * comb(N + P[p] - 1, P[p]) % MOD;
}
ans.writeln;
}
long powmod(long a, long x, long m) {
a %= m;
long ret = 1;
while (x) {
if (x % 2) ret = ret * a % m;
a = a * a % m;
x /= 2;
}
return ret;
}
long comb(long n, long k) {
return F[n] * G[n-k] % MOD * G[k] % MOD;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void minAssign(T, U = T)(ref T dst, U src)
{
dst = cast(T) min(dst, src);
}
enum MOD = 10 ^^ 9 + 7;
void main()
{
long a = lread();
long b = lread();
long c = lread();
long d = lread();
long e = lread();
long k = lread();
writeln((k < e - a) ? ":(" : "Yay!");
}
|
D
|
/+ dub.sdl:
name "E"
dependency "dcomp" version=">=0.6.0"
+/
import std.stdio, std.range, std.algorithm, std.conv;
// import dcomp.scanner;
// import dcomp.algorithm;
int n;
long[] d, v;
bool check(long fi) {
v[] = d[];
foreach_reverse(i; 1..n) {
long x = v[i];
if (fi < x) return false;
long u = (fi-x) / 10;
x += u * 10;
v[i-1] -= u;
fi = x;
}
return v[0] <= fi;
}
int main() {
auto sc = new Scanner(stdin);
string s;
sc.read(s);
d = s.map!(x => (x - '0').to!long).array;
n = d.length.to!int;
v = new long[n];
// writeln(d, " ", d.length);
/* foreach (i; 0..100) {
writeln(i, " ", check(i));
}*/
writeln((binSearch!(i => check(i))(-1L, 10L^^15) + 8) / 9);
return 0;
}
/* 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;
}
string[] buf;
private bool succ() {
while (!buf.length) {
if (f.eof) return false;
buf = f.readln.split;
}
return true;
}
private bool readSingle(T)(ref T x) {
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
//string or char[10] etc
x = buf.front;
buf.popFront;
} else {
static if (isStaticArray!T) {
//static
assert(buf.length == T.length);
}
x = buf.map!(to!E).array;
buf.length = 0;
}
} else {
x = buf.front.to!T;
buf.popFront;
}
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]));
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/algorithm.d */
// module dcomp.algorithm;
import std.range.primitives;
import std.traits : isFloatingPoint, isIntegral;
//[0,0,0,...,1,1,1]で、初めて1となる場所を探す。pred(l) == 0, pred(r) == 1と仮定
T binSearch(alias pred, T)(T l, T r) if (isIntegral!T) {
while (r-l > 1) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
T binSearch(alias pred, T)(T l, T r, int cnt = 60) if (isFloatingPoint!T) {
foreach (i; 0..cnt) {
T md = (l+r)/2;
if (!pred(md)) l = md;
else r = md;
}
return r;
}
Rotator!Range rotator(Range)(Range r) {
return Rotator!Range(r);
}
struct Rotator(Range)
if (isForwardRange!Range && hasLength!Range) {
size_t cnt;
Range start, now;
this(Range r) {
cnt = 0;
start = r.save;
now = r.save;
}
this(this) {
start = start.save;
now = now.save;
}
@property bool empty() {
return now.empty;
}
@property auto front() {
assert(!now.empty);
import std.range : take, chain;
return chain(now, start.take(cnt));
}
@property Rotator!Range save() {
return this;
}
void popFront() {
cnt++;
now.popFront;
}
}
E minimum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? a : b)(seed, range);
}
ElementType!Range minimum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return minimum!pred(range, e);
}
E maximum(alias pred = "a < b", Range, E = ElementType!Range)(Range range, E seed)
if (isInputRange!Range && !isInfinite!Range) {
import std.algorithm, std.functional;
return reduce!((a, b) => binaryFun!pred(a, b) ? b : a)(seed, range);
}
ElementType!Range maximum(alias pred = "a < b", Range)(Range range) {
assert(!range.empty, "range must not empty");
auto e = range.front; range.popFront;
return maximum!pred(range, e);
}
unittest {
assert(minimum([2, 1, 3]) == 1);
assert(minimum!"a > b"([2, 1, 3]) == 3);
assert(minimum([2, 1, 3], -1) == -1);
assert(minimum!"a > b"([2, 1, 3], 100) == 100);
assert(maximum([2, 1, 3]) == 3);
assert(maximum!"a > b"([2, 1, 3]) == 1);
assert(maximum([2, 1, 3], 100) == 100);
assert(maximum!"a > b"([2, 1, 3], -1) == -1);
}
bool[ElementType!Range] toMap(Range)(Range r) {
import std.algorithm : each;
bool[ElementType!Range] res;
r.each!(a => res[a] = true);
return res;
}
|
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;
double[] cross(double[] a, double[] b) {
return [a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]];
}
double dot(double[] a, double[] b) {
return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
}
double norm(double[] a) {
return sqrt(dot(a,a));
}
double[] readv() {
auto s = readln().strip().split();
return [to!double(s[0]),to!double(s[1]), to!double(s[2])];
}
void main() {
auto a = readv();
auto b = readv();
auto A = readv();
auto B = readv();
auto C = readv();
A[] -= C[];
B[] -= C[];
a[] -= C[];
b[] -= C[];
auto n = cross(A,B);
auto k = -dot(n,b)/(dot(n,a)-dot(n,b));
if(k < 0 || 1 < k) {
writeln("HIT");
} else {
auto c = a.dup;
c[] *= k;
c[] += b[]*(1-k);
auto S1 = norm(cross(A,B));
auto A_ = A.dup;
auto B_ = B.dup;
A_[] -= c[];
B_[] -= c[];
auto S2 = norm(cross(A,c))+norm(cross(B,c))+norm(cross(A_,B_));
double eps = 1e-5;
if(abs(S1-S2) < eps)
writeln("MISS");
else
writeln("HIT");
}
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.