結果
問題 | No.579 3 x N グリッド上のサイクルのサイズ(hard) |
ユーザー | yosupot |
提出日時 | 2017-12-02 21:18:40 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 10 ms / 2,000 ms |
コード長 | 29,701 bytes |
コンパイル時間 | 2,212 ms |
コンパイル使用メモリ | 168,052 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-12 22:46:30 |
合計ジャッジ時間 | 4,739 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 2 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 3 ms
5,376 KB |
testcase_21 | AC | 3 ms
5,376 KB |
testcase_22 | AC | 4 ms
5,376 KB |
testcase_23 | AC | 4 ms
5,376 KB |
testcase_24 | AC | 3 ms
5,376 KB |
testcase_25 | AC | 3 ms
5,376 KB |
testcase_26 | AC | 3 ms
5,376 KB |
testcase_27 | AC | 3 ms
5,376 KB |
testcase_28 | AC | 3 ms
5,376 KB |
testcase_29 | AC | 4 ms
5,376 KB |
testcase_30 | AC | 4 ms
5,376 KB |
testcase_31 | AC | 4 ms
5,376 KB |
testcase_32 | AC | 3 ms
5,376 KB |
testcase_33 | AC | 4 ms
5,376 KB |
testcase_34 | AC | 4 ms
5,376 KB |
testcase_35 | AC | 3 ms
5,376 KB |
testcase_36 | AC | 3 ms
5,376 KB |
testcase_37 | AC | 3 ms
5,376 KB |
testcase_38 | AC | 4 ms
5,376 KB |
testcase_39 | AC | 4 ms
5,376 KB |
testcase_40 | AC | 6 ms
5,376 KB |
testcase_41 | AC | 5 ms
5,376 KB |
testcase_42 | AC | 5 ms
5,376 KB |
testcase_43 | AC | 5 ms
5,376 KB |
testcase_44 | AC | 6 ms
5,376 KB |
testcase_45 | AC | 5 ms
5,376 KB |
testcase_46 | AC | 6 ms
5,376 KB |
testcase_47 | AC | 5 ms
5,376 KB |
testcase_48 | AC | 5 ms
5,376 KB |
testcase_49 | AC | 6 ms
5,376 KB |
testcase_50 | AC | 6 ms
5,376 KB |
testcase_51 | AC | 5 ms
5,376 KB |
testcase_52 | AC | 6 ms
5,376 KB |
testcase_53 | AC | 5 ms
5,376 KB |
testcase_54 | AC | 6 ms
5,376 KB |
testcase_55 | AC | 6 ms
5,376 KB |
testcase_56 | AC | 5 ms
5,376 KB |
testcase_57 | AC | 6 ms
5,376 KB |
testcase_58 | AC | 5 ms
5,376 KB |
testcase_59 | AC | 6 ms
5,376 KB |
testcase_60 | AC | 9 ms
5,376 KB |
testcase_61 | AC | 10 ms
5,376 KB |
testcase_62 | AC | 9 ms
5,376 KB |
testcase_63 | AC | 10 ms
5,376 KB |
testcase_64 | AC | 9 ms
5,376 KB |
testcase_65 | AC | 10 ms
5,376 KB |
testcase_66 | AC | 10 ms
5,376 KB |
testcase_67 | AC | 9 ms
5,376 KB |
testcase_68 | AC | 9 ms
5,376 KB |
testcase_69 | AC | 10 ms
5,376 KB |
testcase_70 | AC | 10 ms
5,376 KB |
testcase_71 | AC | 10 ms
5,376 KB |
testcase_72 | AC | 10 ms
5,376 KB |
testcase_73 | AC | 9 ms
5,376 KB |
testcase_74 | AC | 9 ms
5,376 KB |
testcase_75 | AC | 10 ms
5,376 KB |
testcase_76 | AC | 10 ms
5,376 KB |
testcase_77 | AC | 10 ms
5,376 KB |
testcase_78 | AC | 10 ms
5,376 KB |
testcase_79 | AC | 9 ms
5,376 KB |
ソースコード
/+ dub.sdl: name "A" dependency "dcomp" version=">=0.7.4" +/ import std.stdio, std.algorithm, std.range, std.conv; // import dcomp.foundation, dcomp.scanner; // import dcomp.numeric.convolution; // import dcomp.modint; // import dcomp.modpoly; immutable int MD = 10^^9 + 7; alias Mint = ModInt!MD; alias MPol = ModPoly!MD; int main() { auto po = [ 0, 32, 316, 2292, 14422, 84744, 479004, 2638328, 14258574, 75940592, 399782668, 84795558, 786749020, 442043859, 352536615, 76576421, 744912747, 420315017, 25759333, 562730793, 424899366, 153177921, 250747498, 306910436, 324829483, 572545341, 104022619, 226237183, 421453002, 754280938, 291624319, 60437277, 297658752, 677142927, 63550828, 801541292, 683008492, 650348, 519624175, 715484025, 724658778, 152363657, 280344328, 892278238, 206785631, 227202296, 788486407, 392284243, 927772200, 781378846, 881515964, 905982211, 674841192, 139044658, 711210295, 384364637, 137653614, 441363040, 812818651, 929556368, 494420762, 802527485, 700803632, 461521718, 152786116, 688977792, 48724029, 642700933, 15567410, 246397043, 859581827, 685250826, 120226158, 551687712, 987163282, 422276494, 570671107, 813070470, 429968009, 849487586, 453150672, 606112895, 921636591, 961537190, 68995663, 873642098, 377371441, 101407285, 187434129, 180415383, 920712750, 544594592, 402649575, 549811430, 619506771, 426917748, 274013175, 615469131, 800944525, 925880089, ].map!(x => Mint(x)).array; auto pol = MPol(po); auto v = berlekampMassey(po); // writeln(v); long x; sc.read(x); auto z = nthMod(x, v); Mint sm = 0; foreach (i, d; po) { sm += d * z[i]; } writeln(sm); // for (int i: rng(int(v.size()))) { // sm += po[i] * z.freq(i); // } // cout << to_string(z) << endl; // cout << sm.v << endl;*/ return 0; } Scanner sc; static this() { sc = new Scanner(stdin); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modpoly.d */ // module dcomp.modpoly; // import dcomp.numeric.primitive; // import dcomp.numeric.convolution; // import dcomp.modint; // import dcomp.container.stack; struct ModPoly(uint MD) if (MD < int.max) { alias Mint = ModInt!MD; import std.algorithm : min, max, reverse; Stack!Mint d; void shrink() { while (!d.empty && d.back == Mint(0)) d.removeBack; } @property size_t length() const { return d.length; } @property inout(Mint)[] data() inout { return d.data; } this(in Mint[] v) { d = v.dup; shrink(); } const(Mint) opIndex(size_t i) const { if (i < d.length) return d[i]; return Mint(0); } void opIndexAssign(Mint x, size_t i) { if (i < d.length) { d[i] = x; shrink(); return; } if (x == Mint(0)) return; while (d.length < i) d.insertBack(Mint(0)); d.insertBack(x); return; } ModPoly opBinary(string op : "+")(in ModPoly r) const { size_t N = length, M = r.length; Mint[] res = new Mint[max(N, M)]; foreach (i; 0..max(N, M)) res[i] = this[i] + r[i]; return ModPoly(res); } ModPoly opBinary(string op : "-")(in ModPoly r) const { size_t N = length, M = r.length; Mint[] res = new Mint[max(N, M)]; foreach (i; 0..max(N, M)) res[i] = this[i] - r[i]; return ModPoly(res); } ModPoly opBinary(string op : "*")(in ModPoly r) const { size_t N = length, M = r.length; if (min(N, M) == 0) return ModPoly(); return ModPoly(multiply(data, r.data)); } ModPoly opBinary(string op : "*")(in Mint r) const { Mint[] res = new Mint[length]; foreach (i; 0..length) res[i] = this[i]*r; return ModPoly(res); } ModPoly opBinary(string op : "/")(in ModPoly r) const { size_t B = max(1, length, r.length); return divWithInv(r.inv(B), B); } ModPoly opBinary(string op : "%")(in ModPoly r) const { return *this - y * div(y); } ModPoly opBinary(string op : "<<")(size_t n) const { Mint[] res = new Mint[n+length]; foreach (i; 0..length) res[i+n] = this[i]; return ModPoly(res); } ModPoly opBinary(string op : ">>")(size_t n) const { if (length <= n) return ModPoly(); Mint[] res = new Mint[length-n]; foreach (i; n..length) res[i-n] = this[i]; return ModPoly(res); } ModPoly opOpAssign(string op)(in ModPoly r) { return mixin("this=this"~op~"r"); } ModPoly strip(size_t n) const { auto res = d.data.dup; res = res[0..min(n, length)]; return ModPoly(res); } ModPoly divWithInv(in ModPoly ir, size_t B) const { return (this * ir) >> (B-1); } ModPoly remWithInv(in ModPoly r, in ModPoly ir, size_t B) const { return this - r * divWithInv(ir, B); } ModPoly rev(ptrdiff_t n = -1) const { auto res = d.data.dup; if (n != -1) res = res[0..n]; reverse(res); return ModPoly(res); } ModPoly inv(size_t n) const { assert(length >= 1); assert(n >= length-1); ModPoly c = rev(); ModPoly d = ModPoly([Mint(1)/c[0]]); for (ptrdiff_t i = 1; i+length-2 < n; i *= 2) { d = (d * (ModPoly([Mint(2)]) - c*d)).strip(2*i); } return d.rev(n+1-length); } string toString() { import std.conv : to; import std.range : join; string[] l = new string[length]; foreach (i; 0..length) { l[i] = (this[i]).toString ~ "x^" ~ i.to!string; } return l.join(" + "); } } ModPoly!MD nthMod(uint MD)(ulong n, in ModPoly!MD mod) { import core.bitop : bsr; alias Mint = ModInt!MD; assert(mod.length); size_t B = mod.length * 2 - 1; auto modInv = mod.inv(B); auto p = ModPoly!MD([Mint(1)]); if (n == 0) return p; auto m = bsr(n); foreach_reverse(i; 0..m+1) { if (n & (1L<<i)) { p = (p<<1).remWithInv(mod, modInv, B); } if (i) { p = (p*p).remWithInv(mod, modInv, B); } } return p; } ModPoly!MD berlekampMassey(uint MD)(in ModInt!MD[] s) { alias Mint = ModInt!MD; Mint[] b = [Mint(-1)], c = [Mint(-1)]; Mint y = 1; foreach (ed; 1..s.length+1) { auto L = c.length, M = b.length; Mint x = 0; foreach (i; 0..L) { x += c[i] * s[ed-L+i]; } b ~= Mint(0); M++; if (x == Mint(0)) { continue; } auto freq = x/y; if (L < M) { auto tmp = c; import std.range : repeat, take, array; c = Mint(0).repeat.take(M-L).array ~ c; foreach (i; 0..M) { c[M-1-i] -= freq*b[M-1-i]; } b = tmp; y = x; } else { foreach (i; 0..M) { c[L-1-i] -= freq*b[M-1-i]; } } } return ModPoly!MD(c); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modint.d */ // module dcomp.modint; // import dcomp.numeric.primitive; struct ModInt(uint MD) if (MD < int.max) { import std.conv : to; uint v; this(int v) {this(long(v));} this(long v) {this.v = (v%MD+MD)%MD;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {ModInt m; m.v = x; return m;} auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);} auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");} static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);} string toString() const {return v.to!string;} } struct DModInt(string name) { import std.conv : to; static uint MD; uint v; this(int v) {this(long(v));} this(long v) {this.v = ((v%MD+MD)%MD).to!uint;} static auto normS(uint x) {return (x<MD)?x:x-MD;} static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;} auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));} auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));} auto opBinary(string op:"*")(DModInt r) const {return make((ulong(v)*r.v%MD).to!uint);} auto opBinary(string op:"/")(DModInt r) const {return this*inv(r);} auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");} static DModInt inv(DModInt x) { return DModInt(extGcd!int(x.v, MD)[0]); } string toString() {return v.to!string;} } template isModInt(T) { const isModInt = is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s); } T[] factTable(T)(size_t length) if (isModInt!T) { import std.range : take, recurrence; import std.array : array; return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array; } T[] invFactTable(T)(size_t length) if (isModInt!T) { import std.algorithm : map, reduce; import std.range : take, recurrence, iota; import std.array : array; auto res = new T[length]; res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b"; foreach_reverse (i, v; res[0..$-1]) { res[i] = res[i+1] * T(i+1); } return res; } T[] invTable(T)(size_t length) if (isModInt!T) { auto f = factTable!T(length); auto invf = invFactTable!T(length); auto res = new T[length]; foreach (i; 1..length) { res[i] = invf[i] * f[i-1]; } return res; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/convolution.d */ // module dcomp.numeric.convolution; T[] zeta(T)(T[] v, bool rev) { import core.bitop : bsr; int n = bsr(v.length); assert(1<<n == v.length); foreach (fe; 0..n) { foreach (i, _; v) { if (i & (1<<fe)) continue; if (!rev) { v[i] += v[i|(1<<fe)]; } else { v[i] -= v[i|(1<<fe)]; } } } return v; } T[] hadamard(T)(T[] v, bool rev) { import core.bitop : bsr; int n = bsr(v.length); assert(1<<n == v.length); foreach (fe; 0..n) { foreach (i, _; v) { if (i & (1<<fe)) continue; auto l = v[i], r = v[i|(1<<fe)]; if (!rev) { v[i] = l+r; v[i|(1<<fe)] = l-r; } else { v[i] = (l+r)/2; v[i|(1<<fe)] = (l-r)/2; } } } return v; } import std.complex; double[] fftSinList(size_t S) { import std.math : PI, sin; assert(2 <= S); size_t N = 1<<S; static double[][30] buf; if (!buf[S].length) { buf[S] = new double[3*N/4+1]; foreach (i; 0..N/4+1) { buf[S][i] = sin(i*2*double(PI)/N); buf[S][N/2-i] = buf[S][i]; buf[S][N/2+i] = -buf[S][i]; } } return buf[S]; } void fft(bool type)(Complex!double[] c) { import std.algorithm : swap; import core.bitop : bsr; alias P = Complex!double; size_t N = c.length; assert(N); size_t S = bsr(N); assert(1<<S == N); if (S == 1) { auto x = c[0], y = c[1]; c[0] = x+y; c[1] = x-y; return; } auto rot = fftSinList(S); P[] a = c.dup, b = new P[c.length]; foreach (i; 1..S+1) { size_t W = 1<<(S-i); for (size_t y = 0; y < N/2; y += W) { P now = P(rot[y + N/4], rot[y]); if (type) now = conj(now); foreach (x; 0..W) { auto l = a[y<<1 | x]; auto r = now * a[y<<1 | x | W]; b[y | x] = l+r; b[y | x | N>>1] = l-r; } } swap(a, b); } c[] = a[]; } double[] multiply(in double[] a, in double[] b) { alias P = Complex!double; size_t A = a.length, B = b.length; if (!A || !B) return []; size_t lg = 1; while ((1<<lg) < A+B-1) lg++; size_t N = 1<<lg; P[] d = new P[N]; d[] = P(0, 0); foreach (i; 0..A) d[i].re = a[i]; foreach (i; 0..B) d[i].im = b[i]; fft!false(d); foreach (i; 0..N/2+1) { auto j = i ? (N-i) : 0; P x = P(d[i].re+d[j].re, d[i].im-d[j].im); P y = P(d[i].im+d[j].im, -d[i].re+d[j].re); d[i] = x * y / 4; if (i != j) d[j] = conj(d[i]); } fft!true(d); double[] c = new double[A+B-1]; foreach (i; 0..A+B-1) { c[i] = d[i].re / N; } return c; } // import dcomp.modint, dcomp.numeric.primitive; void nft(uint G, bool type, Mint)(Mint[] c) { import std.algorithm : swap; import core.bitop : bsr; size_t N = c.length; assert(N); size_t S = bsr(N); assert(1<<S == N); Mint[] a = c.dup, b = new Mint[N]; foreach (i; 1..S+1) { size_t W = 1<<(S-i); Mint base = pow(Mint(G), Mint(-1).v/(1<<i)); if (type) base = Mint(1)/base; Mint now = 1; for (size_t y = 0; y < N/2; y += W) { foreach (x; 0..W) { auto l = a[y<<1 | x]; auto r = now * a[y<<1 | x | W]; b[y | x] = l+r; b[y | x | N>>1] = l-r; } now *= base; } swap(a, b); } c[] = a[]; } Mint[] multiply(uint G, Mint)(in Mint[] a, in Mint[] b) { size_t A = a.length, B = b.length; if (!A || !B) return []; size_t lg = 1; while ((1<<lg) < A+B-1) lg++; size_t N = 1<<lg; Mint[] _a = new Mint[N]; Mint[] _b = new Mint[N]; foreach (i; 0..A) _a[i] = a[i]; foreach (i; 0..B) _b[i] = b[i]; nft!(G, false)(_a); nft!(G, false)(_b); foreach (i; 0..N) _a[i] *= _b[i]; nft!(G, true)(_a); Mint[] c = new Mint[A+B-1]; Mint iN = Mint(1) / Mint(N); foreach (i; 0..A+B-1) { c[i] = _a[i] * iN; } return c; } Mint[] multiply(Mint, size_t M = 3, size_t W = 10)(in Mint[] a, in Mint[] b) if (isModInt!Mint) { import std.math : round; alias P = Complex!double; size_t A = a.length, B = b.length; if (!A || !B) return []; auto N = A + B - 1; size_t lg = 1; while ((1<<lg) < N) lg++; N = 1<<lg; P[][M] x, y; P[] w = new P[N]; foreach (ph; 0..M) { x[ph] = new P[N]; y[ph] = new P[N]; w[] = P(0, 0); foreach (i; 0..A) w[i].re = (a[i].v >> (ph*W)) % (1<<W); foreach (i; 0..B) w[i].im = (b[i].v >> (ph*W)) % (1<<W); fft!false(w); foreach (i; 0..N) w[i] *= 0.5; foreach (i; 0..N) { auto j = i ? N-i : 0; x[ph][i] = P(w[i].re+w[j].re, w[i].im-w[j].im); y[ph][i] = P(w[i].im+w[j].im, -w[i].re+w[j].re); } } Mint[] c = new Mint[A+B-1]; Mint basel = 1, baser = pow(Mint(1<<W), M); P[] z = new P[N]; foreach (ph; 0..M) { z[] = P(0, 0); foreach (af; 0..ph+1) { auto bf = ph - af; foreach (i; 0..N) { z[i] += x[af][i] * y[bf][i]; } } foreach (af; ph+1..M) { auto bf = ph + M - af; foreach (i; 0..N) { z[i] += x[af][i] * y[bf][i] * P(0, 1); } } fft!true(z); foreach (i; 0..A+B-1) { z[i] *= 1.0/N; c[i] += Mint(cast(long)(round(z[i].re)))*basel; c[i] += Mint(cast(long)(round(z[i].im)))*baser; } basel *= Mint(1<<W); baser *= Mint(1<<W); } return c; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */ // module dcomp.numeric.primitive; import std.traits; import std.bigint; Unqual!T pow(T, U)(T x, U n) if (!isFloatingPoint!T && (isIntegral!U || is(U == BigInt))) { return pow(x, n, T(1)); } Unqual!T pow(T, U, V)(T x, U n, V e) if ((isIntegral!U || is(U == BigInt)) && is(Unqual!T == Unqual!V)) { Unqual!T b = x, v = e; Unqual!U m = n; while (m) { if (m & 1) v *= b; b *= b; m /= 2; } return v; } T powMod(T, U, V)(T x, U n, V md) if (isIntegral!U || is(U == BigInt)) { T r = T(1); while (n) { if (n & 1) r = (r*x)%md; x = (x*x)%md; n >>= 1; } return r % md; } ulong ulongPowMod(U)(ulong x, U n, ulong md) if (isIntegral!U || is(U == BigInt)) { // import dcomp.int128; x %= md; ulong r = 1; while (n) { if (n & 1) { r = mul128(r, x).mod128(md); } x = mul128(x, x).mod128(md); n >>= 1; } return r % md; } T lcm(T)(in T a, in T b) { import std.numeric : gcd; return a / gcd(a,b) * b; } T[3] extGcd(T)(in T a, in T b) if (!isIntegral!T || isSigned!T) { if (b==0) { return [T(1), T(0), a]; } else { auto e = extGcd(b, a%b); return [e[1], e[0]-a/b*e[1], e[2]]; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/ldc/inline.d */ // module dcomp.ldc.inline; version(LDC) { pragma(LDC_inline_ir) R inlineIR(string s, R, P...)(P); } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; // import dcomp.container.stack; 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 read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */ // module dcomp.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 core.bitop : popcnt; static if (!__traits(compiles, popcnt(ulong.max))) { public import core.bitop : popcnt; int popcnt(ulong v) { return popcnt(cast(uint)(v)) + popcnt(cast(uint)(v>>32)); } } bool poppar(ulong v) { v^=v>>1; v^=v>>2; v&=0x1111111111111111UL; v*=0x1111111111111111UL; return ((v>>60) & 1) != 0; } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stack.d */ // module dcomp.container.stack; struct StackPayload(T, size_t MIN = 4) { import core.exception : RangeError; import std.algorithm : max; import std.conv; import std.range.primitives : ElementEncodingType; import core.stdc.string : memcpy; private T* _data; private uint len, cap; bool empty() const { return len == 0; } @property size_t length() const { return len; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout { return _data[i]; } ref inout(T) front() inout { return _data[0]; } ref inout(T) back() inout { assert(len); return _data[len-1]; } void reserve(size_t nlen) { import core.memory : GC; if (nlen <= cap) return; void* nx = GC.malloc(nlen * T.sizeof); cap = nlen.to!uint; if (len) memcpy(nx, _data, len * T.sizeof); _data = cast(T*)(nx); } void free() { import core.memory : GC; GC.free(_data); } void opOpAssign(string op : "~")(T item) { if (len == cap) { reserve(max(MIN, cap*2)); } _data[len++] = item; } void insertBack(T item) { this ~= item; } void removeBack() { len--; } void clear() { len = 0; } inout(T)[] data() inout { return (_data) ? _data[0..len] : null; } static struct RangeT(A) { import std.traits : CopyTypeQualifiers; alias E = CopyTypeQualifiers!(A, T); A *p; size_t a, b; @property bool empty() const { return b <= a; } @property size_t length() const { return b-a; } @property RangeT save() { return RangeT(p, a, b); } @property RangeT!(const A) save() const { return typeof(return)(p, a, b); } alias opDollar = length; @property ref inout(E) front() inout { return (*p)[a]; } @property ref inout(E) back() inout { return (*p)[b-1]; } void popFront() { version(assert) if (empty) throw new RangeError(); a++; } void popBack() { version(assert) if (empty) throw new RangeError(); b--; } ref inout(E) opIndex(size_t i) inout { return (*p)[i]; } RangeT opSlice() { return this.save; } RangeT opSlice(size_t i, size_t j) { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } RangeT!(const A) opSlice() const { return this.save; } RangeT!(const A) opSlice(size_t i, size_t j) const { version(assert) if (i > j || a + j > b) throw new RangeError(); return typeof(return)(p, a+i, a+j); } } alias Range = RangeT!(StackPayload!T); alias ConstRange = RangeT!(const StackPayload!T); alias ImmutableRange = RangeT!(immutable StackPayload!T); } struct Stack(T) { import core.exception : RangeError; import core.memory : GC; import std.range : ElementType, isInputRange; import std.traits : isImplicitlyConvertible; alias Payload = StackPayload!T; alias Range = Payload.Range; alias ConstRange = Payload.ConstRange; alias ImmutableRange = Payload.ImmutableRange; Payload* p; private void I() { if (!p) p = new Payload(); } private void C() const { version(assert) if (!p) throw new RangeError(); } private this(Payload* p) { this.p = p; } this(U)(U[] values...) if (isImplicitlyConvertible!(U, T)) { p = new Payload(); foreach (v; values) { insertBack(v); } } this(Range)(Range r) if (isInputRange!Range && isImplicitlyConvertible!(ElementType!Range, T) && !is(Range == T[])) { p = new Payload(); foreach (v; r) { insertBack(v); } } static Stack make() { return Stack(new Payload()); } @property bool havePayload() const { return (p !is null); } @property bool empty() const { return (!havePayload || p.empty); } @property size_t length() const { return (havePayload ? p.length : 0); } @property inout(T)[] data() inout {C; return (!p) ? [] : p.data; } alias opDollar = length; ref inout(T) opIndex(size_t i) inout {C; return (*p)[i]; } ref inout(T) front() inout {C; return (*p)[0]; } ref inout(T) back() inout {C; return (*p)[$-1]; } void clear() { if (p) p.clear(); } void insertBack(T v) {I; p.insertBack(v); } alias stableInsertBack = insertBack; void removeBack() {C; p.removeBack(); } Range opSlice() {I; return Range(p, 0, length); } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/int128.d */ // module dcomp.int128; // import dcomp.array; version(LDC) { // import dcomp.ldc.inline; } version(LDC) version(X86_64) { version = LDC_IR; } ulong[2] mul128(ulong a, ulong b) { ulong[2] res; version(LDC_IR) { ulong upper, lower; inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = mul i128 %r1, %r0 %r3 = trunc i128 %r2 to i64 %r4 = lshr i128 %r2, 64 %r5 = trunc i128 %r4 to i64 store i64 %r3, i64* %2 store i64 %r5, i64* %3`, void)(a, b, &lower, &upper); return [lower, upper]; } else version(D_InlineAsm_X86_64) { ulong upper, lower; asm { mov RAX, a; mul b; mov lower, RAX; mov upper, RDX; } return [lower, upper]; } else { ulong B = 2UL^^32; ulong[2] a2 = [a % B, a / B]; ulong[2] b2 = [b % B, b / B]; ulong[4] c; foreach (i; 0..2) { foreach (j; 0..2) { c[i+j] += a2[i] * b2[j] % B; c[i+j+1] += a2[i] * b2[j] / B; } } foreach (i; 0..3) { c[i+1] += c[i] / B; c[i] %= B; } return [c[0] + c[1] * B, c[2] + c[3] * B]; } } ulong div128(ulong[2] a, ulong b) { version(LDC_IR) { return inlineIR!(` %r0 = zext i64 %0 to i128 %r1 = zext i64 %1 to i128 %r2 = shl i128 %r1, 64 %r3 = add i128 %r0, %r2 %r4 = zext i64 %2 to i128 %r5 = udiv i128 %r3, %r4 %r6 = trunc i128 %r5 to i64 ret i64 %r6`,ulong)(a[0], a[1], b); } else version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RAX; } return res; } else { if (b == 1) return a[0]; while (!(b & (1UL << 63))) { a[1] <<= 1; if (a[0] & (1UL << 63)) a[1] |= 1; a[0] <<= 1; b <<= 1; } ulong ans = 0; foreach (i; 0..64) { bool up = (a[1] & (1UL << 63)) != 0; a[1] <<= 1; if (a[0] & (1UL << 63)) a[1] |= 1; a[0] <<= 1; ans <<= 1; if (up || b <= a[1]) { a[1] -= b; ans++; } } return ans; } } ulong mod128(ulong[2] a, ulong b) { version(D_InlineAsm_X86_64) { ulong upper = a[1], lower = a[0]; ulong res; asm { mov RDX, upper; mov RAX, lower; div b; mov res, RDX; } return res; } else { return a[0] - div128(a, b) * b; } } /* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */ // module dcomp.array; T[N] fixed(T, size_t N)(T[N] a) {return a;} /* This source code generated by dcomp and include dcomp's source code. dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp) dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt) */