板子
做多项式题就像嗑药,出多项式题就像贩毒。——某著名 OIer
锅很多,所以什么时候我写题挂了就什么时候改。
为什么没有 xxx?答:不会。
发出来是为了防止哪天我自己找不到自己的板子了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P = 998244353, G = 3, GI = 332748118;
ll QPow(ll a, ll b, ll p) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
struct Poly {
friend Poly operator*(const Poly &a, const Poly &b);
friend Poly operator*(const Poly &a, ll b);
friend Poly &operator*=(Poly &a, const Poly &b);
friend Poly operator-(const Poly &a, const Poly &b);
friend Poly operator+(const Poly &a, const Poly &b);
const static int MAX_SIZE = 3000005;
vector<ll> val;
Poly() {}
Poly(int sz) : val(sz) {}
Poly(vector<ll> val) : val(val) {}
size_t size() const { return val.size(); }
void resize(int n) { val.resize(n); }
static int lim, r[MAX_SIZE];
static void SetLimit(int w) {
int l = __lg(w - 1) + 1;
lim = 1 << l;
for (int i = 0; i < lim; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
ll& operator[](size_t i) { return val[i]; }
const ll& operator[](size_t i) const { return val[i]; }
Poly Trunc(int n) const {
Poly res(*this);
res.resize(n);
return res;
}
Poly Shift(int n) const {
if (n >= 0) {
Poly res(*this);
res.val.insert(res.val.begin(), n, 0);
return res;
}
else if (size() <= -n) {
return Poly();
}
else {
Poly res(vector<ll>(val.begin() + (-n), val.end()));
return res;
}
}
void NTT(int tp) {
if (val.size() < lim) val.resize(lim);
for (int i = 0; i < lim; i++) if (i < r[i]) swap(val[i], val[r[i]]);
for (int w = 1; w < lim; w <<= 1) {
ll wn = QPow(tp == 1 ? G : GI, (P - 1) / (w << 1), P);
for (int j = 0; j < lim; j += (w << 1)) {
ll t = 1;
for (int k = 0; k < w; k++) {
ll x = val[j + k], y = t * val[j + k + w] % P;
val[j + k] = (x + y) % P;
val[j + k + w] = (x - y + P) % P;
t = t * wn % P;
}
}
}
if (tp == -1) {
ll inv = QPow(lim, P - 2, P);
for (int i = 0; i < lim; i++) val[i] = val[i] * inv % P;
}
}
Poly Inv(int n) const {
Poly res((vector<ll>){ QPow(val[0], P - 2, P) });
int k = 1;
while (k < n) {
k *= 2;
res = (res * (Poly(vector<ll>{ 2ll }) - Trunc(k) * res)).Trunc(k);
}
return res.Trunc(n);
}
Poly Deri() {
if (!size()) return Poly();
Poly res(size() - 1);
for (int i = 0; i < size() - 1; i++) res[i] = (i + 1) * val[i + 1] % P;
return res;
}
Poly Inte() {
Poly res(size() + 1);
for (int i = 0; i < size(); i++) res[i + 1] = val[i] * QPow(i + 1, P - 2, P) % P;
return res;
}
Poly Ln(int n) {
return (Deri() * Inv(n)).Inte().Trunc(n);
}
Poly Exp(int n) {
Poly res((vector<ll>){ 1ll });
int k = 1;
while (k < n) {
k *= 2;
res = (res * (Poly(vector<ll>{ 1ll }) + Trunc(k) - res.Ln(k))).Trunc(k);
}
return res.Trunc(n);
}
Poly Sqrt(int n) {
Poly res((vector<ll>){ 1ll });
int k = 1;
while (k < n) {
k *= 2;
res = (res + (Trunc(k) * res.Inv(k))).Trunc(k) * QPow(2, P - 2, P);
}
return res.Trunc(n);
}
};
Poly &operator*=(Poly &a, const Poly &b) {
int sz = a.size() + b.size() - 1;
Poly::SetLimit(sz);
Poly c(b);
a.NTT(1); c.NTT(1);
for (int i = 0; i < Poly::lim; i++) a[i] = (a[i] * c[i] % P + P) % P;
a.NTT(-1); a.resize(sz);
return a;
}
Poly operator*(const Poly &a, const Poly &b) {
Poly t(a); t *= b;
return t;
}
Poly &operator*=(Poly &a, ll b) {
int n = a.size();
b = (b % P + P) % P;
for (int i = 0; i < n; i++) a[i] = a[i] * b % P;
return a;
}
Poly operator*(const Poly &a, ll b) {
Poly res(a);
res *= b;
return res;
}
Poly &operator*=(ll b, Poly &a) {
int n = a.size();
b = (b % P + P) % P;
for (int i = 0; i < n; i++) a[i] = a[i] * b % P;
return a;
}
Poly operator*(ll b, const Poly &a) {
Poly res(a);
res *= b;
return res;
}
Poly &operator+=(Poly &a, const Poly &b) {
a.resize(max(a.size(), b.size()));
for (int i = 0; i < b.size(); i++) (a[i] += b[i]) %= P;
return a;
}
Poly operator+(const Poly &a, const Poly &b) {
Poly res(a);
res += b;
return res;
}
Poly &operator-=(Poly &a, const Poly &b) {
a.resize(max(a.size(), b.size()));
for (int i = 0; i < b.size(); i++) (a[i] += P - b[i]) %= P;
return a;
}
Poly operator-(const Poly &a, const Poly &b) {
Poly res(a);
res -= b;
return res;
}
Poly &operator-=(Poly &a, ll b) {
if (!a.size()) a.resize(1);
a[0] = ((a[0] - b) % P + P) % P;
return a;
}
Poly operator-(const Poly &a, ll b) {
Poly res(a);
res -= b;
return res;
}
Poly operator-(const Poly &a) {
Poly res(a);
int n = a.size();
for (int i = 0; i < n; i++) res[i] = P - res[i];
return res;
}
int Poly::r[Poly::MAX_SIZE], Poly::lim;
int main() {
return 0;
}