This documentation is automatically generated by online-judge-tools/verification-helper
#include "fps/fps.hpp"
形式的べき級数 $f$ を管理し、各種操作を行う構造体。std::vector
を継承している。
愚直 $O(N^2)$ 、NTTを用いると $O(N\log N)$ となる。
以下畳み込みの計算量を $O(N\log N)$ として計算量を記述する。
std::vector
と同一。
$O(N)$
NTTを用いると $O(N\log N)$
$f, g$ を多項式として、 $f = q * g + r$ となる多項式 $q$ を商、 $r$ を余りとする。 $O(N\log N)$
$f$ の最大次数 $+1$ を返す。
多項式の前 $sz$ 項を所得する。
多項式を反転させる。つまり $n = deg()$ として $f(x^{-1}) * x^{n-1}$ を計算する。 $O(N)$
形式的べき級数 $f$ を微分したものを返す。 $O(N)$
形式的べき級数 $f$ を積分したものを返す。 $O(N)$
形式的べき級数 $f$ の逆元を $\mod x^d$ で求める。 $O(N \log N)$
形式的べき級数 $f$ の $\log$ を $\mod x^d$ で求める。 $O(N\log N)$
形式的べき級数 $f$ の $\exp$ を $\mod x^d$ で求める。 $O(N\log N)$
形式的べき級数 $f$ について $f^k \mod x^d$ を求める。愚直だと $O(N\log N \log K)$ だが、$\log$ を取って $k$ を掛けて $\exp$ を取ることで求める。 $O(N\log N)$
末尾の不要なゼロを削除する。
#pragma once
#include "../convolution/ntt4.hpp"
#include "../template/template.hpp"
#include "../utility/modint.hpp"
namespace lib {
template <class mint> struct FormalPowerSeries : std::vector<mint> {
private:
using FPS = FormalPowerSeries<mint>;
using std::vector<mint>::vector;
using std::vector<mint>::vector::operator=;
NTT<mint> ntt;
public:
FormalPowerSeries(const std::vector<mint> &a) {
*this = a;
}
FPS operator+(const FPS &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const FPS &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const FPS &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const FPS &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS operator%(const FPS &rhs) const noexcept {
return FPS(*this) %= rhs;
}
FPS operator+(const mint &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const mint &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const mint &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const mint &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS &operator+=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] += rhs[i];
}
return *this;
}
FPS &operator-=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] -= rhs[i];
}
return *this;
}
FPS &operator*=(const FPS &rhs) noexcept {
*this = ntt.multiply(*this, rhs);
return *this;
}
FPS &operator/=(const FPS &rhs) noexcept {
int n = deg() - 1;
int m = rhs.deg() - 1;
if (n < m) {
*this = {};
return *this;
}
*this = (*this).rev() * rhs.rev().inv(n - m + 1);
(*this).resize(n - m + 1);
std::reverse((*this).begin(), (*this).end());
return *this;
}
FPS &operator%=(const FPS &rhs) noexcept {
*this -= *this / rhs * rhs;
shrink();
return *this;
}
FPS &operator+=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] += rhs;
return *this;
}
FPS &operator-=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] -= rhs;
return *this;
}
FPS &operator*=(const mint &rhs) noexcept {
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= rhs;
}
return *this;
}
FPS &operator/=(const mint &rhs) noexcept {
mint inv_rhs = rhs.inv();
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= inv_rhs;
}
return *this;
}
FPS operator>>(int d) const {
if (deg() <= d) return {};
FPS f = *this;
f.erase(f.begin(), f.begin() + d);
return f;
}
FPS operator<<(int d) const {
FPS f = *this;
f.insert(f.begin(), d, 0);
return f;
}
FPS operator-() const {
FPS g(this->size());
for (int i = 0; i < (int)this->size(); i++) g[i] = -(*this)[i];
return g;
}
FPS pre(int sz) const {
return FPS(this->begin(), this->begin() + std::min(deg(), sz));
}
FPS rev() const {
auto f = *this;
std::reverse(f.begin(), f.end());
return f;
}
FPS differential() const {
int n = deg();
FPS g(std::max(0, n - 1));
for (int i = 0; i < n - 1; i++) {
g[i] = (*this)[i + 1] * (i + 1);
}
return g;
}
FPS integral() const {
int n = deg();
FPS g(n + 1);
g[0] = 0;
if (n > 0) g[1] = 1;
auto mod = mint::mod();
for (int i = 2; i <= n; i++) g[i] = (-g[mod % i]) * (mod / i);
for (int i = 0; i < n; i++) g[i + 1] *= (*this)[i];
return g;
}
FPS inv(int d = -1) const {
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = (*this)[0].inv();
while (n < d) {
n <<= 1;
g = (g * 2 - g * g * this->pre(n)).pre(n);
}
g.resize(d);
return g;
}
FPS log(int d = -1) const {
assert((*this)[0].val() == 1);
if (d < 0) d = deg();
return ((*this).differential() * (*this).inv(d)).pre(d - 1).integral();
}
FPS exp(int d = -1) const {
assert((*this)[0].val() == 0);
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = 1;
while (n < d) {
n <<= 1;
g = (g * (this->pre(n) - g.log(n) + 1)).pre(n);
}
g.resize(d);
return g;
}
FPS pow(ll k, int d = -1) const {
const int n = deg();
if (d < 0) d = n;
if (k == 0) {
FPS f(d);
if (d > 0) f[0] = 1;
return f;
}
for (int i = 0; i < n; i++) {
if ((*this)[i].val() != 0) {
mint rev = (*this)[i].inv();
FPS f = (((*this * rev) >> i).log(d) * k).exp(d);
f *= (*this)[i].pow(k);
f = (f << (i * k)).pre(d);
if (f.deg() < d) f.resize(d);
return f;
}
if (i + 1 >= (d + k - 1) / k) break;
}
return FPS(d);
}
int deg() const {
return (*this).size();
}
void shrink() {
while ((!this->empty()) && this->back() == 0) this->pop_back();
}
int count_terms() const {
int c = 0;
for (int i = 0; i < deg(); i++) {
if ((*this)[i] != 0) c++;
}
return c;
}
};
} // namespace lib
#line 2 "fps/fps.hpp"
#line 2 "convolution/ntt4.hpp"
#line 2 "utility/modint.hpp"
#line 2 "template/template.hpp"
#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <typename T> bool chmin(T &a, const T &b) {
if (a <= b) return false;
a = b;
return true;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a >= b) return false;
a = b;
return true;
}
namespace lib {
using namespace std;
} // namespace lib
// using namespace lib;
#line 4 "utility/modint.hpp"
namespace lib {
template <ll m> struct modint {
using mint = modint;
ll a;
modint(ll x = 0) : a((x % m + m) % m) {}
static constexpr ll mod() {
return m;
}
ll val() const {
return a;
}
ll& val() {
return a;
}
mint pow(ll n) const {
mint res = 1;
mint x = a;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
mint inv() const {
return pow(m - 2);
}
mint& operator+=(const mint rhs) {
a += rhs.a;
if (a >= m) a -= m;
return *this;
}
mint& operator-=(const mint rhs) {
if (a < rhs.a) a += m;
a -= rhs.a;
return *this;
}
mint& operator*=(const mint rhs) {
a = a * rhs.a % m;
return *this;
}
mint& operator/=(mint rhs) {
*this *= rhs.inv();
return *this;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const modint &lhs, const modint &rhs) {
return lhs.a == rhs.a;
}
friend bool operator!=(const modint &lhs, const modint &rhs) {
return !(lhs == rhs);
}
mint operator+() const {
return *this;
}
mint operator-() const {
return mint() - *this;
}
};
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1'000'000'007>;
} // namespace lib
#line 4 "convolution/ntt4.hpp"
namespace lib {
// only for modint998244353
template<typename mint>
struct NTT {
using uint = unsigned int;
static constexpr uint mod = mint::mod();
static constexpr ull mod2 = (ull)mod * mod;
static constexpr uint pr = 3; // for modint998244353
static constexpr int level = 23; // for modint998244353
array<mint,level+1> wp, wm;
void set_ws(){
mint r = mint(pr).pow((mod-1) >> level);
wp[level] = r, wm[level] = r.inv();
for (int i = level-1; i >= 0; i--){
wp[i] = wp[i+1] * wp[i+1];
wm[i] = wm[i+1] * wm[i+1];
}
}
NTT () { set_ws(); }
void fft4(vector<mint> &a, int k){
uint im = wm[2].val();
uint n = 1<<k;
uint len = n;
int d = k;
while (len > 1){
if (d == 1){
for (int i = 0; i < (1<<(k-1)); i++){
a[i*2+0] += a[i*2+1];
a[i*2+1] = a[i*2+0] - a[i*2+1] * 2;
}
len >>= 1;
d -= 1;
}
else {
int len4 = len/4;
int nlen = n/len;
ull r1 = 1, r2 = 1, r3 = 1, imr1 = im, imr3 = im;
for (int i = 0; i < len4; i++){
for (int j = 0; j < nlen; j++){
uint a0 = a[len4*0+i + len*j].val();
uint a1 = a[len4*1+i + len*j].val();
uint a2 = a[len4*2+i + len*j].val();
uint a3 = a[len4*3+i + len*j].val();
uint a0p2 = a0 + a2;
uint a1p3 = a1 + a3;
ull b0m2 = (a0 + mod - a2) * r1;
ull b1m3 = (a1 + mod - a3) * imr1;
ull c0m2 = (a0 + mod - a2) * r3;
ull c1m3 = (a1 + mod - a3) * imr3;
a[len4*0+i + len*j] = a0p2 + a1p3;
a[len4*1+i + len*j] = b0m2 + b1m3;
a[len4*2+i + len*j] = (a0p2 + mod*2 - a1p3) * r2;
a[len4*3+i + len*j] = c0m2 + mod2*2 - c1m3;
}
r1 = r1 * wm[d].val() % mod;
r2 = r1 * r1 % mod;
r3 = r1 * r2 % mod;
imr1 = im * r1 % mod;
imr3 = im * r3 % mod;
}
len >>= 2;
d -= 2;
}
}
}
void ifft4(vector<mint> &a, int k){
uint im = wp[2].val();
uint n = 1<<k;
uint len = (k & 1 ? 2 : 4);
int d = (k & 1 ? 1 : 2);
while (len <= n){
if (d == 1){
for (int i = 0; i < (1<<(k-1)); i++){
a[i*2+0] += a[i*2+1];
a[i*2+1] = a[i*2+0] - a[i*2+1] * 2;
}
len <<= 2;
d += 2;
}
else {
int len4 = len/4;
int nlen = n/len;
ull r1 = 1, r2 = 1, r3 = 1, imr1 = im, imr3 = im;
for (int i = 0; i < len4; i++){
for (int j = 0; j < nlen; j++){
ull a0 = a[len4*0+i + len*j].val();
ull a1 = a[len4*1+i + len*j].val() * r1;
ull a2 = a[len4*2+i + len*j].val() * r2;
ull a3 = a[len4*3+i + len*j].val() * r3;
ull b1 = a[len4*1+i + len*j].val() * imr1;
ull b3 = a[len4*3+i + len*j].val() * imr3;
ull a0p2 = a0 + a2;
ull a1p3 = a1 + a3;
ull a0m2 = a0 + mod2 - a2;
ull b1m3 = b1 + mod2 - b3;
a[len4*0+i + len*j] = a0p2 + a1p3;
a[len4*1+i + len*j] = a0m2 + b1m3;
a[len4*2+i + len*j] = a0p2 + mod2*2 - a1p3;
a[len4*3+i + len*j] = a0m2 + mod2*2 - b1m3;
}
r1 = r1 * wp[d].val() % mod;
r2 = r1 * r1 % mod;
r3 = r1 * r2 % mod;
imr1 = im * r1 % mod;
imr3 = im * r3 % mod;
}
len <<= 2;
d += 2;
}
}
}
vector<mint> multiply(const vector<mint> &a, const vector<mint> &b){
if (a.empty() || b.empty()) return {};
int d = a.size() + b.size() - 1;
if (min<int>(a.size(), b.size()) <= 40){
vector<mint> s(d);
rep(i,0,a.size()) rep(j,0,b.size()) s[i+j] += a[i]*b[j];
return s;
}
int k = 2, M = 4;
while (M < d) M <<= 1, ++k;
vector<mint> s(M), t(M);
rep(i,0,a.size()) s[i] = a[i];
rep(i,0,b.size()) t[i] = b[i];
fft4(s,k);
fft4(t,k);
rep(i,0,M) s[i] *= t[i];
ifft4(s, k);
s.resize(d);
mint invm = mint(M).inv();
rep(i,0,d) s[i] *= invm;
return s;
}
};
} // namespace lib
#line 6 "fps/fps.hpp"
namespace lib {
template <class mint> struct FormalPowerSeries : std::vector<mint> {
private:
using FPS = FormalPowerSeries<mint>;
using std::vector<mint>::vector;
using std::vector<mint>::vector::operator=;
NTT<mint> ntt;
public:
FormalPowerSeries(const std::vector<mint> &a) {
*this = a;
}
FPS operator+(const FPS &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const FPS &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const FPS &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const FPS &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS operator%(const FPS &rhs) const noexcept {
return FPS(*this) %= rhs;
}
FPS operator+(const mint &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const mint &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const mint &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const mint &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS &operator+=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] += rhs[i];
}
return *this;
}
FPS &operator-=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] -= rhs[i];
}
return *this;
}
FPS &operator*=(const FPS &rhs) noexcept {
*this = ntt.multiply(*this, rhs);
return *this;
}
FPS &operator/=(const FPS &rhs) noexcept {
int n = deg() - 1;
int m = rhs.deg() - 1;
if (n < m) {
*this = {};
return *this;
}
*this = (*this).rev() * rhs.rev().inv(n - m + 1);
(*this).resize(n - m + 1);
std::reverse((*this).begin(), (*this).end());
return *this;
}
FPS &operator%=(const FPS &rhs) noexcept {
*this -= *this / rhs * rhs;
shrink();
return *this;
}
FPS &operator+=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] += rhs;
return *this;
}
FPS &operator-=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] -= rhs;
return *this;
}
FPS &operator*=(const mint &rhs) noexcept {
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= rhs;
}
return *this;
}
FPS &operator/=(const mint &rhs) noexcept {
mint inv_rhs = rhs.inv();
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= inv_rhs;
}
return *this;
}
FPS operator>>(int d) const {
if (deg() <= d) return {};
FPS f = *this;
f.erase(f.begin(), f.begin() + d);
return f;
}
FPS operator<<(int d) const {
FPS f = *this;
f.insert(f.begin(), d, 0);
return f;
}
FPS operator-() const {
FPS g(this->size());
for (int i = 0; i < (int)this->size(); i++) g[i] = -(*this)[i];
return g;
}
FPS pre(int sz) const {
return FPS(this->begin(), this->begin() + std::min(deg(), sz));
}
FPS rev() const {
auto f = *this;
std::reverse(f.begin(), f.end());
return f;
}
FPS differential() const {
int n = deg();
FPS g(std::max(0, n - 1));
for (int i = 0; i < n - 1; i++) {
g[i] = (*this)[i + 1] * (i + 1);
}
return g;
}
FPS integral() const {
int n = deg();
FPS g(n + 1);
g[0] = 0;
if (n > 0) g[1] = 1;
auto mod = mint::mod();
for (int i = 2; i <= n; i++) g[i] = (-g[mod % i]) * (mod / i);
for (int i = 0; i < n; i++) g[i + 1] *= (*this)[i];
return g;
}
FPS inv(int d = -1) const {
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = (*this)[0].inv();
while (n < d) {
n <<= 1;
g = (g * 2 - g * g * this->pre(n)).pre(n);
}
g.resize(d);
return g;
}
FPS log(int d = -1) const {
assert((*this)[0].val() == 1);
if (d < 0) d = deg();
return ((*this).differential() * (*this).inv(d)).pre(d - 1).integral();
}
FPS exp(int d = -1) const {
assert((*this)[0].val() == 0);
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = 1;
while (n < d) {
n <<= 1;
g = (g * (this->pre(n) - g.log(n) + 1)).pre(n);
}
g.resize(d);
return g;
}
FPS pow(ll k, int d = -1) const {
const int n = deg();
if (d < 0) d = n;
if (k == 0) {
FPS f(d);
if (d > 0) f[0] = 1;
return f;
}
for (int i = 0; i < n; i++) {
if ((*this)[i].val() != 0) {
mint rev = (*this)[i].inv();
FPS f = (((*this * rev) >> i).log(d) * k).exp(d);
f *= (*this)[i].pow(k);
f = (f << (i * k)).pre(d);
if (f.deg() < d) f.resize(d);
return f;
}
if (i + 1 >= (d + k - 1) / k) break;
}
return FPS(d);
}
int deg() const {
return (*this).size();
}
void shrink() {
while ((!this->empty()) && this->back() == 0) this->pop_back();
}
int count_terms() const {
int c = 0;
for (int i = 0; i < deg(); i++) {
if ((*this)[i] != 0) c++;
}
return c;
}
};
} // namespace lib