// using fread//#define INPUT_OPTIMIZE// using fwrite//#define OUTPUT_OPTIMIZEnamespace IO {
#ifdef INPUT_OPTIMIZEstatic char buf[1 << 21], *p1 = buf, *p2 = buf;
#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++#endifinlinebool read(char *t) {
memset(t, 0, sizeof t);
char *p = t, c = getchar();
while (isspace(c)) c = getchar();
while (!isspace(c)) *p++ = c, c = getchar();
return c == EOF;
}
template<typename T>inlinebool read(T &t) {
t = 0;
char c = getchar(); bool f = 1;
while (isspace(c)) c = getchar();
if (c == '-') f = 0, c = getchar();
while (isdigit(c)) t = (t << 3) + (t << 1) + (c ^ 48), c = getchar();
t *= f ? 1 : -1;
return c != EOF;
}
template<typename T, typename... Args>inlinebool read(T &t, Args&... args) {
return read(t) && read(args...);
}
#ifdef OUTPUT_OPTIMIZEstatic char outbuf[1 << 24], *out = outbuf;
#define putchar(x) *out++ = x
#define flush() fwrite(outbuf, 1, out - outbuf, stdout)
#else#define flush() 0#endifinlinevoid write(const char* s) {
int l = strlen(s);
for (int i = 0; i < l; i++) putchar(s[i]);
}
template<typename T>inlinevoid write(T x) {
if (x < 0) putchar('-'), x = -x;
if (!x) return putchar('0'), void();
static char t[20], p = 0;
while (x) t[++p] = (x % 10) ^ 48, x /= 10;
while (p) putchar(t[p--]);
}
template<typename T, typename... Args>inlinevoid write(T t, Args... args) {
write(t), write(args...);
}
}
using namespace IO;
压位高精
C++
const int MAXM = 100;
const int BASE = 1e4;
struct Int {
int a[MAXM];
Int(int k = 0) {
for (int i = MAXM - 1; ~i; i--) a[i] = k % BASE, k /= BASE;
}
Int operator + (const Int &rhs) const {
int t = 0; Int res = *this;
for (int i = MAXM - 1; ~i; i--) {
t += res.a[i] + rhs.a[i];
res.a[i] = t % BASE, t /= BASE;
}
return res;
}
Int operator - (const Int &rhs) const {
int t = 0; Int res = *this;
for (int i = MAXM - 1; ~i; i--) {
t += res.a[i] - rhs.a[i];
if (t < 0) res.a[i - 1]--, t += BASE;
res.a[i] = t % BASE, t /= BASE;
}
return res;
}
Int operator * (const Int &rhs) const {
Int res; int t = 0;
for (int i = 1; i <= MAXM; i++) {
for (int j = 1; j <= MAXM; j++) {
if (i + j - 1 > MAXM) continue; // Error.
t = a[MAXM - i] * rhs.a[MAXM - j] + res.a[MAXM - i - j + 1];
res.a[MAXM - i - j] += t / BASE, res.a[MAXM - i - j + 1] = t % BASE;
}
}
return res;
}
friend ostream & operator << (ostream &out, const Int &n) {
int p = 0;
for (; p < MAXM && !n.a[p]; p++);
if (p == MAXM) return out << 0;
for (out << n.a[p++]; p < MAXM; p++) out << setw(4) << setfill('0') << n.a[p];
return out;
}
Int & operator += (const Int &rhs) { return *this = *this + rhs; }
Int & operator -= (const Int &rhs) { return *this = *this - rhs; }
Int & operator *= (const Int &rhs) { return *this = *this * rhs; }
};
普通多项式
C++
#define _POW_OPTIMIZE_FOR_Knamespace polynomial {
typedef long long ll;
vector<int> rev;
int getrev(int n) {
int l = 1 << __lg(2 * n - 1) + 1; rev.resize(l);
for (int i = 1; i < l; i++) rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? l >> 1 : 0);
return l;
}
const int mod = 998244353;
int add(int x, int y) { x += y; return x < mod ? x : x - mod; }
int sub(int x, int y) { x -= y; return x < 0 ? x + mod : x; }
ll qpow(ll b, ll p) {
ll res = 1;
for (; p; b = b * b % mod, p >>= 1) if (p & 1) res = res * b % mod;
return res;
}
ll inv(ll n) { return qpow(n, mod - 2); }
const int gw = 3, invgw = inv(3);
template<typename T>class poly {
private:
vector<T> func;
public:T &operator [] (int n) { return func[n]; }
T operator [] (int n) const { return func[n]; }
void resize(size_t n) { func.resize(n); }
size_t size() const { return func.size(); }
void push_back(T x) { func.push_back(x); }
T &back() { return func.back(); }
auto begin() { return func.begin(); }
auto end() { return func.end(); }
void clear() { func.clear(); }
void reverse() { ::reverse(begin(), end()); }
poly<T>() {}
poly<T>(int n) { resize(n); }
poly<T>(vector<T> v) {
resize(v.size());
for (int i = 0; i < v.size(); i++) func[i] = v[i];
}
~poly<T>() {}
void ntt(bool t) {
int n = size();
for (int i = 0; i < n; i++) {
if (i < rev[i]) swap(func[i], func[rev[i]]);
}
for (int i = 1; i < n; i <<= 1) {
ll w = qpow(t ? invgw : gw, (mod - 1) / (i << 1));
for (int j = 0; j < n; j += i << 1) {
ll wn = 1, x;
for (int k = j; k < i + j; k++) {
x = wn * func[i + k] % mod, wn = wn * w % mod;
func[i + k] = sub(func[k], x), func[k] = add(func[k], x);
}
}
}
if (t) {
int q = qpow(n, mod - 2);
for (int i = 0; i < n; i++) func[i] = func[i] * q % mod;
}
}
void print() {
int n = size();
for (int i = 0; i < n; i++) printf("%lld ", func[i]); puts("");
}
public:poly operator + (const poly &rhs) const {
int n = size(), m = rhs.size(); poly res(max(n, m));
for (int i = 0; i < max(n, m); i++) res[i] = add(i < n ? func[i] : 0, i < m ? rhs[i] : 0);
return res;
}
poly operator - (const poly &rhs) const {
int n = size(), m = rhs.size(); poly res(max(n, m));
for (int i = 0; i < res.size(); i++) res[i] = sub(i < n ? func[i] : 0, i < m ? rhs[i] : 0);
return res;
}
poly operator * (const poly &rhs) const {
int n = size(), l = getrev(n);
poly f = *this, g = rhs; g.resize(n);
poly res(l); f.resize(l), g.resize(l), f.ntt(0), g.ntt(0);
for (int i = 0; i < l; i++) res[i] = (ll)f[i] * g[i] % mod;
res.ntt(1), res.resize(n);
return res;
}
poly operator / (const poly &rhs) const {
if (size() < rhs.size()) return 0;
poly f = *this, g = rhs; f.reverse(), g.reverse();
g.resize(size() - rhs.size() + 1), g = inv(g) * f, g.reverse();
return g;
}
poly operator % (const poly &rhs) const {
if (size() < rhs.size()) return *this;
poly a = *this - rhs * (*this / rhs); a.resize(rhs.size() - 1);
return a;
}
poly operator << (const int &rhs) const {
int n = size(); poly f = *this;
for (int i = 0; i < n - rhs; i++) f[i] = f[i + rhs]; f.resize(n - rhs);
return f;
}
poly operator >> (const int &rhs) const {
int n = size(); poly f = *this; f.resize(n + rhs);
for (int i = n + rhs - 1; i >= rhs; i--) f[i] = f[i - rhs];
for (int i = rhs - 1; i >= 0; i--) f[i] = 0;
return f;
}
poly operator - () const {
poly res = *this; int n = size();
for (int i = 0; i < n; i++) res[i] = mod - res[i];
return res;
}
poly &operator += (const poly &rhs) { return *this = *this + rhs; }
poly &operator -= (const poly &rhs) { return *this = *this - rhs; }
poly &operator *= (const poly &rhs) { return *this = *this * rhs; }
poly &operator /= (const poly &rhs) { return *this = *this / rhs; }
poly &operator %= (const poly &rhs) { return *this = *this % rhs; }
poly &operator <<= (const int &rhs) { return *this = *this << rhs; }
poly &operator >>= (const int &rhs) { return *this = *this >> rhs; }
poly operator + (const T &rhs) const {
poly res = *this;
size() ? res[0] = add(res[0], rhs) : (res.push_back(rhs), 0);
return res;
}
poly operator - (const T &rhs) const {
poly res = *this;
size() ? res[0] = sub(res[0], rhs) : (res.push_back(mod - rhs), 0);
return res;
}
poly operator * (const T &rhs) const {
poly res = *this; int n = size();
for (int i = 0; i < n; i++) res[i] = res[i] * rhs % mod;
return res;
}
poly &operator += (const T &rhs) { return *this = *this + rhs; }
poly &operator -= (const T &rhs) { return *this = *this - rhs; }
poly &operator *= (const T &rhs) { return *this = *this * rhs; }
};
template<typename T>poly<T> inv(poly<T> f) {
int s[30], t, n; poly<T> g, a;
for (t = 0, n = f.size(); n > 1; n = n + 1 >> 1) s[++t] = n;
for (g += inv(f[0]); t; t--) {
int l = getrev(s[t]); g.resize(l), a.resize(l);
for (int i = 0; i < s[t]; i++) a[i] = f[i];
for (int i = s[t]; i < l; i++) a[i] = 0;
a.ntt(0), g.ntt(0);
for (int i = 0; i < l; i++) g[i] = sub(2, a[i] * g[i] % mod) * g[i] % mod;
g.ntt(1), g.resize(s[t]);
}
return g;
}
template<typename T>poly<T> differential(const poly<T> &f) {
int n = f.size(); poly<T> g(n);
for (int i = 1; i < n; i++) g[i - 1] = i * f[i] % mod;
return g;
}
template<typename T>poly<T> integral(const poly<T> &f) {
int n = f.size(); poly<T> g(n);
for (int i = 1; i < n; i++) g[i] = f[i - 1] * qpow(i, mod - 2) % mod;
return g;
}
template<typename T>poly<T> ln(const poly<T> &f) {
return integral(differential(f) * inv(f));
}
template<typename T>poly<T> exp(poly<T> f) {
int s[30], t, n; poly<T> a, g;
for (t = 0, n = f.size(); n > 1; n = n + 1 >> 1) s[++t] = n;
for (g += 1; t; t--) {
g.resize(s[t]), a = ln(g);
for (int i = 1; i < s[t]; i++) a[i] = sub(f[i], a[i]);
g *= a + 1;
}
return g;
}
template<typename T>poly<T> pow(poly<T> f, ll k) {
#ifdef _POW_OPTIMIZE_FOR_Kint n = f.size(), p;
for (p = 0; p < n; p++) if (f[p]) break;
if (p == n || p * k >= n) { f.resize(0), f.resize(n); return f; }
f <<= p; ll x = qpow(f[0], k);
poly<ll> g = ln(f * inv(f[0])); f = exp(g * k), f *= x;
f >>= p * k, f.resize(n);
return f;
#else
poly<ll> g(f.size());
for (g += 1; k; f *= f, k >>= 1) if (k & 1) g *= f;
return g;
#endif
}
}
using namespace polynomial;
普通多项式 精简版
C++
namespace polynomial {
typedef long long ll;
vector<int> rev;
int getrev(int n) {
int l = 1 << __lg(n) + 1; rev.resize(l);
for (int i = 1; i < l; i++) rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? l >> 1 : 0);
return l;
}
const int mod = 998244353;
int add(int x, int y) { x += y; return x < mod ? x : x - mod; }
int sub(int x, int y) { x -= y; return x < 0 ? x + mod : x; }
ll qpow(ll b, ll p) {
ll res = 1;
for (; p; b = b * b % mod, p >>= 1) if (p & 1) res = res * b % mod;
return res;
}
ll inv(ll n) { return qpow(n, mod - 2); }
const int gw = 3, invgw = inv(3);
template<typename T>class poly {
private:
vector<T> func;
public:T &operator [] (int n) { return func[n]; }
T operator [] (int n) const { return func[n]; }
void resize(size_t n) { func.resize(n); }
size_t size() const { return func.size(); }
void push_back(T x) { func.push_back(x); }
T &back() { return func.back(); }
auto begin() { return func.begin(); }
auto end() { return func.end(); }
void clear() { func.clear(); }
void reverse() { ::reverse(begin(), end()); }
poly<T>() {}
poly<T>(int n) { resize(n); }
poly<T>(vector<T> v) {
resize(v.size());
for (int i = 0; i < v.size(); i++) func[i] = v[i];
}
~poly<T>() {}
void ntt(bool t) {
int n = size();
for (int i = 0; i < n; i++) {
if (i < rev[i]) swap(func[i], func[rev[i]]);
}
for (int i = 1; i < n; i <<= 1) {
ll w = qpow(t ? invgw : gw, (mod - 1) / (i << 1));
for (int j = 0; j < n; j += i << 1) {
ll wn = 1, x;
for (int k = j; k < i + j; k++) {
x = wn * func[i + k] % mod, wn = wn * w % mod;
func[i + k] = sub(func[k], x), func[k] = add(func[k], x);
}
}
}
if (t) {
int q = qpow(n, mod - 2);
for (int i = 0; i < n; i++) func[i] = func[i] * q % mod;
}
}
void print() {
int n = size();
for (int i = 0; i < n; i++) printf("%lld ", func[i]); puts("");
}
public:poly operator + (const poly &rhs) const {
int n = size(), m = rhs.size(); poly res(max(n, m));
for (int i = 0; i < max(n, m); i++) res[i] = add(i < n ? func[i] : 0, i < m ? rhs[i] : 0);
return res;
}
poly operator - (const poly &rhs) const {
int n = size(), m = rhs.size(); poly res(max(n, m));
for (int i = 0; i < res.size(); i++) res[i] = sub(i < n ? func[i] : 0, i < m ? rhs[i] : 0);
return res;
}
poly operator * (const poly &rhs) const {
int n = size() + rhs.size() - 1, l = getrev(n);
poly f = *this, g = rhs; g.resize(n);
poly res(l); f.resize(l), g.resize(l), f.ntt(0), g.ntt(0);
for (int i = 0; i < l; i++) res[i] = (ll)f[i] * g[i] % mod;
res.ntt(1), res.resize(n);
return res;
}
poly operator << (const int &rhs) const {
int n = size(); poly f = *this;
for (int i = 0; i < n - rhs; i++) f[i] = f[i + rhs]; f.resize(n - rhs);
return f;
}
poly operator >> (const int &rhs) const {
int n = size(); poly f = *this; f.resize(n + rhs);
for (int i = n + rhs - 1; i >= rhs; i--) f[i] = f[i - rhs];
for (int i = rhs - 1; i >= 0; i--) f[i] = 0;
return f;
}
poly operator - () const {
poly res = *this; int n = size();
for (int i = 0; i < n; i++) res[i] = mod - res[i];
return res;
}
poly &operator += (const poly &rhs) { return *this = *this + rhs; }
poly &operator -= (const poly &rhs) { return *this = *this - rhs; }
poly &operator *= (const poly &rhs) { return *this = *this * rhs; }
poly &operator <<= (const int &rhs) { return *this = *this << rhs; }
poly &operator >>= (const int &rhs) { return *this = *this >> rhs; }
poly operator + (const T &rhs) const {
poly res = *this;
size() ? res[0] = add(res[0], rhs) : (res.push_back(rhs), 0);
return res;
}
poly operator - (const T &rhs) const {
poly res = *this;
size() ? res[0] = sub(res[0], rhs) : (res.push_back(mod - rhs), 0);
return res;
}
poly operator * (const T &rhs) const {
poly res = *this; int n = size();
for (int i = 0; i < n; i++) res[i] = res[i] * rhs % mod;
return res;
}
poly &operator += (const T &rhs) { return *this = *this + rhs; }
poly &operator -= (const T &rhs) { return *this = *this - rhs; }
poly &operator *= (const T &rhs) { return *this = *this * rhs; }
};
template<typename T>poly<T> inv(poly<T> f) {
int s[30], t, n; poly<T> g, a;
for (t = 0, n = f.size(); n > 1; n = n + 1 >> 1) s[++t] = n;
for (g += inv(f[0]); t; t--) {
int l = getrev(s[t] * 2 - 1); g.resize(l), a.resize(l);
for (int i = 0; i < s[t]; i++) a[i] = f[i];
for (int i = s[t]; i < l; i++) a[i] = 0;
a.ntt(0), g.ntt(0);
for (int i = 0; i < l; i++) g[i] = sub(2, a[i] * g[i] % mod) * g[i] % mod;
g.ntt(1), g.resize(s[t]);
}
return g;
}
template<typename T>poly<T> differential(const poly<T> &f) {
int n = f.size(); poly<T> g(n);
for (int i = 1; i < n; i++) g[i - 1] = i * f[i] % mod;
return g;
}
template<typename T>poly<T> integral(const poly<T> &f) {
int n = f.size(); poly<T> g(n);
for (int i = 1; i < n; i++) g[i] = f[i - 1] * qpow(i, mod - 2) % mod;
return g;
}
template<typename T>poly<T> ln(const poly<T> &f) {
poly<T> res = differential(f) * inv(f); res.resize(f.size());
return integral(res);
}
template<typename T>poly<T> exp(poly<T> f) {
int s[30], t, n; poly<T> a, g;
for (t = 0, n = f.size(); n > 1; n = n + 1 >> 1) s[++t] = n;
for (g += 1; t; t--) {
g.resize(s[t]), a = ln(g);
for (int i = 1; i < s[t]; i++) a[i] = sub(f[i], a[i]);
g *= a + 1;
}
g.resize(f.size()); return g;
}
}
using namespace polynomial;
任意模多项式
C++
typedef long long ll;
typedef long double ldb;
typedef complex<ldb> cldb;
const int MAXN = 1 << 20;
const ldb PI = acos(-1);
int mod;
inlinell qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) res = res * b % mod;
b = b * b % mod, p >>= 1;
}
return res;
}
int rev[MAXN];
inlineint getrev(int n) {
int l = 1;
while (l < n << 1) l <<= 1;
for (int i = 1; i < l; i++) rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? l >> 1 : 0);
return l;
}
inlinevoid fft(cldb *f, int n, int t) {
for (int i = 0; i < n; i++) {
if (i < rev[i]) swap(f[i], f[rev[i]]);
}
for (int i = 1; i < n; i <<= 1) {
cldb w = { cos(PI / i), t ? -sin(PI / i) : sin(PI / i) };
for (int j = 0; j < n; j += i << 1) {
cldb wn = 1, x;
for (int k = j; k < i + j; k++, wn *= w) {
x = wn * f[i + k], f[i + k] = f[k] - x, f[k] += x;
}
}
}
}
inlinevoid mtt(ll *f, ll *g, ll *res, int n, int l) {
static cldb a[MAXN], b[MAXN], c[MAXN], d[MAXN];
for (int i = 0; i < n; i++) a[i] = { f[i] >> 15, f[i] & 32767 }, c[i] = { g[i] >> 15, g[i] & 32767 };
for (int i = n; i < l; i++) a[i] = c[i] = 0;
fft(a, l, 0), fft(c, l, 0);
b[0] = conj(a[0]), d[0] = conj(c[0]);
for (int i = 1; i < l; i++) b[i] = conj(a[l - i]), d[i] = conj(c[l - i]);
for (int i = 0; i < l; i++) {
cldb p0 = (a[i] + b[i]) * 0.5l;
cldb p1 = (b[i] - a[i]) * 0.5il;
cldb q0 = (c[i] + d[i]) * 0.5l;
cldb q1 = (d[i] - c[i]) * 0.5il;
a[i] = p0 * q0 + (p0 * q1 + p1 * q0) * 1il;
b[i] = p1 * q1;
}
fft(a, l, 1), fft(b, l, 1);
for (int i = 0; i < l; i++) {
ll t1 = (ll)(a[i].real() / l + 0.5) % mod;
ll t2 = (ll)(a[i].imag() / l + 0.5) % mod;
ll t3 = (ll)(b[i].real() / l + 0.5) % mod;
res[i] = (((t1 << 30) + (t2 << 15) + t3) % mod + mod) % mod;
}
}
void finv(ll *f, ll *g, int n) {
if (n == 1) return g[0] = qpow(f[0], mod - 2), void();
finv(f, g, n + 1 >> 1);
static ll a[MAXN];
int l = getrev(n);
mtt(f, g, a, n, l);
for (int i = 0; i < n; i++) a[i] = mod - a[i];
a[0] = (a[0] + 2) % mod;
mtt(g, a, g, n, l);
for (int i = n; i < l; i++) g[i] = 0;
}
inlinevoid drt(ll *f, ll *g, int n) {
for (int i = 1; i < n; i++) g[i - 1] = f[i] * i % mod;
g[n - 1] = 0;
}
inlinevoid igt(ll *f, ll *g, int n) {
g[0] = 0;
for (int i = 1; i < n; i++) g[i] = f[i - 1] * qpow(i, mod - 2) % mod;
}
inlinevoid fln(ll *f, ll *g, int n) {
static ll a[MAXN], b[MAXN];
drt(f, a, n), finv(f, b, n);
int l = getrev(n);
for (int i = n; i < l; i++) a[i] = 0;
mtt(a, b, a, n, l);
igt(a, g, n);
for (int i = n; i < l; i++) g[i] = 0;
}
void fexp(ll *f, ll *g, int n) {
if (n == 1) return g[0] = 1, void();
fexp(f, g, n + 1 >> 1);
static ll a[MAXN];
fln(g, a, n);
int l = getrev(n);
a[0] = (f[0] + 1 - a[0] + mod) % mod;
for (int i = 1; i < n; i++) a[i] = (f[i] - a[i] + mod) % mod;
mtt(g, a, g, n, l);
for (int i = n; i < l; i++) g[i] = 0;
}