# 快速数论变换(NTT)小结

## 原根

### 阶

$a,p$互素，且$p>1$

$\delta_7(2)=3$

$2^1 \equiv 2 \pmod{7}$

$2^2 \equiv 4 \pmod{7}$

$2^3 \equiv 1 \pmod{7}$

### 原根

$p$是正整数，$a$是整数，若$\delta_p(a)$等于$\phi(p)$，则称$a$为模$p$的一个原根

$\delta_7(3)=6=\phi(7)$，因此$3$是模$7$的一个原根

• $P$为素数，假设一个数$g$$P$的原根，那么$g^i \mod P (1<g<P,0<i<P)$的结果两两不同

$\omega_n \equiv g^\frac{p-1}{n} \mod p$

$p$建议取$998244353$，它的原根为$3$

## 实现

NTT求卷积代码:

#include<cstdio>
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<21, stdin), p1 == p2) ? EOF : *p1++)
#define swap(x,y) x ^= y, y ^= x, x ^= y
#define LL long long
const int MAXN = 3 * 1e6 + 10, P = 998244353, G = 3, Gi = 332748118;
char buf[1<<21], *p1 = buf, *p2 = buf;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, limit = 1, L, r[MAXN];
LL a[MAXN], b[MAXN];
inline LL fastpow(LL a, LL k) {
LL base = 1;
while(k) {
if(k & 1) base = (base * a ) % P;
a = (a * a) % P;
k >>= 1;
}
return base % P;
}
inline void NTT(LL *A, int type) {
for(int i = 0; i < limit; i++)
if(i < r[i]) swap(A[i], A[r[i]]);
for(int mid = 1; mid < limit; mid <<= 1) {
LL Wn = fastpow( type == 1 ? G : Gi , (P - 1) / (mid << 1));
for(int j = 0; j < limit; j += (mid << 1)) {
LL w = 1;
for(int k = 0; k < mid; k++, w = (w * Wn) % P) {
int x = A[j + k], y = w * A[j + k + mid] % P;
A[j + k] = (x + y) % P,
A[j + k + mid] = (x - y + P) % P;
}
}
}
}
int main() {
for(int i = 0; i <= N; i++) a[i] = (read() + P) % P;
for(int i = 0; i <= M; i++) b[i] = (read() + P) % P;
while(limit <= N + M) limit <<= 1, L++;
for(int i = 0; i < limit; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (L - 1));
NTT(a, 1);NTT(b, 1);
for(int i = 0; i < limit; i++) a[i] = (a[i] * b[i]) % P;
NTT(a, -1);
LL inv = fastpow(limit, P - 2);
for(int i = 0; i <= N + M; i++)
printf("%d ", (a[i] * inv) % P);
return 0;
}


posted @ 2018-05-02 16:07  自为风月马前卒  阅读(5105)  评论(13编辑  收藏  举报