code
stringlengths 4
1.01M
| language
stringclasses 2
values |
|---|---|
/* imports all std modules {{{*/
import
std.algorithm,
std.array,
std.ascii,
std.base64,
std.bigint,
std.bitmanip,
std.compiler,
std.complex,
std.concurrency,
std.container,
std.conv,
std.csv,
std.datetime,
std.demangle,
std.encoding,
std.exception,
std.file,
std.format,
std.functional,
std.getopt,
std.json,
std.math,
std.mathspecial,
std.meta,
std.mmfile,
std.net.curl,
std.net.isemail,
std.numeric,
std.parallelism,
std.path,
std.process,
std.random,
std.range,
std.regex,
std.signals,
std.socket,
std.stdint,
std.stdio,
std.string,
std.system,
std.traits,
std.typecons,
std.uni,
std.uri,
std.utf,
std.uuid,
std.variant,
std.zip,
std.zlib;
/*}}}*/
/+---test
SAT
---+/
/+---test
SUN
---+/
/+---test
TUE
---+/
void main(string[] args) {
const S = readln.chomp;
auto days = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
auto x = days.countUntil!(x => x == S);
(7 - x).writeln;
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto s = readln.strip;
writeln( solve( s ) );
}
auto solve( in string s )
{
auto n = s.to!int;
auto sn = 0;
foreach( c; s )
{
sn += [ c ].to!int;
}
return ( n % sn == 0 ) ? "Yes" : "No";
}
unittest
{
assert( solve( "12" ) == "Yes" );
assert( solve( "101" ) == "No" );
assert( solve( "999999999" ) == "Yes" );
}
|
D
|
// cheese-cracker [2022-02-12]
void solve(){
int n = scan!int;
auto arr = scanArray;
long summ = 0;
for(int i = 1; i <= n; ++i){
summ += i * (n + 1 - i);
}
for(int i = 1; i <= n; ++i){
if(arr[i-1] == 0){
summ += i * (n + 1 - i);
}
}
writeln(summ);
}
void main(){
long tests = scan; // Toggle!
while(tests--) solve;
}
/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
|
D
|
import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
/*
部分文字列 文字列の一致
sのi文字目までとtのj文字目まででやった場合の答えをxs[i][j]とする
s[i]とt[j]が一致する場合、
xs[i][j] = xs[i - 1][j - 1] + 1
そうでない場合、
xs[i][j] = max(xs[i - 1][j], xs[i][j - 1]))
ただし添字が負になる項は 0
求めるものはxs[n][m]である
上記を求めたあと、文字列は以下のようにして作る
string solve(int i, int j){
if(s[i] == t[j]){
if(i == 0 || j == 0) return "" ~ s[i];
else return solve(i - 1, j - 1) ~ s[i];
}
else{
if(i == 0 && j == 0) return "";
if(i == 0) return solve(i, j - 1);
if(j == 0) return solve(i - 1, j);
if(xs[i - 1][j] > xs[i][j - 1]) return solve(i - 1, j);
else return solve(i, j - 1);
}
}
// メモ化再帰のほうがよかったかも
*/
void main(){
string s = readln.chomp;
string t = readln.chomp;
long n = s.length;
long m = t.length;
int[][] xs;
foreach(i; 0 .. n){
xs ~= new int[](m);
foreach(j; 0 .. m){
debug writeln("i:", i, " j: ", j, " xs:", xs);
if(s[i] == t[j]){
if(i == 0 || j == 0) xs[i][j] = 1;
else xs[i][j] = xs[i - 1][j - 1] + 1;
}
else{
if(i == 0 && j == 0) xs[i][j] = 0;
else if(i == 0) xs[i][j] = xs[i][j - 1];
else if(j == 0) xs[i][j] = xs[i - 1][j];
else xs[i][j] = max(xs[i - 1][j], xs[i][j - 1]);
}
}
}
string solve(long i, long j){
if(s[i] == t[j]){
if(i == 0 || j == 0) return "" ~ s[i];
else return solve(i - 1, j - 1) ~ s[i];
}
else{
if(i == 0 && j == 0) return "";
if(i == 0) return solve(i, j - 1);
if(j == 0) return solve(i - 1, j);
if(xs[i - 1][j] > xs[i][j - 1]) return solve(i - 1, j);
else return solve(i, j - 1);
}
}
solve(n - 1, m - 1).writeln;
}
|
D
|
// Vicfred
// https://atcoder.jp/contests/abc162/tasks/abc162_c
// math
import std.conv;
import std.stdio;
import std.string;
long gcd(long a, long b) {
if(b == 0)
return a;
return gcd(b, a%b);
}
long gcd(long a, long b, long c) {
return gcd(a,gcd(b,c));
}
void main() {
int K = readln.chomp.to!int;
long sum = 0;
for(int i = 1; i <= K; i++)
for(int j = 1; j <= K; j++)
for(int k = 1; k <= K; k++)
sum += gcd(i,j,k);
sum.writeln;
}
|
D
|
import std.stdio, std.string, std.algorithm, std.array;
void main()
{
int t;
scanf("%d", &t);
getchar();
foreach(i; 0..t){
long a,b,c,m;
scanf("%lld %lld %lld %lld", &a, &b, &c, &m);
auto top_max = a + b + c - 3;
auto max_v = max(a,b,c);
auto min_v = min(a,b,c);
auto mid_v = a + b + c - max_v - min_v;
if ((m > top_max) || (max_v > (min_v + mid_v + 1 + m)))
writeln("NO");
else
writeln("YES");
}
}
|
D
|
import std;
// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on
void main()
{
long N = lread();
auto A = aryread();
A[] -= 1;
// writeln(A);
auto ans = new long[](N);
foreach_reverse (i; 0 .. N - 1)
{
ans[A[i]]++;
}
foreach (i; 0 .. N)
{
ans[i].writeln();
}
}
|
D
|
//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
final class InputReader {
private:
ubyte[] p, buffer;
bool eof;
bool rawRead () {
if (eof) {
return false;
}
p = stdin.rawRead (buffer);
if (p.empty) {
eof = true;
return false;
}
return true;
}
ubyte nextByte(bool check) () {
static if (check) {
if (p.empty) {
if (!rawRead ()) {
return 0;
}
}
}
auto r = p.front;
p.popFront ();
return r;
}
public:
this () {
buffer = uninitializedArray!(ubyte[])(16<<20);
}
bool seekByte (in ubyte lo) {
while (true) {
p = p.find! (c => c >= lo);
if (!p.empty) {
return false;
}
if (!rawRead ()) {
return true;
}
}
}
template next(T) if (isSigned!T) {
T next () {
if (seekByte (45)) {
return 0;
}
T res;
ubyte b = nextByte!false ();
if (b == 45) {
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 - (b - 48);
}
} else {
res = b - 48;
while (true) {
b = nextByte!true ();
if (b < 48 || b >= 58) {
return res;
}
res = res * 10 + (b - 48);
}
}
}
}
template next(T) if (isUnsigned!T) {
T next () {
if (seekByte (48)) {
return 0;
}
T res = nextByte!false () - 48;
while (true) {
ubyte b = nextByte!true ();
if (b < 48 || b >= 58) {
break;
}
res = res * 10 + (b - 48);
}
return res;
}
}
T[] nextA(T) (in int n) {
auto a = uninitializedArray!(T[]) (n);
foreach (i; 0 .. n) {
a[i] = next!T;
}
return a;
}
}
void main() {
auto r = new InputReader ();
immutable nt = r.next!uint ();
foreach (tid; 0 .. nt) {
const int n = r.next!uint ();
const int m = r.next!uint ();
writeln ((n % m) == 0 ? "YES" : "NO");
}
}
|
D
|
import std.stdio;
import std.string;
void main()
{
auto n = readln;
auto s = readln.strip;
writeln( solve( s ) );
}
int solve( in string s )
{
auto lc = new int[ s.length ];
lc[ 0 ] = 0;
foreach( i; 1 .. s.length )
{
lc[ i ] = lc[ i - 1 ] + ( s[ i - 1 ] == 'E' ? 0 : 1 );
}
auto rc = new int[ s.length ];
rc[ $-1 ] = 0;
foreach_reverse( i; 0 .. s.length-1 )
{
rc[ i ] = rc[ i + 1 ] + ( s[ i + 1 ] == 'W' ? 0 : 1 );
}
auto result = lc[ 0 ] + rc[ 0 ];
foreach( i; 1 .. s.length )
{
auto lrc = lc[ i ] + rc[ i ];
if( lrc < result ) result = lrc;
}
return result;
}
unittest
{
assert( solve( "WEEWW" ) == 1 );
assert( solve( "WEWEWEEEWWWE" ) == 4 );
assert( solve( "WWWWWEEE" ) == 3 );
}
|
D
|
import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container;
import std.range;
import core.stdc.stdlib;
import std.math;
void main() {
bool[int] kuku_answers;
foreach(i; 1..10) {
foreach(o; 1..10) {
kuku_answers[i*o] = true;
}
}
auto N = readln.chomp.to!int;
writeln(N in kuku_answers ? "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 abc=readln.split.array;
if(abc[0][$-1]==abc[1][0] && abc[1][$-1]==abc[2][0])writeln("YES");
else writeln("NO");
}
|
D
|
void main(){
int[] n = _scanln();
(n[0]-n[1]+1).writeln();
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;
// 1要素のみの入力
T _scan(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] _scanln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split()){
ln ~= elm.to!T();
}
return ln;
}
|
D
|
import std;
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(){
long a,b,c,d;
scan(a,b,c,d);
while(true){
c-=b;
if(c<=0){
"Yes".writeln;
break;
}
a-=d;
if(a<=0){
"No".writeln;
break;
}
}
}
|
D
|
// import chie template :) {{{
import std;
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
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;
}
}
// }}}
void main() {
auto cin = new Scanner;
string s;
cin.scan(s);
writeln(canFind(s, '7') ? "Yes" : "No");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
void main() {
auto N = readln.chomp.to!int, K = readln.chomp.to!int;
int res = 1;
foreach(i; 0..N) res = (res * 2 < res + K ? res * 2 : res + K);
writeln(res);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias route = Tuple!(long, "From", long, "To");
long bignum = 1_000_000_007;
auto dp = new long[](100_100);
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long n = lread();
auto blackboard = aryread();
if(blackboard.sum % 2)
writeln("NO");
else
writeln("YES");
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
string s = readln.chomp;
ulong k = readln.chomp.to!ulong;
foreach(i, c; s) {
if (c != '1') {
if (i < k) {
s[i].writeln;
return;
}
}
}
1.writeln;
}
|
D
|
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto s = readln.strip;
writeln( solve( s ) );
}
auto solve( in string s )
{
auto r = 0;
foreach( c; s )
{
if( c == '+' ) r++;
else r--;
}
return r;
}
unittest
{
assert( solve( "+-++" ) == 2 );
assert( solve( "-+--" ) == -2 );
assert( solve( "----" ) == -4 );
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
void main() {
auto ip = readln.split.to!(int[]), X = ip[0], Y = ip[1], Z = ip[2];
auto a = X - Z, b = Z + Y;
writeln(a / b);
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }
void main()
{
auto t = RD!int;
auto ans = new long[](t);
foreach (ti; 0..t)
{
auto n = RD!int;
auto k = RD!int;
auto a = RDA(-1);
auto b = RDA(-1);
auto need = new bool[](n);
foreach (e; b)
{
need[e] = true;
}
auto pos = new int[](n);
auto link = new int[][](n);
foreach (int i; 0..n)
{
pos[a[i]] = i;
link[i] = [i-1, i+1];
}
long tmp = 1;
foreach (e; b)
{
auto p = pos[e];
auto l = link[p][0];
auto r = link[p][1];
int cnt;
if (l != -1)
{
if (!need[a[l]])
++cnt;
link[l][1] = r;
}
if (r != n)
{
if (!need[a[r]])
++cnt;
link[r][0] = l;
}
tmp.modm(cnt);
need[e] = false;
}
ans[ti] = tmp;
}
foreach (e; ans)
writeln(e);
stdout.flush;
debug readln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto nab = readints;
int n = nab[0], a = nab[1], b = nab[2];
int xmax = min(a, b);
int xmin = max(0, a + b - n);
writeln(xmax, " ", xmin);
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
struct Input
{
int a,b;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto ab = readln().strip().split();
a = ab[0].to!int;
b = ab[1].to!int;
}
}
struct Output
{
}
auto main2(Input* input)
{
if (1 <= input.a && input.a <= 9 && 1 <= input.b && input.b <= 9)
return input.a * input.b;
return -1;
}
unittest { writeln("begin unittest"); }
unittest // example1
{
string example =
`2 5`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 10);
}
unittest // example2
{
string example =
`5 10`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == -1);
}
unittest // example3
{
string example =
`9 9`;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
writeln(result);
assert(result == 81);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
void main()
{
Input input = void;
parseInput(input, stdin);
auto result = main2(&input);
writeln(result);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");
T lread(T = long)()
{
return readln.chomp.to!T();
}
T[] aryread(T = long)()
{
return readln.split.to!(T[])();
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
void main()
{
long a, b, c;
scan(a, b, c);
auto d = max(10 * a + b + c, a + 10 * b + c, a + b + 10 * c);
d.writeln();
}
|
D
|
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void main()
{
dchar[] n; readV(n);
foreach (ref ni; n)
if (ni == '1') ni = '9';
else if (ni == '9') ni = '1';
writeln(n);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias 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 main()
{
long n = lread();
long[] l = aryread();
long max_side = 0;
long sum_side = 0;
foreach (e; l)
{
max_side = max(e, max_side);
sum_side += e;
}
(max_side < sum_side - max_side ? "Yes" : "No").writeln();
}
|
D
|
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional;
void main()
{
auto nab = readln.split.to!(int[]);
auto a = nab[1];
auto b = nab[2];
auto xs = readln.split.to!(long[]);
long ret, pos = xs[0];
foreach (x; xs[1..$]) {
auto d = (x - pos) * a;
if (d > b) {
ret += b;
pos = x;
} else {
ret += d;
pos = x;
}
}
writeln(ret);
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
auto S = sread();
auto T = sread();
auto s = (cast(char[])(cast(ubyte[]) S.dup).sort().array).text;
auto t = (cast(char[])(cast(ubyte[]) T.dup).sort!"b<a"().array).text;
dprint(s,t);
writeln(s < t ? "Yes" : "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[] 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()
{
auto S = sread();
auto prev = "";
long ans;
long i;
while (i < S.length)
{
long j = 1;
while (i + j < S.length && S[i .. i + j] == prev)
j++;
if (i + j != S.length || S[i .. i + j] != prev)
ans++;
prev = S[i .. i + j];
i += j;
// writeln(prev, i);
}
writeln(ans);
}
|
D
|
import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons;
T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
void main() {
auto size = readln.chomp.to!int;
auto lucky = cast(byte[])readln.chomp;
lucky[] -= '0';
void solve() {
int count;
bool[byte] ava;
foreach(a; lucky) ava[a] = true;
foreach(x; ava.keys) foreach(y; ava.keys) foreach(z; ava.keys) {
auto after = lucky.findSplitAfter([x]);
if (!after[1].canFind(y)) continue;
after = after[1].findSplitAfter([y]);
if (!after[1].canFind(z)) continue;
count++;
}
writeln(count);
}
solve();
}
|
D
|
import std.stdio;
import std.stdio;
import std.algorithm;
import std.range;
import std.functional;
import std.conv;
import std.string;
import std.math;
import core.bitop;
ulong diff(ulong a, ulong b) {
ulong res = 0;
foreach (bit; 0 .. 64) {
if ((a & (1UL << bit)) != (b & (1UL << bit))) {
res++;
}
}
return res;
}
ulong calc(ulong a) {
if ((a & (a - 1)) == 0) {
return a | (a - 1);
}
else {
ulong highestBit = 1UL << bsr(a);
return calc(a - highestBit) + calc(highestBit);
}
}
void main() {
// ulong res = 0;
// foreach (i; 0 .. 1000) {
// res += diff(i, i + 1);
// write("f(", (i + 1).to!string.padLeft(' ', 10), ") = ", res.to!string(2).padLeft('0', 10));
// writeln("; ", calc(i + 1).to!string(2).padLeft('0', 10));
// }
int tt = readln().chomp.to!int;
outer: foreach (t; 0 .. tt) {
ulong n = readln().chomp.to!ulong;
// writeln(n.to!string(2).padLeft('0', 64));
writeln(calc(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 main()
{
string s; readV(s);
writeln(s <= "2019/04/30" ? "Heisei" : "TBD");
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
import std.string;
void main()
{
while (1) {
string[] input = split(readln());
int h = to!(int)(input[0]);
int w = to!(int)(input[1]);
if (h == 0 && w == 0) break;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (j != 0 && i != 0 && i != h - 1 && j != w - 1) {
write(".");
} else {
write("#");
}
}
write("\n");
}
write("\n");
}
}
|
D
|
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
std.random, std.typecons, std.container;
ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y", long, "z");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
void main()
{
auto h = lread();
auto w = lread();
auto n = lread();
if(h < w)
swap(h, w);
// 必ずh >= w
writeln((n + h - 1) / h);
}
void scan(TList...)(ref TList Args)
{
auto line = readln.split();
foreach (i, T; TList)
{
T val = line[i].to!(T);
Args[i] = val;
}
}
|
D
|
void main() {
int[] tmp = readln.split.to!(int[]);
int a = tmp[0], b = tmp[1], c = tmp[2], d = tmp[3];
int[] time = new int[101];
++time[a], --time[b], ++time[c], --time[d];
foreach (i; 1 .. 101) {
time[i] += time[i-1];
}
time.count(2).writeln;
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
void main() {
auto abc = readints;
int a = abc[0], b = abc[1], c = abc[2];
writeln((a + b + c) - max(a, b, c));
}
|
D
|
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;
void main() {
const R = readln.chomp.to!int;
writeln(R < 1200 ? "ABC" : R < 2800 ? "ARC" : "AGC");
}
|
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;
T gcd(T)(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
T lcm(T)(T a, T b) {
return a * (b / gcd(a, b));
}
long calc(string s, string t) {
int slen = cast(int) s.length;
int tlen = cast(int) t.length;
long l = cast(long) lcm(s.length, t.length);
char[long] d;
for (int i = 0; i < s.length; i++) {
long p = i * (l / slen);
d[p] = s[i];
}
for (int i = 0; i < t.length; i++) {
long p = i * (l / tlen);
if (p in d) {
if (d[p] != t[i]) return -1;
}
}
return l;
}
void main() {
read!string;
auto s = read!string;
auto t = read!string;
writeln(calc(s, t));
}
|
D
|
import std.stdio, std.string, std.conv;
void main()
{
auto M = readln.chomp.to!int;
writeln(48 - M);
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
auto go (string s, string c)
{
char [] stack = "**".dup;
foreach (ref e; s.filter !(e => c.canFind (e)))
{
stack.assumeSafeAppend ();
stack ~= e;
if (stack[$ - 2..$] == c)
{
stack.popBackN (2);
}
}
return stack.length.to !(int) - 2;
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto s = readln.strip;
writeln ((s.length - s.go ("()") - s.go ("[]")) / 2);
}
}
|
D
|
import std.stdio;
import std.string;
import std.conv : to;
import std.algorithm : fill;
import std.math : sqrt;
void main() {
int input;
immutable limitList = 200000;
bool[] listNumbers = new bool[](limitList);
int[] listPrimeNumbers;
listNumbers.fill(true);
foreach (i; 2..limitList.to!double.sqrt.to!int) {
if (listNumbers[i]) {
for (int j = i*2; j < limitList; j += i) listNumbers[j] = false;
}
}
foreach (i; 2..listNumbers.length) {
if (listNumbers[i]) {
listPrimeNumbers ~= i.to!int;
}
}
while ((input = readln.chomp.to!int) != 0) {
ulong sum = 0;
foreach (i; 0..listPrimeNumbers.length) {
if (i == input) break;
sum += listPrimeNumbers[i];
}
writeln(sum);
}
}
|
D
|
import std.stdio;
import std.algorithm;
import std.string;
import std.functional;
import std.array;
import std.conv;
import std.math;
import std.typecons;
import std.regex;
import std.range;
real[] r1 = [ 70,55,50,43,40,37.5,35.5];
real[] r2 = [ 148,116,105,89,83,77,71];
string[] ans = ["NA","E","D","C","B","A","AA","AAA"];
void main(){
while(true){
string ss = readln();
if(stdin.eof()) break;
auto s = ss.split().to!(real[]);
for(int i=6;i>=0;i--){
if(s[0] < r1[i] && s[1] < r2[i]){
writeln(ans[i+1]);
break;
}
if(i == 0)
writeln(ans[0]);
}
}
}
|
D
|
import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string;
void main()
{
auto buf = readln.chomp.split;
(buf[1] ~ buf[0]).writeln;
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() { // 日本語文字列のテスト
int w = readint;
int n = readint;
auto xs = new int[w + 1];
for (int i = 0; i < xs.length; i++)
xs[i] = i;
for (int i = 0; i < n; i++) {
auto ab = readln.chomp.split(",").to!(int[]);
int a = ab[0], b = ab[1];
swap(xs[a], xs[b]);
}
foreach (x; xs[1 .. $])
writeln(x);
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
int N = readln.chomp.to!(int);
int[] a = readln.chomp.split.to!(int[]);
int[int] mp;
for (int i = 0; i < N; i++) {
mp[a[i]]++;
}
int ans;
foreach (m; mp.byKey) {
if (m <= mp[m]) {
ans += mp[m] - m;
}
else {
ans += mp[m];
}
}
writeln(ans);
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;
bool[10^^5+1] NS;
void main()
{
auto nk = readln.split.to!(int[]);
auto N = nk[0];
auto K = nk[1];
auto as = readln.split.to!(int[]);
foreach (i; 0..K+1) {
bool win;
foreach (j; 0..N) if (i >= as[j] && !NS[i - as[j]]) {
win = true;
break;
}
NS[i] = win;
}
writeln(NS[K] ? "First" : "Second");
}
|
D
|
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
int best (int [] a)
{
int res = 0;
int cur = 0;
foreach (ref c; a)
{
cur += c;
res = max (res, cur);
}
return res;
}
int solve (int [] r, int [] b)
{
return best (r) + best (b);
}
void main ()
{
auto tests = readln.strip.to !(int);
foreach (test; 0..tests)
{
auto n = readln.strip.to !(int);
auto r = readln.splitter.map !(to !(int)).array;
auto m = readln.strip.to !(int);
auto b = readln.splitter.map !(to !(int)).array;
writeln (solve (r, b));
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;
void main() {
auto s = readln.split.map!(to!int);
auto H = s[0];
auto W = s[1];
auto A = H.iota.map!(_ => readln.chomp).array;
auto B = H.iota.map!(_ => readln.chomp).array;
H.iota.map!(i => W.iota.map!(j => A[i][j] != B[i][j]).sum).sum.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main()
{
int N = readln.chomp.to!(int);
int[][2] arr;
for (int i = 0; i < 2; i++) {
arr[i] = readln.chomp.split.to!(int[]);
}
int ans;
for (int i = 0; i < N; i++) {
int sum;
for (int j = 0; j <= i; j++) {
sum += arr[0][j];
}
for (int j = i; j < N; j++) {
sum += arr[1][j];
}
ans = ans > sum ? ans : sum;
}
ans.writeln;
}
|
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, std.numeric;
long[] scores;
long[][] abcs;
int N;
bool[] updated;
void main(){
auto nm=readln.split.map!(to!int).array;
N=nm[0];
auto M=nm[1];
foreach(i;0..M){
auto abc=readln.split.map!(to!long).array;
abc[0]-=1;
abc[1]-=1;
abcs~=abc;
}
scores.length=N;
updated.length=N;
foreach(ref s;scores[1..$])s=long.min;
foreach(i;0..N)update();
foreach(ref u;updated)u=false;
foreach(i;0..N)update();
if(updated[N-1]) writeln("inf");
else writeln(scores[N-1]);
}
void update(){
foreach(abc;abcs){
if(scores[cast(uint)abc[0]]==long.min)continue;
if(updated[cast(uint)abc[0]]){updated[cast(uint)abc[1]]=true;}
auto res=scores[cast(uint)abc[0]]+abc[2];
if(scores[cast(uint)abc[1]]<res){
scores[cast(uint)abc[1]]=res;
updated[cast(uint)abc[1]]=true;
}
}
}
|
D
|
// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;
int a, b;
void main() {
scan(a, b);
if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0) {
writeln("Possible");
}
else {
writeln("Impossible");
}
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio;
import std.string;
import std.math;
import std.conv;
import std.algorithm;
import std.bigint;
void main(){
int d,m;
int date = 2;
auto ds = ["Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday","Monday"];
auto a = [31,29,31,30,31,30,31,31,30,31,30,31];
while(true){
date = 2;
auto s = split(readln());
d = to!int(s[0]);
m = to!int(s[1]);
if(d == 0 && m == 0) break;
for(int i=0;i<d-1;i++){
date += a[i];
}
date += m-1;
date = date % 7;
writeln(ds[date]);
}
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
const int DATA = 200;
int main() {
int data = to!(int)(chomp(readln));
string[] result = new string[data];
for(int i=0; i<data; i++) {
int a, b, c, tmp; // a:max
string[] input = (chomp(readln)).split(" ");
a = to!(int)(input[0]);
b = to!(int)(input[1]);
c = to!(int)(input[2]);
if(a<b) {
tmp = a;
a = b;
b = tmp;
}
if(a<c) {
tmp = a;
a = c;
c = tmp;
}
if(a*a == b*b+c*c) result[i]="YES";
else result[i]="NO";
}
for(int i=0; i<data; i++) writeln(result[i]);
return 0;
}
|
D
|
import std.stdio,std.conv,std.string,std.algorithm,std.array;
void main(){
auto sn=readln().split();
auto sa=readln().split();
int n,a;
n=to!int(sn[0]);
a=to!int(sa[0]);
if(n%500<=a) writeln("Yes");
else writeln("No");
}
|
D
|
import std.stdio,
std.string,
std.conv;
void main() {
int N = readln.chomp.to!int;
while(N--) {
auto i = readln.split;
auto c = i[0].to!int, a = i[1].to!int, n = i[2].to!int;
int ans;
while(c && a && n) {
ans++;
c--;
a--;
n--;
}
while(c >= 2 && a) {
ans++;
c -= 2;
a--;
}
while(c >= 3) {
ans++;
c -= 3;
}
ans.writeln;
}
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
int readint() {
return readln.chomp.to!int;
}
int[] readints() {
return readln.split.map!(to!int).array;
}
void main() {
auto xs = readints;
int[int] m;
foreach (x; xs)
m[x]++;
foreach (k, v; m) {
if (v == 1)
writeln(k);
}
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
string s;
scan(s);
int n = s.length.to!int;
string[] a = [""];
for (int i = 0; i < n; i++) {
if (s[i] == 'A') {
a.back ~= 'A';
}
else if (s[i] == 'B') {
if (i < n - 1 && s[i + 1] == 'C') {
a.back ~= 'B';
i++;
}
else {
a ~= "";
}
}
else {
a ~= "";
}
}
debug {
writeln(a);
}
long ans;
foreach (si ; a) {
ans += solve(si);
}
writeln(ans);
}
long solve(string s) {
if (s.empty) {
return 0;
}
long ans;
long ac;
foreach (ch ; s) {
if (ch == 'A') {
ac++;
}
else {
ans += ac;
}
}
return ans;
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import core.bitop;
import std.algorithm;
import std.bigint;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;
import std.container;
alias sread = () => readln.chomp();
alias 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 main()
{
long a,b;
scan(a,b);
auto s = sread();
foreach (i, e; s)
{
if(i == b - 1)
{
write(toLower(e));
continue;
}
write(e);
}
writeln();
}
|
D
|
import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
void main() {
auto S = readln.split[0];
auto cnt = 0;
string[] answers = [ "" ];
while (cnt < S.length) {
if ( S[cnt] == 'A' || S[cnt] == 'T' || S[cnt] == 'G' || S[cnt] == 'C') {
answers[$-1] ~= S[cnt];
}
else {
answers ~= "";
}
cnt++;
}
string ans;
answers.each!(x => x.length > ans.length ? (ans = x) : "");
ans.length.writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.format;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.concurrency;
import std.traits;
import std.uni;
import core.bitop : popcnt;
alias Generator = std.concurrency.Generator;
enum long INF = long.max/3;
enum long MOD = 10L^^9+7;
void main() {
long N, K;
scanln(N, K);
if (N.ceil(2) >= K) {
"YES".writeln;
} else {
"NO".writeln;
}
}
// ----------------------------------------------
void times(alias fun)(long n) {
// n.iota.each!(i => fun());
foreach(i; 0..n) fun();
}
auto rep(alias fun, T = typeof(fun()))(long n) {
// return n.iota.map!(i => fun()).array;
T[] res = new T[n];
foreach(ref e; res) e = fun();
return res;
}
T ceil(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
// `(x+y-1)/y` will only work for positive numbers ...
T t = x / y;
if (t * y < x) t++;
return t;
}
T floor(T)(T x, T y) if (isIntegral!T || is(T == BigInt)) {
T t = x / y;
if (t * y > x) t--;
return t;
}
ref T ch(alias fun, T, S...)(ref T lhs, S rhs) {
return lhs = fun(lhs, rhs);
}
unittest {
long x = 1000;
x.ch!min(2000);
assert(x == 1000);
x.ch!min(3, 2, 1);
assert(x == 1);
x.ch!max(100).ch!min(1000); // clamp
assert(x == 100);
x.ch!max(0).ch!min(10); // clamp
assert(x == 10);
}
mixin template Constructor() {
import std.traits : FieldNameTuple;
this(Args...)(Args args) {
// static foreach(i, v; args) {
foreach(i, v; args) {
mixin("this." ~ FieldNameTuple!(typeof(this))[i]) = v;
}
}
}
void scanln(Args...)(auto ref Args args) {
import std.meta;
template getFormat(T) {
static if (isIntegral!T) {
enum getFormat = "%d";
} else static if (isFloatingPoint!T) {
enum getFormat = "%g";
} else static if (isSomeString!T || isSomeChar!T) {
enum getFormat = "%s";
} else {
static assert(false);
}
}
enum string fmt = [staticMap!(getFormat, Args)].join(" ");
string[] inputs = readln.chomp.split;
foreach(i, ref v; args) {
v = inputs[i].to!(Args[i]);
}
}
// fold was added in D 2.071.0
static if (__VERSION__ < 2071) {
template fold(fun...) if (fun.length >= 1) {
auto fold(R, S...)(R r, S seed) {
static if (S.length < 2) {
return reduce!fun(seed, r);
} else {
return reduce!fun(tuple(seed), r);
}
}
}
}
// cumulativeFold was added in D 2.072.0
static if (__VERSION__ < 2072) {
template cumulativeFold(fun...)
if (fun.length >= 1)
{
import std.meta : staticMap;
private alias binfuns = staticMap!(binaryFun, fun);
auto cumulativeFold(R)(R range)
if (isInputRange!(Unqual!R))
{
return cumulativeFoldImpl(range);
}
auto cumulativeFold(R, S)(R range, S seed)
if (isInputRange!(Unqual!R))
{
static if (fun.length == 1)
return cumulativeFoldImpl(range, seed);
else
return cumulativeFoldImpl(range, seed.expand);
}
private auto cumulativeFoldImpl(R, Args...)(R range, ref Args args)
{
import std.algorithm.internal : algoFormat;
static assert(Args.length == 0 || Args.length == fun.length,
algoFormat("Seed %s does not have the correct amount of fields (should be %s)",
Args.stringof, fun.length));
static if (args.length)
alias State = staticMap!(Unqual, Args);
else
alias State = staticMap!(ReduceSeedType!(ElementType!R), binfuns);
foreach (i, f; binfuns)
{
static assert(!__traits(compiles, f(args[i], e)) || __traits(compiles,
{ args[i] = f(args[i], e); }()),
algoFormat("Incompatible function/seed/element: %s/%s/%s",
fullyQualifiedName!f, Args[i].stringof, E.stringof));
}
static struct Result
{
private:
R source;
State state;
this(R range, ref Args args)
{
source = range;
if (source.empty)
return;
foreach (i, f; binfuns)
{
static if (args.length)
state[i] = f(args[i], source.front);
else
state[i] = source.front;
}
}
public:
@property bool empty()
{
return source.empty;
}
@property auto front()
{
assert(!empty, "Attempting to fetch the front of an empty cumulativeFold.");
static if (fun.length > 1)
{
import std.typecons : tuple;
return tuple(state);
}
else
{
return state[0];
}
}
void popFront()
{
assert(!empty, "Attempting to popFront an empty cumulativeFold.");
source.popFront;
if (source.empty)
return;
foreach (i, f; binfuns)
state[i] = f(state[i], source.front);
}
static if (isForwardRange!R)
{
@property auto save()
{
auto result = this;
result.source = source.save;
return result;
}
}
static if (hasLength!R)
{
@property size_t length()
{
return source.length;
}
}
}
return Result(range, args);
}
}
}
// minElement/maxElement was added in D 2.072.0
static if (__VERSION__ < 2072) {
private template RebindableOrUnqual(T)
{
static if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T)
alias RebindableOrUnqual = Rebindable!T;
else
alias RebindableOrUnqual = Unqual!T;
}
private auto extremum(alias map, alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
in
{
assert(!r.empty, "r is an empty range");
}
body
{
alias Element = ElementType!Range;
RebindableOrUnqual!Element seed = r.front;
r.popFront();
return extremum!(map, selector)(r, seed);
}
private auto extremum(alias map, alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
is(typeof(unaryFun!map(ElementType!(Range).init))))
{
alias mapFun = unaryFun!map;
alias selectorFun = binaryFun!selector;
alias Element = ElementType!Range;
alias CommonElement = CommonType!(Element, RangeElementType);
RebindableOrUnqual!CommonElement extremeElement = seedElement;
// if we only have one statement in the loop, it can be optimized a lot better
static if (__traits(isSame, map, a => a))
{
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
if (selectorFun(r[i], extremeElement))
{
extremeElement = r[i];
}
}
}
else
{
while (!r.empty)
{
if (selectorFun(r.front, extremeElement))
{
extremeElement = r.front;
}
r.popFront();
}
}
}
else
{
alias MapType = Unqual!(typeof(mapFun(CommonElement.init)));
MapType extremeElementMapped = mapFun(extremeElement);
// direct access via a random access range is faster
static if (isRandomAccessRange!Range)
{
foreach (const i; 0 .. r.length)
{
MapType mapElement = mapFun(r[i]);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r[i];
extremeElementMapped = mapElement;
}
}
}
else
{
while (!r.empty)
{
MapType mapElement = mapFun(r.front);
if (selectorFun(mapElement, extremeElementMapped))
{
extremeElement = r.front;
extremeElementMapped = mapElement;
}
r.popFront();
}
}
}
return extremeElement;
}
private auto extremum(alias selector = "a < b", Range)(Range r)
if (isInputRange!Range && !isInfinite!Range &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
return extremum!(a => a, selector)(r);
}
// if we only have one statement in the loop it can be optimized a lot better
private auto extremum(alias selector = "a < b", Range,
RangeElementType = ElementType!Range)
(Range r, RangeElementType seedElement)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void) &&
!is(typeof(unaryFun!selector(ElementType!(Range).init))))
{
return extremum!(a => a, selector)(r, seedElement);
}
auto minElement(alias map = (a => a), Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!map(r);
}
auto minElement(alias map = (a => a), Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!map(r, seed);
}
auto maxElement(alias map = (a => a), Range)(Range r)
if (isInputRange!Range && !isInfinite!Range)
{
return extremum!(map, "a > b")(r);
}
auto maxElement(alias map = (a => a), Range, RangeElementType = ElementType!Range)
(Range r, RangeElementType seed)
if (isInputRange!Range && !isInfinite!Range &&
!is(CommonType!(ElementType!Range, RangeElementType) == void))
{
return extremum!(map, "a > b")(r, seed);
}
}
// popcnt with ulongs was added in D 2.071.0
static if (__VERSION__ < 2071) {
ulong popcnt(ulong x) {
x = (x & 0x5555555555555555L) + (x>> 1 & 0x5555555555555555L);
x = (x & 0x3333333333333333L) + (x>> 2 & 0x3333333333333333L);
x = (x & 0x0f0f0f0f0f0f0f0fL) + (x>> 4 & 0x0f0f0f0f0f0f0f0fL);
x = (x & 0x00ff00ff00ff00ffL) + (x>> 8 & 0x00ff00ff00ff00ffL);
x = (x & 0x0000ffff0000ffffL) + (x>>16 & 0x0000ffff0000ffffL);
x = (x & 0x00000000ffffffffL) + (x>>32 & 0x00000000ffffffffL);
return x;
}
}
|
D
|
import std.stdio;
import std.conv;
import std.array;
void main()
{
int N = readln.split[0].to!int;
auto reader = readln.split;
int[] A = new int[N + 1];
for(uint i = 0; i < N + 1; i++){
A[i] = reader[i].to!int;
}
reader = readln.split;
int[] B = new int[N];
for(uint i = 0; i < N; i++){
B[i] = reader[i].to!int;
}
ulong ans = 0;
for (uint i = 0; i < N; i++){
ans += battle(A[i], B[i]);
ans += battle(A[i + 1], B[i]);
}
writeln(ans);
}
int battle(ref int a, ref int b){
int ret;
if (b > a){
ret = a;
b -= a;
a = 0;
} else {
ret = b;
a -= b;
b = 0;
}
return ret;
}
|
D
|
import std;
void main(){
auto input=readln.chomp.split.to!(size_t[]);
auto x=input[0];
auto y=input[1];
auto b_2=y-2*x;
if(b_2%2!=0){
"No".writeln;
return;
}
auto b=b_2/2;
if(x<b){
"No".writeln;
return;
}
"Yes".writeln;
}
|
D
|
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
auto _n = readln;
string[] n = [];
string line;
while((line = readln.chomp) != "") {
n ~= line;
}
foreach (_s; ["S", "H", "C", "D"])
foreach (_i; 1..14) {
auto t = _s ~ " " ~ _i.to!string;
if (!n.canFind(t)) t.writeln;
}
}
|
D
|
import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;
void main()
{
while (true) {
auto rd = readln.split.map!(to!int);
auto h = rd[0], w = rd[1];
if (h == 0 && w == 0) break;
foreach (i; 0..h) {
foreach (j; 0..w)
write((i + j) % 2 == 0 ? '#' : '.');
writeln;
}
writeln;
}
}
|
D
|
// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
auto S = sread();
long N = S.length;
if (N == 1)
{
writeln(S);
return;
}
auto cnt = new long[][](10, 10);
foreach (x; 1 .. S.to!long + 1)
{
auto s = x.text;
// dprint(s[0] - '0', s[$ - 1] - '0');
cnt[s[0] - '0'][s[$ - 1] - '0']++;
}
long ans;
foreach (i; 1 .. 10)
foreach (j; 1 .. 10)
{
// dprint(i, j, cnt[i][j], cnt[j][i]);
ans += cnt[i][j] * cnt[j][i];
}
writeln(ans);
}
|
D
|
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
int calc(string s, string t) {
int n = cast(int) s.length;
if (s == t) return n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j == n) {
return n + i;
}
if (s[i + j] != t[j]) break;
}
}
return n * 2;
}
void main() {
readint;
string s = read!string;
string t = read!string;
writeln(calc(s, t));
}
|
D
|
// import chie template :) {{{
import std.stdio,
std.algorithm,
std.array,
std.string,
std.math,
std.conv,
std.range,
std.container,
std.bigint,
std.ascii;
// }}}
// tbh.scanner {{{
class Scanner {
import std.stdio;
import std.conv : to;
import std.array : split;
import std.string : chomp;
private File file;
private dchar[][] str;
private uint idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
private dchar[] next() {
if (idx < str.length) {
return str[idx++];
}
dchar[] s;
while (s.length == 0) {
s = file.readln.chomp.to!(dchar[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(uint len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
}
// }}}
void main() {
auto cin = new Scanner;
int n, a, b;
n = cin.next!int;
a = cin.next!int;
b = cin.next!int;
writeln((a + b) & 1 ? "Borys" : "Alice");
}
|
D
|
module app;
import core.bitop;
import std.algorithm;
import std.array;
import std.bigint;
import std.container.rbtree;
import std.conv;
import std.stdio;
import std.string;
import std.traits;
struct Input
{
int n, p;
string s;
}
void parseInput(T)(out Input input, T file)
{
with (file) with (input)
{
auto ar = readln().strip().split().map!(to!int).array();
n = ar[0];
p = ar[1];
s = readln().strip();
}
}
auto main2(Input* input)
{
with (input)
{
ulong result = 0;
if (p == 2 || p == 5)
{
foreach (i, c; s)
if ((c - '0' - p) % p == 0)
result += i + 1;
}
else
{
int[] subMods;
subMods.length = n;
int[] rem2count;
rem2count.length = p;
for (int i = n - 1; i >= 0; i--)
{
Mod!int subNum = Mod!(int)(p, 0);
subNum += 10;
subNum ^^= n - 1 - i;
subNum *= s[i] - '0';
if (i < n - 1)
subNum += subMods[i + 1];
if (subNum.value == 0)
result += 1;
result += rem2count[subNum.value];
subMods[i] = subNum.value;
rem2count[subNum.value]++;
}
}
return result;
}
}
alias retType = ReturnType!main2;
static if (!is(retType == void))
{
unittest { writeln("begin unittest"); }
auto _placeholder_ = ReturnType!main2.init;
unittest // example1
{
string example =
`4 3
3543`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 6);
}
unittest // example2
{
string example =
`4 2
2020`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 10);
}
unittest // example3
{
string example =
`20 11
33883322005544116655`;
if (example.empty) return;
Input input = void;
parseExample(input, example);
auto result = main2(&input);
printResult(result);
assert(result == 68);
}
unittest { writeln("end unittest"); }
void parseExample(out Input input, string example)
{
struct Adapter
{
string[] _lines;
this(string input) { _lines = input.splitLines(); }
string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
}
parseInput(input, Adapter(example));
}
}
void printResult(T)(T result)
{
static if (isFloatingPoint!T) writefln("%f", result);
else writeln(result);
}
void main()
{
Input input = void;
parseInput(input, stdin);
static if (is(retType == void))
main2(&input);
else
{
auto result = main2(&input);
printResult(result);
}
}
struct Mod(T)
if ((is(T : int) || is(T : long)))
{
private
{
T _m;
T _value;
}
invariant
{
assert(_m > 0);
assert(_value >= 0);
assert(_value < _m);
}
this() @disable;
this(T m, T t)
{
_m = m;
_value = t % m;
}
void value(T t) { _value = t % _m; }
T value() const { return _value; }
void opAssign(typeof(this) mod) { value = mod._value; }
typeof(this) opBinary(string op)(T t)
{
static if (op == "+") { return Mod(_m, _value + t); }
else static if (op == "-")
{
auto temp = _value - t % _m;
if (temp < 0)
temp += _m;
return Mod(_m, temp);
}
else static if (op == "*") { return Mod(_m, _value * t); }
else static if (op == "^^")
{
if (t < 0) assert(0);
else if (t == 0) return Mod(_m, 1);
else if (t == 1) return this;
else if (t % 2 == 1) return this * (this ^^ (t - 1));
else
{
auto temp = this ^^ (t / 2);
return temp * temp;
}
}
else static assert(0);
}
typeof(this) opBinary(string op)(typeof(this) mod) { return opBinary!op(mod._value); }
void opOpAssign(string op)(T t) { this = this.opBinary!op(t); }
void opOpAssign(string op)(typeof(this) mod) { opOpAssign!op(mod._value); }
bool opEquals()(auto ref const T t) const { return _value == t; }
bool opEquals()(auto ref const typeof(this) mod) const { return opEquals(mod._value); }
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
void main()
{
char[] s = readln.dup.chomp;
s[5] = ' ';
s[13] = ' ';
writeln(s);
}
|
D
|
import std;
alias sread = () => readln.chomp();
alias lread = () => readln.chomp.to!long();
alias aryread(T = long) = () => readln.split.to!(T[]);
//aryread!string();
//auto PS = new Tuple!(long,string)[](M);
//x[]=1;でlong[]全要素1に初期化
void main()
{
long a, b, c, d;
scan(a, b, c, d);
if (a + b > c + d)
{
writeln("Left");
}
else if (a + b < c + d)
{
writeln("Right");
}
else
{
writeln("Balanced");
}
}
void scan(L...)(ref L A)
{
auto l = readln.split;
foreach (i, T; L)
{
A[i] = l[i].to!T;
}
}
void arywrite(T)(T a)
{
a.map!text.join(' ').writeln;
}
|
D
|
import std.stdio;
import std.string;
import std.array; // split
import std.conv; // to
void main()
{
string[] input = split(readln()); // splitで文字列を文字列の配列に切り分け
int a = to!int(input[0]); // 第0要素を整数に変換
int b = to!int(input[1]); // 第1要素を整数に変換
if((a*b)%2 == 0){
writeln("Even"); // 表示
} else {
writeln("Odd"); // 表示
}
}
|
D
|
import std.stdio,std.ascii,std.conv,std.string,std.algorithm,std.range,std.functional,std.math,core.bitop;
void main()
{
int num;
auto a=readln.chomp.to!int;
for(int i=0;i<=a;i++){
if(i%2==0)continue;
int b;
for(int j=1;j<=i;j++){
if(i%j==0)b++;
}
if(b==8)num++;
}
writeln(num);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.array, std.algorithm, std.range;
void main()
{
for(string s; (s=readln().chomp()).length; )
{
immutable N=4;
auto a = s.split().map!(to!int).array();
auto b = readln().split().map!(to!int).array();
int h=0,m=0;
foreach(i;0..N)
if(a[i]==b[i]) ++h;
else
foreach(j;0..N)
if(a[i]==b[j]) ++m;
writeln(h," ",m);
}
}
|
D
|
// import chie template :) {{{
static if (__VERSION__ < 2090) {
import std.stdio, std.algorithm, std.array, std.string, std.math, std.conv,
std.range, std.container, std.bigint, std.ascii, std.typecons, std.format,
std.bitmanip, std.numeric;
} else {
import std;
}
// }}}
// nep.scanner {{{
class Scanner {
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin) {
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType)) {
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next() {
if (idx < str.length) {
return str[idx++];
}
char[] s;
while (s.length == 0) {
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)() {
return next.to!(T);
}
T[] nextArray(T)(size_t len) {
T[] ret = new T[len];
foreach (ref c; ret) {
c = next!(T);
}
return ret;
}
void scan()() {
}
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;
}
}
// }}}
// memo {{{
/*
- ある値が見つかるかどうか
<https://dlang.org/phobos/std_algorithm_searching.html#canFind>
canFind(r, value); -> bool
- 条件に一致するやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#filter>
// 2で割り切れるやつ
filter!"a % 2 == 0"(r); -> Range
- 合計
<https://dlang.org/phobos/std_algorithm_iteration.html#sum>
sum(r);
- 累積和
<https://dlang.org/phobos/std_algorithm_iteration.html#cumulativeFold>
// 今の要素に前の要素を足すタイプの一般的な累積和
// 累積和のrangeが帰ってくる(破壊的変更は行われない)
cumulativeFold!"a + b"(r, 0); -> Range
- rangeをarrayにしたいとき
array(r); -> Array
- 各要素に同じ処理をする
<https://dlang.org/phobos/std_algorithm_iteration.html#map>
// 各要素を2乗
map!"a * a"(r) -> Range
- ユニークなやつだけ残す
<https://dlang.org/phobos/std_algorithm_iteration.html#uniq>
uniq(r) -> Range
- 順列を列挙する
<https://dlang.org/phobos/std_algorithm_iteration.html#permutations>
permutation(r) -> Range
- ある値で埋める
<https://dlang.org/phobos/std_algorithm_mutation.html#fill>
fill(r, val); -> void
- バイナリヒープ
<https://dlang.org/phobos/std_container_binaryheap.html#.BinaryHeap>
// 昇順にするならこう(デフォは降順)
BinaryHeap!(T[], "a > b") heap;
heap.insert(val);
heap.front;
heap.removeFront();
- 浮動小数点の少数部の桁数設定
// 12桁
writefln("%.12f", val);
- 浮動小数点の誤差を考慮した比較
<https://dlang.org/phobos/std_math.html#.approxEqual>
approxEqual(1.0, 1.0099); -> true
- 小数点切り上げ
<https://dlang.org/phobos/std_math.html#.ceil>
ceil(123.4); -> 124
- 小数点切り捨て
<https://dlang.org/phobos/std_math.html#.floor>
floor(123.4) -> 123
- 小数点四捨五入
<https://dlang.org/phobos/std_math.html#.round>
round(4.5) -> 5
round(5.4) -> 5
*/
// }}}
void main() {
auto cin = new Scanner;
long n;
dstring s;
cin.scan(n, s);
long r = s.count('R'.to!dchar),
g = s.count('G'.to!dchar),
b = s.count('B'.to!dchar);
long res = r * g * b;
foreach (i; 0 .. n) {
foreach (j; i + 1 .. n - 1) {
size_t k = j + (j - i);
if (k >= n) continue;
if (s[i] != s[j] && s[j] != s[k] && s[i] != s[k]) {
res--;
}
}
}
writeln(res);
}
|
D
|
import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
import std.typecons;
alias Tuple!(int, "x", int, "y") Point;
auto move = [[-1, 0], [0, 1], [1, 0], [0, -1]];
void main(){
string input;
Point[200] points;
while((input = readln.chomp) != "0"){
int n = input.to!int;
if(n == 1){
"1 1".writeln;
continue;
}
points[1..n] = Point.init;
points[0] = Point(0, 0);
foreach(i; 1..n){
auto line = readln.split;
int base = line[0].to!int;
int dir = line[1].to!int;
points[i] = Point(points[base].x + move[dir][0], points[base].y + move[dir][1]);
}
auto result = reduce!(q{max(a, b.x)}, q{min(a, b.x)}, q{max(a, b.y)}, q{min(a, b.y)})(tuple(0, 0, 0, 0), points[0..n]);
writeln(result[0] - result[1] + 1, " ", result[2] - result[3] + 1);
}
}
|
D
|
import std.stdio;
import std.string;
import std.algorithm;
import std.conv;
import std.array;
long[] read_nums() {return readln.strip.chomp.split(" ").map!(to!(long)).array;}
void main() {
while(true) {
long[] data = read_nums;
long people = data[0];
long kinds = data[1];
if (people == 0 && kinds == 0) break;
long[] limit = read_nums;
long[] sum = new long[](kinds);
bool ans = true;
foreach(i; 0..people) {
long[] line = read_nums;
foreach(long j, x; line) {
sum[j] += x;
}
}
for(long i = 0; i < sum.length; i++) {
if (sum[i] > limit[i]) {
ans = false;
break;
}
}
writeln(ans ? "Yes" : "No");
}
}
|
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 a, b, c;
scan(a);
scan(b);
scan(c);
auto A = a.length.to!int;
auto B = b.length.to!int;
auto C = c.length.to!int;
auto ab = new bool[](20000);
auto ac = new bool[](20000);
auto bc = new bool[](20000);
ab[] = ac[] = bc[] = true;
bool match(char c1, char c2) {
return c1 == '?' || c2 == '?' || c1 == c2;
}
foreach (i ; 0 .. A) {
foreach (j ; 0 .. B) {
if (!match(a[i], b[j])) {
ab[i - j + 10000] = false;
}
}
}
foreach (i ; 0 .. A) {
foreach (j ; 0 .. C) {
if (!match(a[i], c[j])) {
ac[i - j + 10000] = false;
}
}
}
foreach (i ; 0 .. B) {
foreach (j ; 0 .. C) {
if (!match(b[i], c[j])) {
bc[i - j + 10000] = false;
}
}
}
int ans = inf;
foreach (lb ; -4000 .. 4001) {
foreach (lc ; -4000 .. 4001) {
bool valid = ab[lb + 10000] && ac[lc + 10000] && bc[lc - lb + 10000];
if (valid) {
int l = min(lb, lc, 0);
int r = max(lb + B, lc + C, A);
chmin(ans, r - l);
}
}
}
writeln(ans);
}
void scan(T...)(ref T args) {
auto line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront;
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x > arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) if (x < arg) {
x = arg;
isChanged = true;
}
return isChanged;
}
void yes(bool ok, string y = "Yes", string n = "No") {
return writeln(ok ? y : n);
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;
auto solve(string X) {
immutable N = X.length.to!int();
int n=0,q=0;
foreach(c;X) {
if(c=='S') ++q;
else if(q>0) --q;
else ++n;
}
return n+q;
}
void main(){ for(string s; (s=readln.chomp()).length;) writeln(solve(s)); }
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
immutable long MOD = 10^^9 + 7;
immutable long INF = 1L << 59;
void main() {
auto s = readln.split.map!(to!int);
auto N = s[0];
auto A = s[1] - 1;
auto B = s[2] - 1;
auto C = s[3] - 1;
auto D = s[4] - 1;
auto S = readln.chomp;
bool ok1 = !S[A..min(N, C+1)].canFind("##");
bool ok2 = !S[B..min(N, D+1)].canFind("##");
if (C < D) {
writeln(ok1 && ok2 ? "Yes" : "No");
} else {
long flip = S[B-1..min(N, D+2)].indexOf("...");
if (flip == -1) {
writeln("No");
return;
}
writeln(ok1 && ok2 ? "Yes" : "No");
}
}
|
D
|
import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;
string FMT_F = "%.10f";
static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }
void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 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 main()
{
auto n = RD!int;
auto p = RDA!int;
auto cnt = new int[](2);
cnt[0] = n/2;
cnt[1] = (n+1)/2;
foreach (i; 0..n)
{
if (p[i] == 0) continue;
auto j = p[i] % 2;
--cnt[j];
}
auto dp = new int[][][](n+1, n+1, 2);
foreach (i; 0..n+1)
{
foreach (j; 0..n+1)
dp[i][j][] = -1;
}
dp[cnt[0]][cnt[1]][0] = 0;
dp[cnt[0]][cnt[1]][1] = 0;
foreach (i; 0..n)
{
auto ndp = new int[][][](n+1, n+1, 2);
foreach (j; 0..n+1)
{
foreach (k; 0..n+1)
ndp[j][k][] = -1;
}
foreach (j; 0..n+1)
{
foreach (k; 0..n+1)
{
foreach (l; 0..2)
{
if (dp[j][k][l] == -1) continue;
if (p[i] != 0)
{
auto eo = p[i] % 2;
auto num = dp[j][k][l] + (eo != l ? 1 : 0);
if (ndp[j][k][eo] == -1)
ndp[j][k][eo] = num;
else
ndp[j][k][eo].chmin(num);
continue;
}
if (j != 0)
{
auto num = dp[j][k][l] + (l != 0 ? 1 : 0);
if (ndp[j-1][k][0] == -1)
ndp[j-1][k][0] = num;
else
ndp[j-1][k][0].chmin(num);
}
if (k != 0)
{
auto num = dp[j][k][l] + (l != 1 ? 1 : 0);
if (ndp[j][k-1][1] == -1)
ndp[j][k-1][1] = num;
else
ndp[j][k-1][1].chmin(num);
}
}
}
}
dp = ndp;
}
debug writeln(dp);
int ans = int.max;
foreach (i; 0..2)
{
if (dp[0][0][i] != -1)
ans.chmin(dp[0][0][i]);
}
writeln(ans);
stdout.flush;
debug readln;
}
|
D
|
string solve(int n){
int x = (n.to!double/1.08).to!int();
if((x*1.08).to!int() < n)x++;
if((x*1.08).to!int()==n)return x.to!string();
else return ":(";
}
void main(){
int n = inelm();
writeln(solve(n));
}
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;
// 1要素のみの入力
T inelm(T= int)(){
return to!(T)( readln().chomp() );
}
// 1行に同一型の複数入力
T[] inln(T = int)(){
T[] ln;
foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
return ln;
}
|
D
|
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
void main() {
int n;
scan(n);
int[int] cnt;
foreach (i ; 0 .. n) {
int ai;
scan(ai);
cnt[ai]++;
debug {
writeln(cnt);
}
}
int ans;
foreach (n, v; cnt) {
if (v & 1) ans++;
}
writeln(ans);
}
void scan(T...)(ref T args) {
string[] line = readln.split;
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
|
D
|
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;
void main() {
auto S = readln.chomp;
auto N = S.length.to!int;
auto ans = new int[](N);
foreach (i; 0..N-1) {
if (S[i..i+2] == "RL") {
int p = i;
for (int j = i, k = 0; j >= 0 && S[j] == 'R'; --j, k ^= 1) ans[p+k] += 1;
p = i + 1;
for (int j = i+1, k = 0; j < N && S[j] == 'L'; ++j, k ^= 1) ans[p-k] += 1;
}
}
ans.map!(to!string).join(" ").writeln;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;
enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;
void main() {
int n;
scan(n);
int ca, cb, cab;
int ans;
foreach (i ; 0 .. n) {
auto s = readln.chomp;
foreach (j ; 0 .. s.length.to!int - 1) {
if (s[j .. j + 2] == "AB") ans++;
debug { writeln(s[j .. j + 2]); }
}
if (s.front == 'B') cb++;
if (s.back == 'A') ca++;
if (s.front == 'B' && s.back == 'A') cab++;
}
ans += min(ca, cb);
if (ca > 0 && ca == cb && cb == cab) {
ans--;
}
writeln(ans);
}
int[][] readGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new int[][](n, 0);
foreach (i; 0 .. m) {
int u, v;
scan(u, v);
if (is1indexed) {
u--, v--;
}
adj[u] ~= v;
if (isUndirected) {
adj[v] ~= u;
}
}
return adj;
}
alias Edge = Tuple!(int, "to", int, "cost");
Edge[][] readWeightedGraph(int n, int m, bool isUndirected = true, bool is1indexed = true) {
auto adj = new Edge[][](n, 0);
foreach (i; 0 .. m) {
int u, v, c;
scan(u, v, c);
if (is1indexed) {
u--, v--;
}
adj[u] ~= Edge(v, c);
if (isUndirected) {
adj[v] ~= Edge(u, c);
}
}
return adj;
}
void yes(bool b) {
writeln(b ? "Yes" : "No");
}
void YES(bool b) {
writeln(b ? "YES" : "NO");
}
T[] readArr(T)() {
return readln.split.to!(T[]);
}
T[] readArrByLines(T)(int n) {
return iota(n).map!(i => readln.chomp.to!T).array;
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, 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 h, n;
scan(h, n);
auto a = new int[](n);
auto b = new int[](n);
foreach (i ; 0 .. n) {
scan(a[i], b[i]);
}
auto dp = new long[][](n + 1, h + 1);
foreach (j ; 1 .. h + 1) {
dp[0][j] = inf6;
}
foreach (i ; 1 .. n + 1) {
foreach (j ; 0 .. h + 1) {
dp[i][j] = dp[i - 1][j];
if (j - a[i - 1] < 0) {
chmin(dp[i][j], dp[i][0] + b[i - 1]);
}
if (j - a[i - 1] >= 0) {
chmin(dp[i][j], dp[i][j - a[i - 1]] + b[i - 1]);
}
}
foreach_reverse (j ; 0 .. h) {
chmin(dp[i][j], dp[i][j + 1]);
}
}
auto ans = dp[n][h];
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
|
//dlang template---{{{
import std.stdio;
import std.conv;
import std.string;
import std.array;
import std.algorithm;
import std.typecons;
// MIT-License https://github.com/kurokoji/nephele
class Scanner
{
import std.stdio : File, stdin;
import std.conv : to;
import std.array : split;
import std.string;
import std.traits : isSomeString;
private File file;
private char[][] str;
private size_t idx;
this(File file = stdin)
{
this.file = file;
this.idx = 0;
}
this(StrType)(StrType s, File file = stdin) if (isSomeString!(StrType))
{
this.file = file;
this.idx = 0;
fromString(s);
}
private char[] next()
{
if (idx < str.length)
{
return str[idx++];
}
char[] s;
while (s.length == 0)
{
s = file.readln.strip.to!(char[]);
}
str = s.split;
idx = 0;
return str[idx++];
}
T next(T)()
{
return next.to!(T);
}
T[] nextArray(T)(size_t len)
{
T[] ret = new T[len];
foreach (ref c; ret)
{
c = next!(T);
}
return ret;
}
void scan()()
{
}
void scan(T, S...)(ref T x, ref S args)
{
x = next!(T);
scan(args);
}
void fromString(StrType)(StrType s) if (isSomeString!(StrType))
{
str ~= s.to!(char[]).strip.split;
}
}
//Digit count---{{{
int DigitNum(int num) {
int digit = 0;
while (num != 0) {
num /= 10;
digit++;
}
return digit;
}
//}}}
//}}}
void main() {
Scanner sc = new Scanner;
int X;
sc.scan(X);
if (X == 7 || X == 5 || X == 3) {
writeln("YES");
} else {
writeln("NO");
}
}
|
D
|
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;
void main()
{
auto N = readln.chomp.to!long;
long r;
foreach (i; 1..N+1) {
r += i * (N-i+1);
}
foreach (_; 1..N) {
auto uv = readln.split.to!(long[]);
auto u = uv[0];
auto v = uv[1];
if (u > v) swap(u, v);
r -= u * (N-v+1);
}
writeln(r);
}
|
D
|
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto a = readln.chomp.map!(c => cast(int)(c-'0')).array;
foreach (i; 0..1<<3) {
auto r = a[0];
foreach (j; 0..3)
r += a[j+1] * (i.bitTest(j) ? 1 : -1);
if (r == 7) {
write(a[0]);
foreach (j; 0..3)
write(i.bitTest(j) ? "+" : "-", a[j+1]);
writeln("=7");
return;
}
}
}
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.algorithm;
import std.array;
import std.conv;
import std.math;
import std.numeric;
import std.stdio;
import std.string;
void main()
{
long n = readln.chomp.to!long;
long[] a = readln.chomp.split(" ").to!(long[]);
long sum;
foreach (x; a)
{
sum += x - 1;
}
writeln(sum);
}
|
D
|
import std.algorithm, std.array, std.conv, std.stdio, std.string;
void main()
{
writeln(cast(char)(readln[0]+1));
}
|
D
|
import std.stdio, std.string, std.algorithm, std.array;
void main()
{
writeln(readln.chomp.split(" ").uniq.array.length == 1 ? "H" : "D");
}
|
D
|
void main()
{
long n = rdElem;
long[] a = rdRow;
long[long] list;
foreach (i, x; a)
{
++list[i-x];
}
long result;
foreach (i, x; a)
{
--list[i-x];
if (i + x in list)
{
result += list[i+x];
}
}
result.writeln;
}
enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;
T rdElem(T = long)()
if (!is(T == struct))
{
return readln.chomp.to!T;
}
alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);
T rdElem(T)()
if (is(T == struct))
{
T result;
string[] input = rdRow!string;
assert(T.tupleof.length == input.length);
foreach (i, ref x; result.tupleof)
{
x = input[i].to!(typeof(x));
}
return result;
}
T[] rdRow(T = long)()
{
return readln.split.to!(T[]);
}
T[] rdCol(T = long)(long col)
{
return iota(col).map!(x => rdElem!T).array;
}
T[][] rdMat(T = long)(long col)
{
return iota(col).map!(x => rdRow!T).array;
}
void rdVals(T...)(ref T data)
{
string[] input = rdRow!string;
assert(data.length == input.length);
foreach (i, ref x; data)
{
x = input[i].to!(typeof(x));
}
}
void wrMat(T = long)(T[][] mat)
{
foreach (row; mat)
{
foreach (j, compo; row)
{
compo.write;
if (j == row.length - 1) writeln;
else " ".write;
}
}
}
import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;
|
D
|
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math, core.stdc.stdio;
void main() {
string[3] ss;
foreach(ref s; ss) s = readln();
writeln(ss[0][0], ss[1][1], ss[2][2]);
}
|
D
|
import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void main()
{
auto nm = readln.chomp.split.map!(to!int);
int n = nm.front;
int m = nm.back;
writeln = m * 1900 * (1 << m) + (n - m) * 100 * (1 << m);
}
|
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 a, b;
scan(a, b);
writeln(a * b % 2 ? "Yes" : "No");
}
void scan(T...)(ref T args) {
import std.stdio : readln;
import std.algorithm : splitter;
import std.conv : to;
import std.range.primitives;
auto line = readln().splitter();
foreach (ref arg; args) {
arg = line.front.to!(typeof(arg));
line.popFront();
}
assert(line.empty);
}
void fillAll(R, T)(ref R arr, T value) {
static if (is(typeof(arr[] = value))) {
arr[] = value;
}
else {
foreach (ref e; arr) {
fillAll(e, value);
}
}
}
bool chmin(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x > arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
bool chmax(T, U...)(ref T x, U args) {
bool isChanged;
foreach (arg; args) {
if (x < arg) {
x = arg;
isChanged = true;
}
}
return isChanged;
}
|
D
|
import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;
void main()
{
auto s = readln.chomp;
bool[char] ch;
foreach (e; s) {
ch[e] = false;
}
bool f;
foreach (c; 'a'..'z'+1) {
auto t = c.to!char;
if (ch.get(t, true)) {
writeln(t);
f = true;
break;
}
}
if (!f) {
writeln("None");
}
}
|
D
|
import std.conv, std.stdio, std.string, std.typecons, std.math;
alias Tuple!(int,int) p;
void main() {
int [] hwd = readln().chomp().split().to!(int[]);
int H = hwd[0], W = hwd[1];
int D = hwd[2];
int [][] A;
p [] crd;
A.length = H;
crd.length = H*W+1;
foreach(i; 0..H) {
A[i] = readln().chomp().split().to!(int[]);
foreach(j; 0..W) {
crd[A[i][j]] = p(i,j);
}
}
int [][] cost;
cost.length = 20;
cost[0].length = H*W+1;
foreach(i; 1..H*W+1) {
if(i+D <= H*W) {
cost[0][i] = to!(int)(abs(crd[i][0] - crd[i+D][0]) + abs(crd[i][1] - crd[i+D][1]));
}
else {
cost[0][i] = -1;
}
}
foreach(i; 1..20) {
cost[i].length = H*W+1;
foreach(j; 1..H*W+1) {
if(j+D*to!(long)(1<<i) <= H*W) {
cost[i][j] = cost[i-1][j] + cost[i-1][j+D*(1<<(i-1))];
}
else {
cost[i][j] = -1;
}
}
}
int Q = readln().chomp().to!(int);
foreach(i; 0..Q) {
int [] lr = readln().chomp().split().to!(int[]);
int it = (lr[1] - lr[0]) / D;
int j = 0, k = lr[0];
long res = 0;
while(it) {
if(it & 1) {
res += cost[j][k];
k += D*(1 << j);
}
j++;
it /= 2;
}
writeln(res);
}
}
|
D
|
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void scan(T...)(ref T a) {
string[] ss = readln.split;
foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;
const MOD = 10^^9 + 7;
long calc(long k, int[] a) {
// a 内での転倒数
long p = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) p++;
}
}
long ans1 = (p * k) % MOD;
// 二つの a に対する転倒数
long q = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] > a[j]) q++;
}
}
long ans2 = q * (((k * (k - 1)) / 2) % MOD); // q * kC2
ans2 %= MOD;
return (ans1 + ans2) % MOD;
}
void main() {
int n, k; scan(n, k);
auto a = readints;
writeln(calc(k, a));
}
|
D
|
import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.range;
void main()
{
for(string s_; (s_=readln().chomp()).length;)
{
auto NM = s_.split().map!(to!int)();
immutable N=NM[0], M=NM[1];
auto xy = new int[][M];
foreach(ref a;xy) a=readln.split.map!(x=>x.to!int()-1).array();
auto c = new int[N]; c[]=1;
auto p = new bool[N]; p[]=false; p[0]=true;
foreach(ref a;xy)
{
immutable x=a[0], y=a[1];
++c[y]; p[y]=p[y]||p[x];
--c[x]; p[x]=p[x]&&c[x]!=0;
}
writeln(count(p,true));
}
}
|
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; }
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 t = RD!int;
auto ans = new bool[](t);
foreach (ti; 0..t)
{
auto p = RD!string;
auto h = RD!string;
auto cnt1 = new int[](26);
foreach (c; p)
++cnt1[c-'a'];
auto cnt2 = new int[][](h.length+1, 26);
foreach (i, c; h)
{
cnt2[i+1] = cnt2[i].dup;
++cnt2[i+1][c-'a'];
}
foreach (i; p.length..h.length+1)
{
bool ok = true;
foreach (j; 0..26)
{
if (cnt2[i][j] - cnt2[i-p.length][j] != cnt1[j])
{
ok = false;
break;
}
}
if (ok)
{
ans[ti] = true;
break;
}
}
}
foreach (e; ans)
writeln(e ? "YES" : "NO");
stdout.flush;
debug readln;
}
|
D
|
import std.stdio;
import std.string;
import std.conv;
void main() {
int n = readln.chomp.to!int;
bool[string] table;
foreach (i;0..n) {
auto line = readln.chomp;
table[line] = true;
}
writeln(table.length);
}
|
D
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.