code
stringlengths
4
1.01M
language
stringclasses
2 values
import std.stdio; import std.conv; import std.algorithm; import std.string; import std.array; void main() { int n = readln.chomp.to!int; auto s = readln.chomp.split; bool[string] dict; int ans; foreach(ss; s) { if ((ss in dict) is null) { dict[ss] = true; ans++; } } if (ans == 3) { writeln("Three"); } else { writeln("Four"); } return; }
D
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math, core.stdc.stdio; int f(int x) { string s = x.to!string; int r = 1, n = cast(int) s.length; foreach(i, c; s) { r &= s[i] == s[n-1-i]; } return r; } void main() { int a, b; scanf("%d %d", &a, &b); int ans; foreach(x; a..b+1) ans += f(x); ans.writeln; }
D
import std.stdio; void main() { string line=readln(); int ans=0; for(int i=0;i<3;i++) ans+=line[i]-'0'; writeln(ans); }
D
import std.stdio; import std.conv; import std.string; import std.typecons; import std.algorithm; import std.array; import std.range; import std.math; import std.regex : regex; import std.container; import std.bigint; import std.ascii; void main() { auto n = readln.chomp; writeln("ABC" ~ n); }
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; // 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; // dfmt on void main() { long n, m; scan(n, m); auto b = new bool[](n); long L = 1; long R = n; foreach (_; 0 .. m) { long l, r; scan(l, r); L = max(l, L); R = min(r, R); } max(R - L + 1, 0).writeln(); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; void main() { string str = readln.chomp; string ans = ""; string[] boin = ["w", "", "k", "s", "t", "n", "h", "m", "y", "r"]; string[] shiin = ["a", "i", "u", "e", "o"]; for (int i=0; i<str.length; i+=2) { if (str[i..i+2] == ['0', 'U']) { ans ~= "nn"; } else { ans ~= boin[str[i]-'0']; switch(str[i+1]) { case 'T': ans ~= shiin[0]; break; case 'L': ans ~= shiin[1]; break; case 'U': ans ~= shiin[2]; break; case 'R': ans ~= shiin[3]; break; case 'D': ans ~= shiin[4]; break; default: } } } ans.writeln; }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long A, B, C, D; scan(A, B, C, D); long f(long x) { long lcm = (C * D) / gcd(C, D); return x - (x / C) - (x / D) + (x / lcm); } writeln(f(B) - f(A - 1)); }
D
import std.stdio, std.string, std.array, std.algorithm, std.conv; void main() { immutable nm = readln.chomp.split(" ").map!(to!int).array; immutable n = nm[0]; immutable m = nm[1]; auto nodes = new int[][n]; foreach(_i; 0..m) { auto ab = readln.chomp.split(" ").map!(to!int).array; nodes[ab[0]-1] ~= ab[1]-1; nodes[ab[1]-1] ~= ab[0]-1; } int search(int pos, int[] hist) { auto nexts = nodes[pos].filter!(n => hist.find(n).empty); if (nexts.empty) { if (hist.length == n) return 1; else return 0; } return reduce!"a + b"(0, nexts.map!(node => search(node, hist ~ node))); } writeln(search(0, [0])); }
D
import std.algorithm; import std.array; import std.conv; import std.range; import std.stdio; import std.string; import std.typecons; void main() { //stdin.open("input.txt", "r"); //stdout.open("output.txt", "w"); string num = strip(readln()); if (num[0] == '-') { int a = to!int(num[0..$-1]); int b = to!int(num[0..$-2] ~ num[$-1..$]); writeln(a > b ? a : b); } else { writeln(num); } }
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; static import std.ascii; // 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() { auto S = sread(); long N = S.length; long K = lread(); auto dp = new long[][][](2, 4, N + 1); // N以下確定か否か、0でない数字使用回数、処理する位置 dp[0][0][N] = 1; foreach_reverse (i; 0 .. N) { long s = S[N - i - 1] - '0'; dp[s != 0][3][i] += dp[0][3][i + 1]; dp[1][3][i] += dp[1][3][i + 1]; foreach (j; 0 .. 3) { if (s == 0) { dp[1][j + 1][i] += dp[1][j][i + 1] * 9; dp[1][j][i] += dp[1][j][i + 1]; dp[0][j][i] += dp[0][j][i + 1]; } else { dp[1][j + 1][i] += dp[1][j][i + 1] * 9; dp[1][j][i] += dp[1][j][i + 1]; dp[0][j + 1][i] += dp[0][j][i + 1]; // sを使う dp[1][j][i] += dp[0][j][i + 1]; // 0を使う dp[1][j + 1][i] += dp[0][j][i + 1] * (s - 1); // それ以外 } } } // writeln(dp[0][K][0]); // writeln(dp[1][K][0]); writeln(dp[0][K][0] + dp[1][K][0]); }
D
import std; long calc(long n) { if (n == 1) return 0; int ans = 0; foreach (p; factor(n)) { long k = p[1]; for (int i = 1; ; i++) { if (k >= i) { k -= i; ans++; } else break; } } return ans; } void main() { long n; scan(n); writeln(calc(n)); } void scan(T...)(ref T a) { string[] ss = readln.split; foreach (i, t; T) a[i] = ss[i].to!t; } T read(T=string)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readints = reads!int; // 素因数分解([[素因数, 個数], ...]) T[][] factor(T)(T n) { if (n == 1) return [[1, 1]]; long[][] res; for (T i = 2; i * i <= n; i++) { if (n % i != 0) continue; int k = 0; while (n % i == 0) { n /= i; k++; } res ~= [i, k]; } if (n > 1) res ~= [n, 1]; return res; }
D
import std.stdio; import std.string; import std.conv; void main() { int x = to!int(chomp(readln())); writeln(x < 1200 ? "ABC": "ARC"); }
D
import std.algorithm, std.string, std.array, std.range, std.stdio, std.conv; void main() { int[] nm = readln.chomp.split.to!(int[]); int n = nm[0], m = nm[1]; int[][] routes; routes.length = n + 1; foreach (i; m.iota) { int[] ab = readln.chomp.split.to!(int[]); int ta = ab[0], tb = ab[1]; routes[tb] ~= ta; } bool flag; foreach (route1; routes[n]) { foreach (route; routes[route1]) { if (route == 1) { flag = true; break; } } if (flag) break; } writeln(flag ? "POSSIBLE" : "IMPOSSIBLE"); }
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; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto K = s[1]; auto A = readln.split.map!(to!int).array; A.map!(a => a >= K).sum.writeln; }
D
import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum A = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]; void main() { try { for (; ; ) { const K = readInt() - 1; writeln(A[K]); } } catch (EOFException e) { } }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.stdio; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!int).array; auto B = new long[](N+10); long ans = 0; foreach (a; A) B[a] += 1; foreach (b; B) ans += b * (b - 1) / 2; foreach (a; A) writeln(ans - B[a] * (B[a] - 1) / 2 + (B[a] - 1) * (B[a] - 2) / 2); }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; void main() { auto n = readln.chomp.to!size_t; auto a = readln.split.to!(int[]), mi = a.reduce!min, ma = a.reduce!max; auto ans = int.max; foreach (x; mi..ma+1) ans = min(ans, a.map!(ai => (ai - x) ^^ 2).sum); writeln(ans); }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm; void main() { int n = readln.chomp.to!int; int[] a = readln.chomp.split.to!(int[]); int q = readln.chomp.to!int; foreach (i; 0 .. q) { int[] tmp = readln.chomp.split.to!(int[]); int b = tmp[0], e = tmp[1], k = tmp[2]; a[b..e].count(k).writeln; } }
D
void main() { long n = rdElem; long[] a = rdRow; long m = rdElem; long[] b = rdRow; foreach (x; b) { long idx = a.binSrch!"a <= b"(x); if (idx == -1 || a[idx] != x) { 0.writeln; return; } } 1.writeln; } long binSrch(alias pred = "a < b")(long[] arr, long val) { alias predFun = binaryFun!pred; long l = -1, r = arr.length; if (!predFun(l, r)) swap(l, r); while (abs(r - l) > 1) { long m = (l + r) / 2; if (predFun(arr[m], val)) l = m; else r = m; } return l; } 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
import std.stdio; import std.conv; import std.string; import std.algorithm; import std.math; void main(){ while(true){ string[] s = []; for(int i=0;i<8;i++){ string temp = readln(); if(stdin.eof()) break; s ~= chomp(temp); } if(stdin.eof()) break; string ans = "none"; for(int i=0;i<7;i++) for(int j=0;j<7;j++) if(s[i][j]=='1'&&s[i][j+1]=='1'&&s[i+1][j]=='1'&&s[i+1][j+1]=='1') ans = "A"; for(int i=0;i<5;i++) for(int j=0;j<8;j++) if(s[i][j]=='1'&&s[i+1][j]=='1'&&s[i+2][j]=='1'&&s[i+3][j]=='1') ans = "B"; for(int i=0;i<8;i++) for(int j=0;j<5;j++){ if(s[i][j]=='1'&&s[i][j+1]=='1'&&s[i][j+2]=='1'&&s[i][j+3]=='1') ans = "C"; } for(int i=0;i<6;i++) for(int j=1;j<8;j++) if(s[i][j]=='1'&&s[i+1][j]=='1'&&s[i+1][j-1]=='1'&&s[i+2][j-1]=='1') ans = "D"; for(int i=0;i<7;i++) for(int j=0;j<6;j++) if(s[i][j]=='1'&&s[i][j+1]=='1'&&s[i+1][j+1]=='1'&&s[i+1][j+2]=='1') ans = "E"; for(int i=0;i<6;i++) for(int j=0;j<7;j++) if(s[i][j]=='1'&&s[i+1][j]=='1'&&s[i+1][j+1]=='1'&&s[i+2][j+1]=='1') ans = "F"; for(int i=0;i<7;i++) for(int j=1;j<7;j++) if(s[i][j]=='1'&&s[i][j+1]=='1'&&s[i+1][j-1]=='1'&&s[i+1][j]=='1') ans = "G"; writeln(ans); if(stdin.eof()) break; auto a = readln(); if(stdin.eof()) break; } }
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() { while (true) { int N; scan(N); if (N == 0) return; auto w = readln.split.to!(int[]); auto ans = solve(N, w); writeln(ans); } } int solve(int N, int[] w) { auto dp = new int[][](N + 1, N + 1); fillAll(dp, -1); int rec(int l, int r) { if (r - l <= 1) { return 0; } if (dp[l][r] != -1) { return dp[l][r]; } dp[l][r] = 0; foreach (i ; l + 1 .. r) { chmax(dp[l][r], rec(l, i) + rec(i, r)); } if (rec(l + 1, r - 1) == r - 1 - (l + 1) && abs(w[l] - w[r - 1]) <= 1) { dp[l][r] = r - l; } return dp[l][r]; } auto ans = rec(0, N); return ans; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } } bool chmin(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x > arg) { x = arg; isChanged = true; } } return isChanged; } bool chmax(T, U...)(ref T x, U args) { bool isChanged; foreach (arg; args) { if (x < arg) { x = arg; isChanged = true; } } return isChanged; }
D
import std.algorithm, std.conv, std.range, std.stdio, std.string; const size_t n = 10; void main() { auto hi = n.iota.map!(_ => readln.chomp.to!int).array; hi.sort!("a > b"); hi.take(3).each!(writeln); }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; import std.range; import std.container; import std.bigint; import std.math; void main() { auto ab = readln.split.map!(to!int); writeln(ab[0]*ab[1], " ", ab[0]*2+ab[1]*2); }
D
module app; import core.bitop; import std.algorithm; import std.array; import std.bigint; import std.conv; import std.stdio; import std.string; import std.traits; struct Input { int a, b, c; } void parseInput(T)(out Input input, T file) { with (file) with (input) { auto abc = readln().strip().split().map!(to!int).array(); a = abc[0].to!int; b = abc[1].to!int; c = abc[2].to!int; } } auto main2(Input* input) { with (input) { if (a == b && b == c) return "No"; if (a == b) return "Yes"; if (b == c) return "Yes"; if (c == a) return "Yes"; return "No"; } } alias retType = ReturnType!main2; static if (!is(retType == void)) { unittest { writeln("begin unittest"); } auto _placeholder_ = ReturnType!main2.init; unittest // example1 { string example = `5 7 5`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "Yes"); } unittest // example2 { string example = `4 4 4`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "No"); } unittest // example3 { string example = `4 9 6`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "No"); } unittest // example4 { string example = `3 3 4`; if (example.empty) return; Input input = void; parseExample(input, example); auto result = main2(&input); printResult(result); assert(result == "Yes"); } 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); alias retType = ReturnType!main2; static if (is(retType == void)) main2(&input); else { auto result = main2(&input); printResult(result); } }
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 A = aryread(); long ans; foreach (i, a; A) { if (((i + 1) & 1) && (a & 1)) ans++; } 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) { int ans = 0; for (int i = 0; i < s.length - 1; i++) { int cnt = 0; foreach (c; 'a'..'z' + 1) { if (s[0..i+1].canFind(c) && s[i+1..$].canFind(c)) { cnt++; } } ans = max(ans, cnt); } return ans; } void main() { readint; auto s = readln.chomp; writeln(calc(s)); }
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() { auto N = readln.chomp.to!ulong; auto halfN = N / 2; writeln(halfN - (N%2 == 0 ? 1 : 0)); }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "a", long, "b"); alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); void main() { long a, b, c, k; scan(a, b, c, k); if (a > k) k.writeln(); else if (a + b >= k) a.writeln(); else writeln(a - (k - (a + b))); } 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.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(){ A: foreach(_; 0 .. scan!int){ int n = scan!int; long[] as = scan!long(n); long t = as[0]; long maxg = 0; foreach(a; as){ if(a < t) maxg.raiseTo(t - a); else t = a; } if(maxg == 0) "0".writeln; else foreach(i; 0 .. 60){ if((1L<<i) <= maxg) continue; i.writeln; continue A; } } }
D
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.functional, std.math, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container; ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000; alias sread = () => readln.chomp(); alias lread(T = long) = () => readln.chomp.to!(T); alias aryread(T = long) = () => readln.split.to!(T[]); alias Pair = Tuple!(long, "x", long, "y"); alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less); void main() { auto s = sread(); if (s.solve()) writeln("YES"); else writeln("NO"); } bool solve(string s) { auto key = "keyence"; if (s[0 .. key.length] == key) return true; if (s[$ - key.length .. $] == key) return true; long iter; while (s[iter] == key[iter]) iter++; // s[$ - (key.length - iter) .. $].writeln(); // key[iter .. $].writeln(); return (s[$ - (key.length - iter) .. $] == key[iter .. $]); } 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 mk = readln.split.to!(uint[]); auto M = mk[0]; auto K = mk[1]; if (K == 0) { foreach (a; 0..2^^M-1) { write(a, " "); write(a, " "); } write(2^^M-1, " "); writeln(2^^M-1); return; } uint c; foreach (a; 1..2^^M) { if (a == K) continue; c ^= a; } if (c == K) { foreach (a; 0..2^^M) { if (a == K) continue; write(a, " "); } write(K, " "); foreach_reverse (a; 0..2^^M) { if (a == K) continue; write(a, " "); } writeln(K); } else { writeln("-1"); } }
D
import std.stdio, std.conv, std.string, std.math; void main(){ auto ip = readln.split.to!(int[]); if(ip[0] * ip[1] < ip[2] * ip[3]){ writeln(ip[2] * ip[3]); } else if(ip[0] * ip[1] > ip[2] * ip[3]){ writeln(ip[0] * ip[1]); } else { writeln(ip[2] * ip[3]); } }
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 minAssign(T, U = T)(ref T dst, U src) { dst = cast(T) min(dst, src); } void maxAssign(T, U = T)(ref T dst, U src) { dst = cast(T) max(dst, src); } enum MOD = (10 ^^ 9) + 7; void main() { long X, Y; scan(X, Y); long n = X; long cnt; while (n <= Y) { cnt++; n <<= 1; } writeln(cnt); }
D
/+ dub.sdl: name "F" dependency "dunkelheit" version=">=0.7.4" +/ 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); g[a] ~= b; g[b] ~= a; } if (iota(n).filter!(i => g[i].length >= 3).empty) { writeln(1); return 0; } int ans = 0; bool dfs(int p, int b) { bool f = true; if (g[p].length >= 3) f = false; int lc = 0; foreach (d; g[p]) { if (d == b) continue; bool u = dfs(d, p); if (u) lc++; else f = false; } if (g[p].length >= 3) { ans += max(0, lc-1); f = false; } return f; } foreach (i; 0..n) { if (g[i].length >= 3) { dfs(i, -1); break; } } writeln(ans); return 0; } /* IMPORT /home/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 /home/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 /home/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(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) { import std.exception; enforce(readSingle(x)); static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { read!enforceEOF(args); } } void read(bool enforceEOF = false, Args...)(auto ref Args args) { import std.exception; static if (args.length == 0) { enforce(enforceEOF == false || !succ()); } else { enforce(readSingle(args[0])); read!enforceEOF(args); } } } /* 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 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; void main() { auto n = readln.chomp.to!int * 2; auto cards = iota(1, n+1).array; auto m = readln.chomp.to!int; while (m--) { auto t = readln.chomp.to!int; if (t == 0) { auto tmp = cards.dup; foreach (i; iota(0, n, 2)) { tmp[i] = cards[i/2]; tmp[i+1] = cards[(i+n)/2]; } cards = tmp; } else { cards = cards[t..$] ~ cards[0..t]; } } cards.map!(to!string).join("\n").writeln; }
D
import std.conv; import std.stdio; import std.string; void main() { auto n = readln.strip.to!int; auto as = readln.split.to!( long[] ); writeln( solve( n, as ) ); } auto solve( in int n, in long[] as ) { auto c = 0; foreach( i, long a; as ) { while( 0 < a && a % 2 == 0 ) { a /= 2; c++; } } return c; } unittest { assert( solve( 3, [ 5, 2, 4 ] ) == 3 ); assert( solve( 4, [ 631, 577, 243, 199 ] ) == 0 ); assert( solve( 10, [ 2184, 2126, 1721, 1800, 1024, 2528, 3360, 1945, 1280, 1776 ] ) == 39 ); }
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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto N = RD; long ans; for (long i = 1; i * i < N; ++i) { /*if (N / i == N % i) { ans += i; //debug writeln("1:", i, " ", i); }*/ auto x = N - i; auto y = x / i; if (x % i == 0 && x / y == i && N / y == i) { debug writeln("3:", y, " ", N / y, " ", i); ans += y; //debug writeln("3:", i, " ", y); } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; //long mod = 998244353; //long mod = 1_000_003; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto A = RD; auto B = RD; auto C = RD; writeln(A + B >= C ? "Yes" : "No"); stdout.flush(); debug readln(); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, core.bitop; Tuple!(N, N)[] prime_division(N)(N n) { auto nn = n; Tuple!(N, N)[] res; for (N i = 2; i^^2 <= nn; ++i) { if (n%i == 0) { N cnt; while (n%i == 0) { ++cnt; n /= i; } res ~= tuple(i, cnt); } } if (n != cast(N)1) res ~= tuple(n, cast(N)1); return res; } void main() { auto N = readln.chomp.to!long; long[] ps; foreach (p; prime_division(N)) { ps ~= p[0]; } auto r = N; foreach (x; 1..(1<<(ps.length))) { long n = 1; foreach (i; 0..ps.length) if (x & (1L<<i)) n *= ps[i]; r += N/n * (-1)^^popcnt(x); } writeln(r); }
D
import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; import std.numeric; void main() { int n; scan(n); foreach (k ; 1 .. n + 1) { int s = k * (k + 1) / 2; if (n <= s) { int x = s - n; foreach (j ; 1 .. k + 1) { if (j == x) continue; writeln(j); } return; } } } struct Stack(T) { private: int N, peek; T[] data; public: this(int size) { N = size; data = new T[](N); } bool empty() @property { return peek == 0; } bool full() @property { return peek == N; } void push(T x) @property { assert(!full); data[peek++] = x; } void pop() @property { assert(!empty); --peek; } T top() @property { return data[peek - 1]; } void clear() @property { peek = 0; } int length() @property { return peek; } } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }
D
import std.algorithm; 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() { readln; auto xs = readints(); int n = cast(int) xs.length; auto segTree = new SegmentTree(n + 1); foreach (x; xs) { auto max = segTree.query(1, x); segTree.update(x, x + max); } auto max = segTree.query(1, n + 1); writeln(cast(long) n * (n + 1) / 2 - max); } class SegmentTree { private long[] _data; this(int n) { int len = 1; while (len < n) len *= 2; _data = new long[len * 2]; } /// k ?????????????´????(0-indexed)??? a ????????´ void update(int k, long a) { // ????????\??? k += (_data.length / 2) - 1; _data[k] = a; // ?????????????????´??° while (k > 0) { k = (k - 1) / 2; _data[k] = max(_data[k * 2 + 1], _data[k * 2 + 2]); } } /// [a, b) ???????°????????±??????? long query(int a, int b, int k, int l, int r) { // [a, b) ??¨ [l, r) ?????????????????? if (r <= a || b <= l) return 0; // [a, b) ??? [l, r) ????????¨???????????§????????°???????????\????????? if (a <= l && r <= b) { return _data[k]; } // ????????§???????????°???2 ?????????????????§??? auto vl = query(a, b, k * 2 + 1, l, (l + r) / 2); auto vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return max(vl, vr); } long query(int a, int b) { return query(a, b, 0, 0, cast(int)(_data.length / 2)); } }
D
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string; void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}} void main() { int x, a, b; readV(x, a, b); writeln((x-a).abs < (x-b).abs ? "A" : "B"); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; void main() { auto k = readln.chomp.to!int; auto ab = readln.split.to!(int[]); auto A = ab[0]; auto B = ab[1]; auto x = k; while (x <= B) { if (x >= A) { writeln("OK"); return; } x += k; } writeln("NG"); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto P = readln.split.map!(a => a.to!int-1).array; auto Q = new int[](N); foreach (i; 0..N) Q[P[i]] = i; auto uf = new UnionFind(N); foreach (i; 0..M) { s = readln.split.map!(to!int); int a = s[0] - 1; int b = s[1] - 1; uf.unite(a, b); } auto B = new bool[int][](N); foreach (i; 0..N) { if (uf.find(i) != i) continue; foreach (x; uf.group[i]) B[i][x] = true; } int ans = 0; foreach (i; 0..N) { int j = uf.find(Q[i]); if (i in B[j]) ans += 1; } ans.writeln; } class UnionFind { int N; int[] table; int[][] group; this(int n) { N = n; table = new int[](N); fill(table, -1); group = new int[][](N); foreach (i; 0..N) group[i] = [i]; } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; group[x] ~= group[y].dup; group[y] = []; table[y] = x; } }
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() { int[] answers; answers ~= readln.chomp.to!int; answers ~= readln.chomp.to!int; answers = answers.sort().array; if (answers[0] == 1 && answers[1] == 2) writeln(3); if (answers[0] == 1 && answers[1] == 3) writeln(2); if (answers[0] == 2 && answers[1] == 3) writeln(1); }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; int readInt(){ return readln.chomp.to!int; } int[] readInts(){ return readln.chomp.split.to!(int[]); } void main(){ string s = readln.chomp; int nf = s[0 .. 2].to!int; int nl = s[2 .. 4].to!int; bool YYMM = false, MMYY = false; if((nf == 0 || nf > 12) && (nl == 0 || nl > 12)) { writeln("NA"); return; } if(1 <= nf && nf <= 12) MMYY = true; if(1 <= nl && nl <= 12) YYMM = true; if(YYMM && MMYY){ writeln("AMBIGUOUS"); } else if(YYMM){ writeln("YYMM"); } else if(MMYY){ writeln("MMYY"); } }
D
import std.stdio,std.conv, std.algorithm, std.container,std.array,std.range,std.string,std.typecons; const dx = [1,0,-1,0], dy = [0,1,0,-1]; const readMixin = q{ auto line = readln().split(); if (line.length < args.length) return; foreach(i,ref arg; args) arg = line[i].to!(typeof(arg)); }; void read(T...) (auto ref T args){ mixin(readMixin);} void readArray(T)(auto ref T args){ mixin(readMixin);} //------------------------------ const coin = [10,50,100,500]; void main(){ int total; read(total); while(total){ auto have = new int[4]; readArray(have); int howMany = int.max; auto uses = new int[4]; foreach(i0;iota(have[0]+1)){ foreach(i1;iota(have[1]+1)){ foreach(i2;iota(have[2]+1)){ foreach(i3;iota(have[3]+1)){ auto tmpuses = [i0,i1,i2,i3]; auto pay = i0 * coin[0] + i1 * coin[1] + i2 * coin[2] + i3 * coin[3]; if (pay < total) continue; int tmpHowMany = have.sum + howManyCoins(pay - total) - tmpuses.sum; if (tmpHowMany < howMany ){ howMany = tmpHowMany; uses = tmpuses; } } } } } foreach(i,u;uses){if(u >0)writeln(coin[i]," ",u);} read(total); if (total) writeln(); } } int howManyCoins(int pay){ int res; foreach_reverse(i,c;coin){ while(pay >= c){ pay -= c;res ++; } } return res; }
D
import std.stdio; import std.string; import std.conv; import std.algorithm; const dx = [0, 1, 0, -1]; const dy = [1, 0, -1, 0]; int H, W; bool[55][55] memo; string[] s; int bfs(int h, int w) { if (!(0 <= h && h < H && 0 <= w && w < W)) return 0; if (s[h][w] == '.') return 0; if (s[h][w] == '#' && memo[h][w] == true) return 0; memo[h][w] = true; int ans; for (int i = 0; i < 4; i++) { ans += bfs(h + dy[i], w + dx[i]); } return ans + 1; } void main() { int[] buf = readln.chomp.split.to!(int[]); H = buf[0], W = buf[1]; for (int i = 0; i < H; i++) { s ~= readln.chomp; } bool flg = true; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { flg &= (bfs(i, j) == 1 ? false : true); } } writeln(flg ? "Yes" : "No"); }
D
import std.stdio; import std.string; import std.conv; import std.typecons; import std.algorithm; import std.functional; import std.bigint; import std.numeric; import std.array; import std.math; import std.range; import std.container; import std.ascii; void main(){ auto N = readln.chomp.to!int; auto tanda = readln.split.to!(int[]); auto T = tanda[0]; auto ideal = tanda[1]; auto H = readln.split.to!(int[]); real X = real.max; int count; int result; foreach(i; H){ count++; auto difference = abs(T - i * 0.006 - ideal); if(difference < X){ X = difference; result = count; } } writeln(result); } //writeln("滲み出す混濁の紋章 不遜なる狂気の器 湧き上がり 否定し 痺れ 瞬き 眠りを妨げる 爬行する鉄の王女 絶えず自壊する泥の人形 結合せよ 反発せよ 地に満ち己の無力を知れ 破道の九十 黒棺")
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; enum P = 10L^^9+7; void main() { readln.chomp.to!int; int x, y, z; long r = 1; foreach (a; readln.split.to!(int[])) { long d; if (a == x) ++d; if (a == y) ++d; if (a == z) ++d; r = (r * d) % P; if (a == x) { ++x; } else if (a == y) { ++y; } else { ++z; } } writeln(r); }
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; import std.container; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } long binarySearch(long min, long max, bool delegate(long) f){ if(!f(min)) return min - 1; if(f(max)) return max; long mid; while(max - min > 1){ mid = min + (max - min) / 2; if(f(mid)) min = mid; else max = mid; } return min; } bool check(long x){ writeln("? ", x); stdout.flush; return (readln.chomp == "Y"); } void main(){ long[] ms = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000]; long ans; if(check(ms[10])){ // 10...0 の形である foreach(m; ms){ if(check(m + 1)){ ans = m; break; } } } else{ foreach(m; ms){ if(!check(m * 10)){ ans = binarySearch(m, m * 10 - 1, x => !check(x * 10)) + 1; break; } } } writeln("! ", ans); }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; struct UFTree(T) { struct Node { T parent; T rank = 1; } /// T min_size, max_size; /// this(T n) { nodes.length = n; sizes.length = n; foreach (i, ref node; nodes) { node = Node(i.to!T); sizes[i] = 1; } min_sizes.length = n + 1; min_sizes[1] = n; min_size = 1; max_size = 1; } /// bool unite(T a, T b) { a = root(a); b = root(b); if (a == b) return false; auto a_size = sizes[a]; auto b_size = sizes[b]; --min_sizes[a_size]; --min_sizes[b_size]; ++min_sizes[a_size + b_size]; foreach (nxt_size; min(a_size, b_size)..min_sizes.length) if (min_sizes[nxt_size] != 0) { min_size = nxt_size.to!T; break; } max_size = max(max_size, a_size + b_size); if (nodes[a].rank < nodes[b].rank) { sizes[a] += sizes[b]; nodes[b].parent = a; } else { sizes[b] += sizes[a]; nodes[a].parent = b; if (nodes[a].rank == nodes[b].rank) ++nodes[b].rank; } return true; } /// bool is_same(T a, T b) { return root(a) == root(b); } /// T size(T i) { return sizes[root(i)]; } private: Node[] nodes; T[] sizes; T[] min_sizes; T root(T i) { if (nodes[i].parent == i) return i; return nodes[i].parent = root(nodes[i].parent); } } /// UFTree!T uftree(T)(T n) { return UFTree!T(n); } void main() { auto nm = readln.split.to!(int[]); auto N = nm[0]; auto M = nm[1]; int[] as, bs; foreach (_; 0..M) { auto ab = readln.split.to!(int[]); as ~= ab[0]-1; bs ~= ab[1]-1; } int r; foreach (i; 0..M) { auto uft = uftree(N); foreach (j; 0..M) if (i != j) { uft.unite(as[j], bs[j]); } if (!uft.is_same(as[i], bs[i])) ++r; } writeln(r); }
D
import std.stdio; import std.algorithm; import std.conv; import std.string; import std.array; import std.math; void main(){ auto nums = readln().chomp.split.map!(to!int); int[] result; result.length = nums[1]; for(int i; i < nums[0]; i++){ auto input = readln().chomp.split.map!(to!int); foreach(a; input[1..$]){ result[a-1]++; } } result.count!("a==b")(nums[0]).writeln; }
D
void main() { dchar[] n = readln.chomp.to!(dchar[]); auto g = n.group.assocArray; writeln(g.values.any!"a >= 3" ? "Yes" : "No"); } import std.stdio; import std.string; import std.array; import std.conv; import std.algorithm; import std.range; import std.math; import std.numeric; import std.container; import std.typecons; import std.ascii; import std.uni;
D
import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.regex, std.conv, std.stdio, std.typecons; void main() { for (;;) { auto rd = readln.split.map!(to!int); auto n = rd[0], x = rd[1]; if (n == 0 && x == 0) break; auto r = 0; foreach (i; 1..n+1) foreach (j; i+1..n+1) { auto k = x - i - j; if (k > j && k <= n) ++r; } writeln(r); } }
D
import std.stdio, std.string, std.array, std.conv, std.algorithm.iteration, std.functional; long[10^^5+1] AS; void main() { readln; auto as = readln.split.to!(long[]); foreach (a; as) ++AS[a]; auto cnt = as.length; size_t i = 1, j = 10^^5+1; for (; j > i && i < 10^^5+1 && j > 0;) { if (AS[i] > 2) { AS[i] -= 2; cnt -= 2; } else if (AS[i] == 2) { if (AS[j] < 2) { --j; } else if (AS[j] > 2) { AS[j] -= 2; cnt -= 2; } else { --AS[i]; --AS[j]; cnt -= 2; } } else { ++i; } } while (AS[i] > 1) { AS[i] -= 2; cnt -= 2; } while (AS[j] > 1) { AS[j] -= 2; cnt -= 2; } writeln(cnt); }
D
void main() { problem(); } void problem() { auto A = scan!long; auto B = scan!long; long solve() { return A * B; } solve().writeln; } // ---------------------------------------------- import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric; T[][] combinations(T)(T[] s, in int m) { if (!m) return [[]]; if (s.empty) return []; return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); } string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; } void deb(T ...)(T t){ debug writeln(t); } alias Point = Tuple!(long, "x", long, "y"); // -----------------------------------------------
D
import std.stdio, std.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; long[] A; long[] B; Tuple!(long, long)[] X; Tuple!(long, long)[] Y; void main() { auto D = readln.chomp.to!long; A = new long[](10); B = new long[](10); X = new Tuple!(long, long)[](10); Y = new Tuple!(long, long)[](10); foreach (i; 0..10) { foreach (j; 0..10) { if (i >= j) { A[i-j] += 1; X[i-j] = tuple(i.to!long, j.to!long); } else { B[i-j+10] += 1; Y[i-j+10] = tuple(i.to!long, j.to!long); } } } long ans = 0; foreach (i; D.to!string.length.to!int..19) { ans += solve(D, i); } ans.writeln; } long solve(long D, int N) { int M = N/2 + N%2; long ans = 0; int[] DD; long d = D; for (int i = 0; i < N; ++i, d/=10) { DD ~= (d % 10).to!int; } foreach (mask; 0..(1 << M)) { long tmp = 1; long[] tmp1; long[] tmp2; bool carry = false; foreach (i; 0..M) { if (mask & (1 << i)) { if (carry) { tmp *= A[(DD[i]+1)%10]; tmp1 ~= X[(DD[i]+1)%10][0]; tmp2 ~= X[(DD[i]+1)%10][1]; carry = DD[i] == 9; } else if (i == 0) { tmp *= A[DD[i]] - 1; tmp1 ~= X[DD[i]][0]; tmp2 ~= X[DD[i]][1]; } else { tmp *= A[DD[i]]; tmp1 ~= X[DD[i]][0]; tmp2 ~= X[DD[i]][1]; carry = false; } } else { if (carry && DD[i] == 9) { tmp = 0; break; } else if (carry) { tmp *= B[DD[i]+1]; tmp1 ~= Y[DD[i]+1][0]; tmp2 ~= Y[DD[i]+1][1]; } else if (i == 0) { tmp *= B[DD[i]] - 1; tmp1 ~= Y[DD[i]][0]; tmp2 ~= Y[DD[i]][1]; carry = true; } else { tmp *= B[DD[i]]; tmp1 ~= Y[DD[i]][0]; tmp2 ~= Y[DD[i]][1]; carry = true; } } } if (tmp == 0) continue; if (N % 2 == 1) tmp1.popBack; long e = 0; long f = 0; foreach (i; tmp1) e *= 10, e += i; foreach_reverse (i; tmp2) e *= 10, e += i; foreach (i; tmp2) f *= 10, f += i; foreach_reverse (i; tmp1) f *= 10, f += i; if (f - e != D) tmp = 0; ans += tmp; } return ans; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; auto solve(immutable int n) { auto a = iota(n).map!(_=>readln().chomp()).array(); auto dp = new int[][](n+1,n+1); foreach(i;0..n) foreach(j;0..n) dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j]-dp[i][j]+(a[i][j]!='.'?1:0); int m = 0; foreach(i;0..n) foreach(j;0..n) { int lo=0, hi=min(n-i,n-j)+1; while(lo+1<hi) { immutable k = (lo+hi)/2; if(0==dp[i+k][j+k]-dp[i][j+k]-dp[i+k][j]+dp[i][j]) lo=k; else hi=k; } m=max(m,lo); } return m; } void main() { for(int n; 0!=(n=readln().chomp().to!int()); ) writeln(solve(n)); }
D
void main(){ import std.stdio, std.string, std.conv, std.algorithm; int n; rd(n); auto a=readln.split.to!(int[]); auto num=new int[](9); foreach(e; a){ foreach(i; 0..8){ if(i*400<=e && e<(i+1)*400){ num[i]++; break; } } if(e>=3200){ num[$-1]++; } } auto lb=reduce!((r, e)=>(r+(e>0 ? 1 : 0)))(0, num[0..$-1]); if(num[$-1]==0){ writeln(lb, " ", lb); }else{ if(lb==0){ writeln(1, " ", num[$-1]); }else{ writeln(lb, " ", lb+num[$-1]); } } } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x) e=l[i].to!(typeof(e)); }
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 s = RD!string; long l, r; foreach (i; 0..s.length) { if (s[i] == 'A') { l = i; break; } } foreach_reverse (i; 0..s.length) { if (s[i] == 'Z') { r = i; break; } } writeln(r-l+1); stdout.flush(); debug readln(); }
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 long[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto a = RDA!int; auto b = RDA!int; bool[int] set; long cnt; foreach (i; 0..m) { if (set.get(b[i], false)) { ++cnt; set.remove(b[i]); continue; } cnt += set.length*2; while (a.front != b[i]) { set[a.front] = true; a.popFront; cnt += 2; } a.popFront; ++cnt; debug writeln("cnt:", cnt); } ans[ti] = cnt; } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.stdio; import std.algorithm; import std.string; import std.range; import std.array; import std.conv; import std.complex; import std.math; import std.ascii; import std.bigint; import std.container; import std.typecons; auto readInts() { return array(map!(to!int)(readln().strip().split())); } auto readInt() { return readInts()[0]; } auto readLongs() { return array(map!(to!long)(readln().strip().split())); } auto readLong() { return readLongs()[0]; } const real eps = 1e-10; void main(){ while(true) { auto nr = readInts(); auto n = nr[0]; auto r = nr[1]; if(n == 0 && r == 0) { return; } int[] cards = array(iota(n)); reverse(cards); for(int i = 0; i < r; ++i) { auto pc = readInts(); auto p = pc[0]; auto c = pc[1]; cards = cards[p-1..p-1+c] ~ cards[0..p-1] ~ cards[p-1+c..$]; } writeln(cards[0]+1); } }
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; void main(){ int q = readln.chomp.to!int; string x, y; int ans; foreach(qi ; 0 .. q){ x = readln.chomp; y = readln.chomp; ans = computeLCS(x, y); writeln(ans); } } int computeLCS(string x, string y){ int n = x.length.to!int; int m = y.length.to!int; auto dp = new int[][](n + 1, m + 1); foreach(i ; 1 .. n + 1){ foreach(j ; 1 .. m + 1){ dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (x[i - 1] == y[j - 1]){ dp[i][j] = dp[i - 1][j - 1] + 1; } } } return dp[n][m]; } 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){ writeln(line); throw new Exception("args num < input num"); } }
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 NK = readln.split.to!(int[]); auto N = NK[0], K = NK[1]; bool[] flag; flag.length = N; foreach (i; 0 .. K) { readln; auto A = readln.split.to!(int[]); foreach (a; A) { flag[a-1] = true; } } int ans; foreach (x; flag) { if (!x) ans++; } writeln(ans); }
D
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, std.bitmanip; immutable long MOD = 10^^9 + 7; long[] F; void solve() { auto s = readln.split.map!(to!long); auto N = s[0]; auto X = s[1]; writeln(X * 2); } void main() { auto T = readln.chomp.to!int; while(T--) solve; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; void main() { iota(1,readln().chomp().to!long()+1).reduce!"a*b"().writeln(); }
D
void main() { import std.algorithm, std.array, std.range, std.stdio, std.string, std.conv; readln; auto yum = cast(ubyte[])(readln.chomp); yum.uniq.walkLength.writeln; }
D
import std.stdio; import std.string; import std.conv; void main(){ int n; while((n = readln.chomp.to!int) != 0){ string input = readln.chomp; foreach(Unused; 0..n){ input = input.operate; } input.writeln; } } string operate(string input){ string res; char current = input[0]; int count = 1; foreach(c; input[1..$]){ if(current == c){ ++count; }else{ res ~= text(count, current); current = c; count = 1; } } res ~= text(count, current); return 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; } 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; } bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } } bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } } long lcm(long x, long y) { return x * y / gcd(x, y); } long mod = 10^^9 + 7; 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 H = RD; auto W = RD; auto A = new string[](H); foreach (i; 0..H) { A[i] = RD!string; } long[3][] open; auto used = new long[][](H, W); foreach (i; 0..H) { fill(used[i], long.max); foreach (j; 0..W) { if (A[i][j] == '#') { open ~= [i, j, 0]; used[i][j] = 0; } } } long ans; void f(long y, long x, long c) { if (c < used[y][x]) { used[y][x] = c; open ~= [y, x, c]; ans = max(ans, c); } } while (!open.empty) { auto n = open.front; open.popFront; auto y = n[0]; auto x = n[1]; auto c = n[2] + 1; if (inside(y-1, 0, H)) { f(y-1, x, c); } if (inside(y+1, 0, H)) { f(y+1, x, c); } if (inside(x-1, 0, W)) { f(y, x-1, c); } if (inside(x+1, 0, W)) { f(y, x+1, c); } } writeln(ans); stdout.flush(); debug readln(); }
D
import std.algorithm; import std.array; import std.conv; import std.math; import std.range; import std.stdio; import std.string; import std.typecons; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; void main() { auto nm = readints; int n = nm[0], m = nm[1]; long ans = 0; for (long i = 1; i * i <= m; i++) { if (m % i == 0) { foreach (g; [i, m / i]) { if (m >= g * n) { // a_1, a_2, .., a_n の和(約数)が m を超えてはいけない ans = max(ans, g); } } } } writeln(ans); }
D
import std.functional, std.algorithm, std.container, std.typetuple, std.typecons, std.bigint, std.string, std.traits, std.array, std.range, std.stdio, std.conv, std.format, std.math; void main() { auto s = readln.chomp; auto t = readln.chomp; int a; foreach(i; 0..s.length){ if(s == t){ a = 1; break; }else{ s ~= s.front; s.popFront(); } } if(a == 1) writeln("Yes"); else writeln("No"); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto A = RD; auto V = RD; auto B = RD; auto W = RD; auto T = RD; auto d = abs(A-B); auto v = V - W; auto ans = v * T >= d; writeln(ans ? "YES" : "NO"); stdout.flush; debug readln; }
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 n = RD!int; if (n == 1) writeln("9 8"); else { writeln(n*3, " ", n*2); } stdout.flush; debug readln; }
D
import std.stdio, std.string, std.conv; import std.array, std.algorithm, std.range; int[][] J; static this() { foreach(i;0..3) J~=[i*3,i*3+1,i*3+2]; foreach(i;0..3) J~=[i,i+3,i+6]; foreach(i;0..2) J~=[i*2,4,8-i*2]; } char solve(char[] s) { foreach(c;"ox") foreach(a;J) if(a.map!(i=>s[i]==c).all!"a"()) return c; return 'd'; } void main() { foreach(s;stdin.byLine()) writeln(solve(s)); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto k = RD!int; auto cnt = n / k; auto x = min(cnt, m); m -= x; ans[ti] = x - (m+k-2) / (k-1); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
import std.array, std.stdio, std.conv, std.string, std.math, std.random, std.range,std.functional; import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.comparison; void main() { int[] input = readln().split.to!(int[]); int N = input[0]; int K = input[1]; auto wList = iota(N).map!(a=>readln().strip.to!int).array(); int maxWight = wList.reduce!"a>b?a:b"; writeln(search(maxWight, maxWight*N, K, wList)); } int search(int min, int max, int k, int[] list) { if(max-min==1) { if(fun(min, k, list)) { return min; }else{ return max; } } int p = min+(max-min)/2; if(fun(p, k, list)) { return search(min, p, k, list); }else{ return search(p, max, k, list); } } bool fun(int p, int k, int[] list) { int ck=1; int cp; foreach(e; list) { if(cp + e <= p) { cp += e; }else if(ck < k){ ck++; cp = e; }else{ return false; } } return true; } sizediff_t minIndex(alias pred="a<b", R) (R list) if(isForwardRange!R && !isInfinite!R && is(typeof(binaryFun!pred(list.front, list.front)))) { assert(!list.empty); typeof(list.front) min = list.front; sizediff_t minPos; sizediff_t curPos; for(list.popFront(); !list.empty; list.popFront()) { curPos++; if(binaryFun!pred(list.front, min)) { min = list.front; minPos = curPos; } } return minPos; }
D
void main() { int[] tmp = readln.split.to!(int[]); int r = tmp[0], g = tmp[1], b = tmp[2], n = tmp[3]; int cnt; int x = n / r; foreach (i; 0 .. x+1) { int y = (n - r * i) / g; foreach (j; 0 .. y+1) { if ((n - r * i - g * j) % b == 0) { ++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
import std.stdio, std.conv, std.string, std.bigint; import std.math, std.random, std.datetime; import std.array, std.range, std.algorithm, std.container, std.format; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } /* けたごとに見る。 A[i]たちについて、そのけたで1が多ければXのそのけたは0、0が多ければXのそのけたは1 それを集めれば良いが…、K以下という制限がある その場合は、上のほうのけたを、やむなく1が良かったのに0にする */ void main(){ int n = read.to!int; long k = read.to!long; long[] as = readln.chomp.split.map!(to!long).array; long xt; // kを超えているかもしれないが int maxi; for(int i = 0; (1L<<i) < 2 * k; i ++){ int cnt; foreach(a; as) if((1L<<i) & a) cnt += 1; if(cnt * 2 < n) xt += 1L<<i; maxi = i; debug writeln("i:", i, " cnt:", cnt, " xt:", xt); } long[] xs = [0]; // k以下である候補 foreach_reverse(i; 0 .. maxi + 1){ if(! ((1L<<i) & xt)) continue; if(((1L<<i) ^ xt) <= k) xs ~= (1L<<i) ^ xt; else xt ^= (1L<<i); } debug writeln("xs:", xs); long ans; foreach(x; xs){ long tmpans = 0; foreach(a; as) tmpans += x ^ a; if(tmpans > ans) ans = tmpans; debug writeln("x:", x, " tmpans:", tmpans, " ans:", ans); } ans.writeln; }
D
import std.stdio; import std.algorithm; import std.math; import std.conv; import std.string; T readNum(T)(){ return readStr.to!T; } T[] readNums(T)(){ return readStr.split.to!(T[]); } string readStr(){ return readln.chomp; } void main(){ auto n = readNum!int; auto p = readNums!int; int nmin = p[0], ret = 1; for(int i = 1; i < n; i++){ if(p[i] <= nmin){ nmin = p[i]; ret++; } } writeln(ret); }
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 s = RD!string; int[int] cnt; cnt[0] = 1; int x; foreach (i; 0..n) { x += s[i] - '0'; auto y = x-i-1; ans[ti] += cnt.get(y, 0); cnt[y] += 1; debug writeln(ans[ti]); } } 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, std.container, std.range; void get(Args...)(ref Args args) { import std.traits, std.meta, std.typecons; static if (Args.length == 1) { alias Arg = Args[0]; static if (isArray!Arg) { args[0] = readln.split.to!Arg; } else static if (isTuple!Arg) { auto input = readln.split; static foreach (i; 0..Fields!Arg.length) { args[0][i] = input[i].to!(Fields!Arg[i]); } } else { args[0] = readln.chomp.to!Arg; } } else { auto input = readln.split; assert(input.length == Args.length); static foreach (i; 0..Args.length) { args[i] = input[i].to!(Args[i]); } } } void get_lines(Args...)(size_t N, ref Args args) { import std.traits, std.range; static foreach (i; 0..Args.length) { static assert(isArray!(Args[i])); args[i].length = N; } foreach (i; 0..N) { static if (Args.length == 1) { get(args[0][i]); } else { auto input = readln.split; static foreach (j; 0..Args.length) { args[j][i] = input[j].to!(ElementType!(Args[j])); } } } } void solve() { int N; get(N); int[] AS; get(AS); foreach (i; 1..N) if (AS[i-1] <= AS[i]) { writeln("YES"); return; } writeln("NO"); } void main() { int T; get(T); foreach (_; 0..T) solve(); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto a = RD; auto b = RD; auto c = RD; auto d = RD; a -= b; ans[ti] = b; if (a <= 0) continue; auto e = c - d; if (e <= 0) { ans[ti] = -1; continue; } ans[ti] += c * ((a+e-1) / e); } foreach (e; ans) { writeln(e); } stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static string[] s_rd; T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } string RDR()() { return readln.chomp; } T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } long lcm(long x, long y) { return x * y / gcd(x, y); } //long mod = 10^^9 + 7; long mod = 998244353; void moda(ref long x, long y) { x = (x + y) % mod; } void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; } void modm(ref long x, long y) { x = (x * y) % mod; } void main() { auto T = RD!int; string[] ans; foreach (i; 0..T) { auto N = RD!int; auto s = RD!string; bool a; foreach (j; 0..N) { if (j > N - 11) break; if (s[j] == '8') { a = true; break; } } ans ~= a ? "YES" : "NO"; } foreach (e; ans) writeln(e); stdout.flush(); debug readln(); }
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.datetime; void main() { auto N = readln.chomp.to!int; auto A = readln.split.map!(to!long).array; auto M = A.reduce!min; int last = - (1 << 29); int ans = 1 << 29; foreach (i; 0..N) { if (A[i] == M) { ans = min(ans, i - last); last = i; } } ans.writeln; }
D
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric; long lcm(long a, long b) { return a * b / gcd(a, b); } void main() { auto N = readln.chomp.to!int; auto as = readln.split.to!(long[]); auto ls = new long[](N); ls[0] = as[0]; foreach (i; 1..N) ls[i] = gcd(as[i], ls[i-1]); auto rs = new long[](N); rs[$-1] = as[$-1]; foreach_reverse (i; 0..N-1) rs[i] = gcd(as[i], rs[i+1]); auto x = rs[1]; foreach (i; 1..N-1) x = lcm(x, gcd(ls[i-1], rs[i+1])); writeln(lcm(x, ls[$-2])); }
D
import std.stdio, std.string, std.conv; import std.typecons; import std.algorithm; bool isPrefixOf(string x, string y){ if (x.length > y.length) return false; bool res = true; for(int i;i<x.length;i++){ res = res && x[i] == y[i]; } return res; } void main(){ string x = readln.chomp; string y = readln.chomp; int res = 0; for(int i=0; i < x.length;){ if (isPrefixOf(y,x[i..$])){ res++; i += y.length; } else { i++; } } writeln(res); }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } //long mod = 10^^9 + 7; long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto n = RD!int; auto a = RDA; auto cnt = new int[](20); foreach (i; 0..n) { foreach (j; 0..20) { auto bit = 1L << j; if (a[i] & bit) { ++cnt[j]; } } } long ans; foreach (i; 0..n) { long t; foreach (j; 0..20) { if (cnt[j]) { t += 1L << j; --cnt[j]; } } ans += t^^2; } writeln(ans); stdout.flush; debug readln; }
D
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container; import std.math, std.random, std.bigint, std.datetime, std.format; void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; } T read(T)(){ return read.to!T; } T[] read(T)(long n){ return n.iota.map!(_ => read!T).array; } T[][] read(T)(long m, long n){ return m.iota.map!(_ => read!T(n)).array; } // ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- // void solve(){ int n = read.to!int; int m = read.to!int; int r = read.to!int; int a = 9999; foreach(i; 0 .. n) a = min(a, read.to!int); int b = -1; foreach(j; 0 .. m) b = max(b, read.to!int); int x; if(a > b) x = 0; else x = r / a; int ans = (r - x * a) + (x * b); ans.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 d = r.next!uint; const m = r.next!uint; uint add (uint x, uint y) { x += y; if (x >= m) x -= m; return x; } uint sub (uint x, uint y) { if (x < y) x += m; x -= y; return x; } uint mul (uint x, uint y) { ulong z = x.to!ulong * y; return (z % m).to!uint; } auto deg = new uint[32]; int last = 1; foreach (i; 1 .. 32) { uint l = min (d + 1, 1U << i); debug stderr.writeln ("l = ", l); if (l == last) break; uint dt = l - last; debug stderr.writeln ("dt = ", dt); deg[i] = dt % m; last = l; } debug stderr.writeln (deg); auto u = new uint[32]; u[0] = 1; uint res; while (u.any!("a != 0")) { auto e = new uint[32]; foreach (i; 0 .. 32) { foreach (j; i + 1 .. 32) { e[j] = add (e[j], mul (u[i], deg[j])); } } debug stderr.writeln ("e = ", e); u = e; foreach (i; u) { res = add (res, i); } } writeln (res); } }
D
import std.stdio, std.conv, std.functional, std.string; import std.algorithm, std.array, std.container, std.range, std.typecons; import std.bigint, std.numeric, std.math, std.random; import core.bitop; string FMT_F = "%.10f"; static File _f; void file_io(string fn) { _f = File(fn, "r"); } static string[] s_rd; T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; } T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; } T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; } T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; } T[] RDA(T = long)(T fix = 0) { if (_f.isOpen) return _RDA!T(_f, fix); else return _RDA!T(fix); } size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;} size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; } void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); } bool inside(T)(T x, T b, T e) { return x >= b && x < e; } T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); } long mod = 10^^9 + 7; //long mod = 998_244_353; //long mod = 1_000_003; void moda(T)(ref T x, T y) { x = (x + y) % mod; } void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; } void modm(T)(ref T x, T y) { x = (x * y) % mod; } void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); } void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); } void main() { auto t = RD!int; auto ans = new long[](t); foreach (ti; 0..t) { auto n = RD!int; auto a = RDA; foreach (i; 0..n-1) { auto x = a[i] - a[i+1]; if (x > 0) { ans[ti] += x; } } } 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, std.bigint; void main() { readln; auto os = readln.chomp; int n; foreach (o; os) { if (o == '+') { ++n; } else if (o == '-' && n > 0) { --n; } } writeln(n); }
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 int[](t); foreach (ti; 0..t) { auto n = RD!int; auto m = RD!int; auto r = RD!int; auto c = RD!int; ans[ti].chmax(r-1 + c-1); ans[ti].chmax(n-r + m-c); ans[ti].chmax(r-1 + m-c); ans[ti].chmax(n-r + c-1); } foreach (e; ans) writeln(e); stdout.flush; debug readln; }
D
void main() { auto N = ri; long res; foreach(i; 0..N+1) { if(i % 5 != 0 && i % 3 != 0) res += i; } res.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
void main() { string s = readln.chomp; writeln(s.canFind("AC") ? "Yes" : "No"); } 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; immutable mod = 4294967311L; void main(){ long x, y; int n, op; scanf("%d", &n); long[] inv = new long[1000_001]; inv[1] = 1; foreach(i; 2..inv.length){ inv[i] = mul(inv[mod%i], mod - mod/i); } foreach(_; 0..n){ scanf("%d%lld", &op, &y); if(op >= 3 && y < 0) x = -x, y = -y; if(op == 1) x += y; else if(op == 2) x -= y; else if(op == 3) x *= y; else if(op == 4) x = mul(x, inv[cast(int)y]); x %= mod; } if(x < 0) x += mod; if(x > int.max) x -= mod; writeln(x); } long mul(long x, long y){ return ( ((x*(y>>16)%mod)<<16) + (x*(y&0xffff)) ) % mod; }
D
import std.stdio; long div = 1_000_000_007; void main() { long n, m; scanf("%ld %ld\n", &n, &m); auto dp = new long[n+2]; dp[0] = 1; auto check = new bool[1_0000]; foreach(_; 0..m) { long a; scanf("%ld\n", &a); check[a] = true; } foreach(i; 0..n) { if (check[i]) continue; dp[i+1] = (dp[i+1] + dp[i]) % div; dp[i+2] = (dp[i+2] + dp[i]) % div; } dp[n].write; }
D
import std.stdio,std.conv,std.string,std.algorithm; void main(){ for( int i;;++i ){ auto arg = readln().chomp().to!int; if( !arg ) break; writeln("Case ",i+1,": ",arg ); } }
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 A, B, C, X; scanln(A); scanln(B); scanln(C); scanln(X); long[] as = [A, B, C]; long[] bs = [500, 100, 50]; long rec(long depth, long x) { if (depth == 3) return x == 0; long res = 0; foreach(i; 0..as[depth]+1) { if (i*bs[depth] > x) continue; res += rec(depth+1, x-i*bs[depth]); } return res; } rec(0, X).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
// Your code here! import std.stdio; import std.range; import std.array; import std.algorithm; import std.conv; import std.string; void main(){ auto input = readln().chomp().to!int; if(input <= 999){ writeln("ABC"); }else{ writeln("ABD"); } }
D