多项式重工业基地
作者:互联网
未完待续……(只是给自己存个板子)
快速傅里叶变换
#include <bits/stdc++.h>
using namespace std; const int maxn = 2e6 + 1e2;
struct Cp
{
double x, y;
inline Cp operator +(const Cp& b) const { return (Cp) { x + b.x, y + b.y }; }
inline Cp operator -(const Cp& b) const { return (Cp) { x - b.x, y - b.y }; }
inline Cp operator *(const Cp& b) const { return (Cp) { x * b.x - y * b.y, x * b.y + y * b.x }; }
};
namespace fft
{
int l, r[maxn], lim = 1; long double Pi = acos(-1.0); inline void init(int n)
{
while (lim <= n) lim <<= 1, l++; for (int i = 0; i < lim; i++)
{
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
}
inline void fft(Cp A[], int v)
{
for (int i = 0; i < lim; i++) if (i < r[i]) swap(A[i], A[r[i]]);
Cp Wn, w, x, y; for (int mid = 1; mid < lim; mid <<= 1)
{
Wn = (Cp) { cos(Pi / mid), v * sin(Pi / mid) }; for (int R = mid << 1, j = 0; j < lim; j += R)
{
w = (Cp) { 1, 0 }; for (int k = 0; k < mid; k++, w = w * Wn)
{
x = A[j + k], y = w * A[j + mid + k], A[j + k] = x + y, A[j + mid + k] = x - y;
}
}
}
for (int i = 0; i < lim; i++) (v > 0) ? (A[i].x /= lim, A[i].y /= lim) : (A[i].x *= lim, A[i].y /= lim);
}
inline void conv(Cp A[], Cp B[], Cp C[]) { for (int i = 0; i < lim; i++) C[i] = A[i] * B[i]; }
}
Cp A[maxn], B[maxn], C[maxn]; int n, m; int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cin >> n >> m;
for (int i = 0; i <= n; i++) cin >> A[i].x;
for (int i = 0; i <= m; i++) cin >> B[i].x;
fft::init(n + m), fft::fft(A, 1), fft::fft(B, 1), fft::conv(A, B, C), fft::fft(C, -1);
for (int i = 0; i <= n + m; i++) cout << int(C[i].x + 0.5) << ' '; return 0;
}
快速数论变换:
#include <bits/stdc++.h>
using namespace std; const int maxn = 5e5 + 1e2, mod = 998244353, g = 3, h = 332748118;
namespace fft
{
int l, r[maxn], lim = 1; inline void init(int n)
{
while (lim <= n) lim <<= 1, l++; for (int i = 0; i < lim; i++)
{
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
}
inline int fpow(int x, int k)
{
int res = 1; while (k) { if (k & 1) res = 1ll * res * x % mod; x = 1ll * x * x % mod; k >>= 1; } return res;
}
inline void fft(int A[], int v)
{
for (int i = 0; i < lim; i++) if (i < r[i]) swap(A[i], A[r[i]]);
int Wn, w, x, y; for (int mid = 1; mid < lim; mid <<= 1)
{
Wn = fpow(v == 1 ? g : h, (mod - 1) / (mid << 1)); for (int R = mid << 1, j = 0; j < lim; j += R)
{
w = 1; for (int k = 0; k < mid; k++, w = 1ll * w * Wn % mod)
{
y = 1ll * w * A[j + mid + k] % mod, A[j + mid + k] = (A[j + k] - y) % mod, A[j + k] = (A[j + k] + y) % mod;
}
}
}
int inv = fpow(lim, mod - 2);
for (int i = 0; i < lim; i++) (v > 0) ? (A[i] = A[i] * 1ll * inv % mod) : (A[i] = A[i] * 1ll * lim % mod);
}
inline void conv(int A[], int B[], int C[]) { for (int i = 0; i < lim; i++) C[i] = 1ll * A[i] * B[i] % mod; }
}
int A[maxn], B[maxn], C[maxn]; int n, m; int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cin >> n >> m;
for (int i = 0; i <= n; i++) cin >> A[i];
for (int i = 0; i <= m; i++) cin >> B[i];
fft::init(n + m), fft::fft(A, 1), fft::fft(B, 1), fft::conv(A, B, C), fft::fft(C, -1);
for (int i = 0; i <= n + m; i++) cout << (C[i] + mod) % mod << ' '; return 0;
}
快速莫比乌斯/沃尔什变换:
namespace fwt
{
int lim; inline void init(int n) { lim = 1; while (lim < n) lim <<= 1; }
inline void OR(modint f[], modint t)
{
for (int mid = 1; mid < lim; mid <<= 1) for (int R = (mid << 1), j = 0; j < lim; j += R)
for (int k = 0; k < mid; k++) f[j + k + mid] += f[j + k] * t;
}
inline void AND(modint f[], modint t)
{
for (int mid = 1; mid < lim; mid <<= 1) for (int R = (mid << 1), j = 0; j < lim; j += R)
for (int k = 0; k < mid; k++) f[j + k] += f[j + k + mid] * t;
}
inline void XOR(modint f[], modint t)
{
for (int mid = 1; mid < lim; mid <<= 1) for (int R = (mid << 1), j = 0; j < lim; j += R)
for (int k = 0; k < mid; k++)
{
f[j + k] += f[j + mid + k], f[j + mid + k] = f[j + k] - f[j + mid + k] - f[j + mid + k], f[j + k] *= t, f[j + mid + k] *= t;
}
}
inline void CONV(modint A[], modint B[], modint C[]) { for (int i = 0; i < lim; i++) C[i] = A[i] * B[i]; }
}
modint 类(乱入):
int ttmp; struct modint
{
int x; modint(int o) { x = o; } modint() { x = 0; }
inline modint &operator =(int o) { return x = o, *this; }
inline modint &operator +=(modint b) { return x = (ttmp = x + b.x) >= mod ? ttmp - mod : ttmp, *this; }
inline modint &operator -=(modint b) { return x = (ttmp = x - b.x) < 0 ? ttmp + mod : ttmp, *this; }
inline modint &operator *=(modint b) { return x = 1ll * x * b.x % mod, *this; }
inline modint operator ^(int k)
{
modint res = 1, xx = x; while (k) { if (k & 1) res *= xx; k >>= 1, xx = xx * xx; } return res;
}
inline modint operator /=(modint b) { return x = 1ll * x * ((b ^ (mod - 2)).x) % mod, *this; }
inline modint operator /=(int b) { return x = 1ll * x * (modint(b) ^ (mod - 2)).x, *this; }
inline modint operator +(modint b) const { return modint((ttmp = x + b.x) >= mod ? ttmp - mod : ttmp); }
inline modint operator -(modint b) const { return modint((ttmp = x - b.x) < 0 ? ttmp + mod : ttmp); }
inline modint operator *(modint b) const { return modint(1ll * x * b.x % mod); }
inline modint operator /(modint b) const { return modint(1ll * x * ((b ^ (mod - 2)).x) % mod); }
};
未完待续……敬请期待(bushi
标签:int,多项式,fft,lim,modint,inline,mod,基地,重工业 来源: https://www.cnblogs.com/Linshey/p/14400905.html