This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/fps/formal_power_series_ll.hpp"
long long
型の範囲で扱う形式的冪級数です.
実装されている機能が mint
版に比べて絞られています.
計算量や使用法は mint
版と同じなのでそちらをご覧ください.
実装されている機能は以下のとおりです.
fps = fps;
-fps;
fps += ll;
fps -= ll;
fps *= ll;
fps /= ll;
fps += fps;
fps -= fps;
fps *= fps;
fps + ll;
fps - ll;
fps * ll;
fps / ll;
fps + fps;
fps - fps;
fps * fps;
fps << int;
fps >> int;
void fps.onemul(int d, ll c, int deg = -1);
void fps.onediv(int d, ll c, int deg = -1);
void fps.shrink();
fps fps.rev();
fps fps.pre(int deg);
ll fps.eval(ll a);
#pragma once
#include "../template/template.hpp"
#include "../convolution/convolution_ll.hpp"
template <typename T>
struct FormalPowerSeriesLL : vector<T> {
using vector<T>::vector;
using F = FormalPowerSeriesLL;
F& operator=(const vector<T>& g) {
const int n = (*this).size();
const int m = g.size();
if(n < m) (*this).resize(m);
for(int i = 0; i < m; ++i) (*this)[i] = g[i];
return (*this);
}
F& operator-() {
const int n = (*this).size();
for(int i = 0; i < n; ++i) (*this)[i] *= -1;
return (*this);
}
F& operator+=(const F& g) {
const int n = (*this).size();
const int m = g.size();
if(n < m) (*this).resize(m);
for(int i = 0; i < m; ++i) (*this)[i] += g[i];
return (*this);
}
F& operator+=(const T& r) {
if((*this).empty()) (*this).resize(1, T(0));
(*this)[0] += r;
return (*this);
}
F& operator-=(const F& g) {
const int n = (*this).size();
const int m = g.size();
if(n < m) (*this).resize(m);
for(int i = 0; i < m; ++i) (*this)[i] -= g[i];
return (*this);
}
F& operator-=(const T& r) {
if((*this).empty()) (*this).resize(1, T(0));
(*this)[0] -= r;
return (*this);
}
F& operator*=(const F& g) {
(*this) = convolution_ll((*this), g);
return (*this);
}
F& operator*=(const T& r) {
const int n = (*this).size();
for(int i = 0; i < n; ++i) (*this)[i] *= r;
return (*this);
}
F& operator/=(const T& r) {
const int n = (*this).size();
for(int i = 0; i < (int)n; ++i) (*this)[i] /= r;
return (*this);
}
F operator*(const T& g) const {
return F(*this) *= g;
}
F operator-(const T& g) const {
return F(*this) -= g;
}
F operator+(const T& g) const {
return F(*this) += g;
}
F operator/(const T& g) const {
return F(*this) /= g;
}
F operator*(const F& g) const {
return F(*this) *= g;
}
F operator-(const F& g) const {
return F(*this) -= g;
}
F operator+(const F& g) const {
return F(*this) += g;
}
F operator<<(const int d) const {
F ret(*this);
ret.insert(ret.begin(), d, T(0));
return ret;
}
F operator>>(const int d) const {
const int n = (*this).size();
if(n <= d) return {};
F ret(*this);
ret.erase(ret.begin(), ret.begin() + d);
return ret;
}
void shrink() {
while(!(*this).empty() and (*this).back() == T(0)) (*this).pop_back();
}
F rev() const {
F ret(*this);
reverse(begin(ret), end(ret));
return ret;
}
F pre(const int deg) const {
assert(deg >= 0);
F ret(begin(*this), begin(*this) + min((int)(*this).size(), deg));
if((int)ret.size() < deg) ret.resize(deg);
return ret;
}
T eval(const T& a) const {
const int n = (*this).size();
T x = 1, ret = 0;
for(int i = 0; i < n; ++i) {
ret += (*this)[i] * x;
x *= a;
}
return ret;
}
void onemul(const int d, const T& c, int deg = -1) {
assert(deg >= -1);
const int n = (*this).size();
if(deg == -1) deg = n + d;
if(deg > n) (*this).resize(deg);
for(int i = deg - d - 1; i >= 0; --i) {
(*this)[i + d] += (*this)[i] * c;
}
}
void onediv(const int d, const T& c, int deg = -1) {
assert(deg >= -1);
const int n = (*this).size();
if(deg == -1) deg = n;
if(deg > n) (*this).resize(deg + 1);
for(int i = 0; i < deg - d; ++i) {
(*this)[i + d] -= (*this)[i] * c;
}
}
};
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/template/static_modint.hpp"
template <uint32_t m>
struct StaticModint {
using mint = StaticModint;
static constexpr uint32_t mod() {
return m;
}
static constexpr mint raw(const uint32_t v) {
mint a;
a._v = v;
return a;
}
constexpr StaticModint()
: _v(0) {}
template <class T>
constexpr StaticModint(const T& v) {
static_assert(is_integral_v<T>);
if constexpr(is_signed_v<T>) {
int64_t x = int64_t(v % int64_t(m));
if(x < 0) x += m;
_v = uint32_t(x);
} else _v = uint32_t(v % m);
}
constexpr uint32_t val() const {
return _v;
}
constexpr mint& operator++() {
return *this += 1;
}
constexpr mint& operator--() {
return *this -= 1;
}
constexpr mint operator++(int) {
mint res = *this;
++*this;
return res;
}
constexpr mint operator--(int) {
mint res = *this;
--*this;
return res;
}
constexpr mint& operator+=(mint rhs) {
if(_v >= m - rhs._v) _v -= m;
_v += rhs._v;
return *this;
}
constexpr mint& operator-=(mint rhs) {
if(_v < rhs._v) _v += m;
_v -= rhs._v;
return *this;
}
constexpr mint& operator*=(mint rhs) {
return *this = *this * rhs;
}
constexpr mint& operator/=(mint rhs) {
return *this *= rhs.inv();
}
constexpr mint operator+() const {
return *this;
}
constexpr mint operator-() const {
return mint{} - *this;
}
constexpr mint pow(long long n) const {
assert(0 <= n);
if(n == 0) return 1;
mint x = *this, r = 1;
while(1) {
if(n & 1) r *= x;
n >>= 1;
if(n == 0) return r;
x *= x;
}
}
constexpr mint inv() const {
if constexpr(prime) {
assert(_v);
return pow(m - 2);
} else {
const auto eg = inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend constexpr mint operator+(mint lhs, mint rhs) {
return lhs += rhs;
}
friend constexpr mint operator-(mint lhs, mint rhs) {
return lhs -= rhs;
}
friend constexpr mint operator*(mint lhs, mint rhs) {
return uint64_t(lhs._v) * rhs._v;
}
friend constexpr mint operator/(mint lhs, mint rhs) {
return lhs /= rhs;
}
friend constexpr bool operator==(mint lhs, mint rhs) {
return lhs._v == rhs._v;
}
friend constexpr bool operator!=(mint lhs, mint rhs) {
return lhs._v != rhs._v;
}
friend istream& operator>>(istream& in, mint& x) {
long long a;
in >> a;
x = a;
return in;
}
friend ostream& operator<<(ostream& out, const mint& x) {
return out << x.val();
}
private:
uint32_t _v = 0;
static constexpr bool prime = []() -> bool {
if(m == 1) return 0;
if(m == 2 or m == 7 or m == 61) return 1;
if(m % 2 == 0) return 0;
uint32_t d = m - 1;
while(d % 2 == 0) d /= 2;
for(uint32_t a : {2, 7, 61}) {
uint32_t t = d;
mint y = mint(a).pow(t);
while(t != m - 1 && y != 1 && y != m - 1) {
y *= y;
t <<= 1;
}
if(y != m - 1 && t % 2 == 0) return 0;
}
return 1;
}();
static constexpr pair<int32_t, int32_t> inv_gcd(const int32_t a, const int32_t b) {
if(a == 0) return {b, 0};
int32_t s = b, t = a, m0 = 0, m1 = 1;
while(t) {
const int32_t u = s / t;
s -= t * u;
m0 -= m1 * u;
swap(s, t);
swap(m0, m1);
}
if(m0 < 0) m0 += b / s;
return {s, m0};
}
};
using modint998244353 = StaticModint<998244353>;
using modint1000000007 = StaticModint<1000000007>;
#line 3 "src/math/pow_mod.hpp"
constexpr long long pow_mod(long long x, long long n, const long long mod) {
assert(n >= 0 and mod >= 1);
x %= mod;
if(x < 0) x += mod;
long long res = 1;
while(n > 0) {
if(n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
#line 4 "src/math/primitive_root.hpp"
constexpr int primitive_root(const int m) {
if(m == 2) return 1;
if(m == 167772161) return 3;
if(m == 469762049) return 3;
if(m == 754974721) return 11;
if(m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while(x % 2 == 0) x /= 2;
for(int i = 3; (long long)(i)*i <= x; i += 2) {
if(x % i == 0) {
divs[cnt++] = i;
while(x % i == 0) {
x /= i;
}
}
}
if(x > 1) {
divs[cnt++] = x;
}
for(int g = 2;; ++g) {
bool ok = true;
for(int i = 0; i < cnt; ++i) {
if(pow_mod(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if(ok) return g;
}
}
#line 4 "src/convolution/convolution.hpp"
constexpr int countr_zero(const unsigned int n) {
int res = 0;
while(!(n & (1 << res))) ++res;
return res;
}
template <typename mint, int g = primitive_root(mint::mod())>
struct FFT_Info {
static constexpr int rank2 = countr_zero(mint::mod() - 1);
array<mint, rank2 + 1> root;
array<mint, rank2 + 1> iroot;
array<mint, max(0, rank2 - 2 + 1)> rate2;
array<mint, max(0, rank2 - 2 + 1)> irate2;
array<mint, max(0, rank2 - 3 + 1)> rate3;
array<mint, max(0, rank2 - 3 + 1)> irate3;
FFT_Info() {
root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
iroot[rank2] = root[rank2].inv();
for(int i = rank2 - 1; i >= 0; --i) {
root[i] = root[i + 1] * root[i + 1];
iroot[i] = iroot[i + 1] * iroot[i + 1];
}
{
mint prod = 1, iprod = 1;
for(int i = 0; i <= rank2 - 2; ++i) {
rate2[i] = root[i + 2] * prod;
irate2[i] = iroot[i + 2] * iprod;
prod *= iroot[i + 2];
iprod *= root[i + 2];
}
}
{
mint prod = 1, iprod = 1;
for(int i = 0; i <= rank2 - 3; ++i) {
rate3[i] = root[i + 3] * prod;
irate3[i] = iroot[i + 3] * iprod;
prod *= iroot[i + 3];
iprod *= root[i + 3];
}
}
}
};
template <typename mint>
void butterfly(vector<mint>& a) {
const int n = (int)a.size();
const int h = __builtin_ctz((unsigned int)n);
static const FFT_Info<mint> info;
int len = 0;
while(len < h) {
if(h - len == 1) {
const int p = 1 << (h - len - 1);
mint rot = 1;
for(int s = 0; s < (1 << len); ++s) {
const int offset = s << (h - len);
for(int i = 0; i < p; ++i) {
const auto l = a[i + offset];
const auto r = a[i + offset + p] * rot;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
if(s + 1 != (1 << len)) rot *= info.rate2[__builtin_ctz(~(unsigned int)(s))];
}
++len;
} else {
const int p = 1 << (h - len - 2);
mint rot = 1, imag = info.root[2];
for(int s = 0; s < (1 << len); ++s) {
const mint rot2 = rot * rot;
const mint rot3 = rot2 * rot;
const int offset = s << (h - len);
for(int i = 0; i < p; ++i) {
const auto mod2 = 1ULL * mint::mod() * mint::mod();
const auto a0 = 1ULL * a[i + offset].val();
const auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
const auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
const auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
const auto a1na3imag = 1ULL * mint(a1 + mod2 - a3).val() * imag.val();
const auto na2 = mod2 - a2;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
}
if(s + 1 != (1 << len)) rot *= info.rate3[__builtin_ctz(~(unsigned int)(s))];
}
len += 2;
}
}
}
template <typename mint>
void butterfly_inv(vector<mint>& a) {
const int n = (int)a.size();
const int h = __builtin_ctz((unsigned int)n);
static const FFT_Info<mint> info;
int len = h;
while(len) {
if(len == 1) {
const int p = 1 << (h - len);
mint irot = 1;
for(int s = 0; s < (1 << (len - 1)); ++s) {
const int offset = s << (h - len + 1);
for(int i = 0; i < p; ++i) {
const auto l = a[i + offset];
const auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * irot.val();
}
if(s + 1 != (1 << (len - 1))) irot *= info.irate2[__builtin_ctz(~(unsigned int)(s))];
}
--len;
} else {
const int p = 1 << (h - len);
mint irot = 1, iimag = info.iroot[2];
for(int s = 0; s < (1 << (len - 2)); ++s) {
const mint irot2 = irot * irot;
const mint irot3 = irot2 * irot;
const int offset = s << (h - len + 2);
for(int i = 0; i < p; ++i) {
const auto a0 = 1ULL * a[i + offset + 0 * p].val();
const auto a1 = 1ULL * a[i + offset + 1 * p].val();
const auto a2 = 1ULL * a[i + offset + 2 * p].val();
const auto a3 = 1ULL * a[i + offset + 3 * p].val();
const auto a2na3iimag = 1ULL * mint((mint::mod() + a2 - a3) * iimag.val()).val();
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + 1 * p] = (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
a[i + offset + 2 * p] = (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) * irot2.val();
a[i + offset + 3 * p] = (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) * irot3.val();
}
if(s + 1 != (1 << (len - 2))) irot *= info.irate3[__builtin_ctz(~(unsigned int)(s))];
}
len -= 2;
}
}
}
template <typename mint>
vector<mint> convolution_naive(const vector<mint>& a, const vector<mint>& b) {
const int n = (int)a.size(), m = (int)b.size();
vector<mint> res(n + m - 1);
if(n < m) {
for(int j = 0; j < m; ++j) {
for(int i = 0; i < n; ++i) {
res[i + j] += a[i] * b[j];
}
}
} else {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
res[i + j] += a[i] * b[j];
}
}
}
return res;
}
template <typename mint>
vector<mint> convolution(vector<mint> a, vector<mint> b) {
const int n = (int)a.size(), m = (int)b.size();
if(n == 0 or m == 0) return {};
int z = 1;
while(z < n + m - 1) z *= 2;
assert((mint::mod() - 1) % z == 0);
if(min(n, m) <= 60) return convolution_naive(a, b);
a.resize(z);
b.resize(z);
butterfly(a);
butterfly(b);
for(int i = 0; i < z; ++i) a[i] *= b[i];
butterfly_inv(a);
a.resize(n + m - 1);
const mint iz = mint(z).inv();
for(int i = 0; i < n + m - 1; ++i) a[i] *= iz;
return a;
}
#line 5 "src/convolution/convolution_ll.hpp"
vector<long long> convolution_ll(const vector<long long>& a, const vector<long long>& b) {
const int n = (int)a.size(), m = (int)b.size();
if(!n or !m) return {};
static constexpr unsigned long long MOD1 = 754974721;
static constexpr unsigned long long MOD2 = 167772161;
static constexpr unsigned long long MOD3 = 469762049;
static constexpr unsigned long long M2M3 = MOD2 * MOD3;
static constexpr unsigned long long M1M3 = MOD1 * MOD3;
static constexpr unsigned long long M1M2 = MOD1 * MOD2;
static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
static constexpr unsigned long long i1 = 190329765;
static constexpr unsigned long long i2 = 58587104;
static constexpr unsigned long long i3 = 187290749;
static constexpr int MAX_AB_BIT = 24;
assert(n + m - 1 <= (1 << MAX_AB_BIT));
using mint1 = StaticModint<MOD1>;
using mint2 = StaticModint<MOD2>;
using mint3 = StaticModint<MOD3>;
vector<mint1> a1(n), b1(m);
vector<mint2> a2(n), b2(m);
vector<mint3> a3(n), b3(m);
for(int i = 0; i < n; ++i) a1[i] = a[i];
for(int i = 0; i < n; ++i) a2[i] = a[i];
for(int i = 0; i < n; ++i) a3[i] = a[i];
for(int i = 0; i < m; ++i) b1[i] = b[i];
for(int i = 0; i < m; ++i) b2[i] = b[i];
for(int i = 0; i < m; ++i) b3[i] = b[i];
vector<mint1> c1 = convolution<mint1>(a1, b1);
vector<mint2> c2 = convolution<mint2>(a2, b2);
vector<mint3> c3 = convolution<mint3>(a3, b3);
vector<long long> c(n + m - 1);
for(int i = 0; i < n + m - 1; ++i) {
unsigned long long x = 0;
x += (c1[i].val() * i1) % MOD1 * M2M3;
x += (c2[i].val() * i2) % MOD2 * M1M3;
x += (c3[i].val() * i3) % MOD3 * M1M2;
long long diff = c1[i].val() - ((long long)x % (long long)MOD1 + (long long)MOD1) % (long long)MOD1;
if(diff < 0) diff += MOD1;
static constexpr unsigned long long offset[5] = {0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
x -= offset[diff % 5];
c[i] = x;
}
return c;
}
#line 4 "src/fps/formal_power_series_ll.hpp"
template <typename T>
struct FormalPowerSeriesLL : vector<T> {
using vector<T>::vector;
using F = FormalPowerSeriesLL;
F& operator=(const vector<T>& g) {
const int n = (*this).size();
const int m = g.size();
if(n < m) (*this).resize(m);
for(int i = 0; i < m; ++i) (*this)[i] = g[i];
return (*this);
}
F& operator-() {
const int n = (*this).size();
for(int i = 0; i < n; ++i) (*this)[i] *= -1;
return (*this);
}
F& operator+=(const F& g) {
const int n = (*this).size();
const int m = g.size();
if(n < m) (*this).resize(m);
for(int i = 0; i < m; ++i) (*this)[i] += g[i];
return (*this);
}
F& operator+=(const T& r) {
if((*this).empty()) (*this).resize(1, T(0));
(*this)[0] += r;
return (*this);
}
F& operator-=(const F& g) {
const int n = (*this).size();
const int m = g.size();
if(n < m) (*this).resize(m);
for(int i = 0; i < m; ++i) (*this)[i] -= g[i];
return (*this);
}
F& operator-=(const T& r) {
if((*this).empty()) (*this).resize(1, T(0));
(*this)[0] -= r;
return (*this);
}
F& operator*=(const F& g) {
(*this) = convolution_ll((*this), g);
return (*this);
}
F& operator*=(const T& r) {
const int n = (*this).size();
for(int i = 0; i < n; ++i) (*this)[i] *= r;
return (*this);
}
F& operator/=(const T& r) {
const int n = (*this).size();
for(int i = 0; i < (int)n; ++i) (*this)[i] /= r;
return (*this);
}
F operator*(const T& g) const {
return F(*this) *= g;
}
F operator-(const T& g) const {
return F(*this) -= g;
}
F operator+(const T& g) const {
return F(*this) += g;
}
F operator/(const T& g) const {
return F(*this) /= g;
}
F operator*(const F& g) const {
return F(*this) *= g;
}
F operator-(const F& g) const {
return F(*this) -= g;
}
F operator+(const F& g) const {
return F(*this) += g;
}
F operator<<(const int d) const {
F ret(*this);
ret.insert(ret.begin(), d, T(0));
return ret;
}
F operator>>(const int d) const {
const int n = (*this).size();
if(n <= d) return {};
F ret(*this);
ret.erase(ret.begin(), ret.begin() + d);
return ret;
}
void shrink() {
while(!(*this).empty() and (*this).back() == T(0)) (*this).pop_back();
}
F rev() const {
F ret(*this);
reverse(begin(ret), end(ret));
return ret;
}
F pre(const int deg) const {
assert(deg >= 0);
F ret(begin(*this), begin(*this) + min((int)(*this).size(), deg));
if((int)ret.size() < deg) ret.resize(deg);
return ret;
}
T eval(const T& a) const {
const int n = (*this).size();
T x = 1, ret = 0;
for(int i = 0; i < n; ++i) {
ret += (*this)[i] * x;
x *= a;
}
return ret;
}
void onemul(const int d, const T& c, int deg = -1) {
assert(deg >= -1);
const int n = (*this).size();
if(deg == -1) deg = n + d;
if(deg > n) (*this).resize(deg);
for(int i = deg - d - 1; i >= 0; --i) {
(*this)[i + d] += (*this)[i] * c;
}
}
void onediv(const int d, const T& c, int deg = -1) {
assert(deg >= -1);
const int n = (*this).size();
if(deg == -1) deg = n;
if(deg > n) (*this).resize(deg + 1);
for(int i = 0; i < deg - d; ++i) {
(*this)[i + d] -= (*this)[i] * c;
}
}
};