1
0
Fork 0

Add number/ntt.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-09-02 21:05:50 +08:00
parent c42d033dcc
commit 5017d889b4
1 changed files with 61 additions and 0 deletions

61
number/ntt.cc Normal file
View File

@ -0,0 +1,61 @@
template <ll M>
void ntt(vector<MLL<M>>& y, bool idft) {
using mll = MLL<M>;
int n = y.size();
vector<int> rev(n);
for (int i = 0; i < n; ++i) {
rev[i] = rev[i >> 1] >> 1;
if (i & 1) {
rev[i] |= n >> 1;
}
}
for (int i = 0; i < n; ++i) {
if (i < rev[i]) {
swap(y[i], y[rev[i]]);
}
}
vector<mll> roots = { 0, 1 };
if (roots.size() < n) {
int k = lsp(roots.size());
roots.resize(n);
for (; (1 << k) < n; ++k) {
mll e = qpow<mll>(31, 1 << lsp(M - 1) - k - 1);
for (int i = 1 << k - 1; i < (1 << k); ++i) {
roots[2 * i] = roots[i];
roots[2 * i + 1] = roots[i] * e;
}
}
}
for (int h = 2; h <= n; h <<= 1) {
for (int j = 0; j < n; j += h) {
for (int k = j; k < j + h / 2; ++k) {
mll u = y[k], t = roots[k - j + h / 2] * y[k + h / 2];
y[k] = u + t;
y[k + h / 2] = u - t;
}
}
}
if (idft) {
reverse(y.begin() + 1, y.end());
for (int i = 0; i < n; ++i) {
y[i] /= n;
}
}
}
template <ll M>
vector<MLL<M>> multiply(const vector<MLL<M>>& a, const vector<MLL<M>>& b) {
using mll = MLL<M>;
vector<mll> A(a.begin(), a.end()), B(b.begin(), b.end());
int n = 1;
while (n < a.size() + b.size()) n <<= 1;
A.resize(n), B.resize(n);
ntt<M>(A, false), ntt<M>(B, false);
for (int i = 0; i < n; ++i) {
A[i] *= B[i];
}
ntt<M>(A, true);
return A;
}