luogu3803(FFT/NTT模板)

题目链接

https://www.luogu.org/problemnew/show/P3803

题解

来贴 $FFT$ 模板

这个和位数为 $1e6$ 的高精度乘法优化差不多。。

多项式乘完之后项数会变多,所以要对原多项式进行拓展,视作高次项的系数为 $0$ ,然后由于 $FFT$ 需要以 $2^n$ 作为运算的基数,所以内存要开大一点。。

然后由于数字比较小可以用 $NTT$ 做




代码

$FFT$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y)/2
#define NM 2100005
#define nm 105
using namespace std;
const double pi=acos(-1);
const ll inf=1e9+7;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}




int n,m;
ll a[NM],b[NM];


#define cp complex<double>
struct FFT{
int n,bit,rev[NM];
cp a[NM],b[NM];
void fft(cp*a,int f){
inc(i,0,n-1)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int k=1;k<n;k<<=1){
cp t(cos(pi/k),f*sin(pi/k));
for(int i=0;i<n;i+=k<<1){
cp w(1,0);
for(int j=0;j<k;j++,w*=t){
cp x=a[i+j],y=w*a[i+j+k];
a[i+j]=x+y;a[i+j+k]=x-y;
}
}
}
}
int plu(ll*_a,ll*_b,int p,int m){
inc(i,0,p)a[i]=_a[i];
inc(i,0,m)b[i]=_b[i];
for(n=p+m+1,bit=0;succ(bit)<n;bit++);n=succ(bit);
inc(i,1,n-1)rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1));
fft(a,1);fft(b,1);inc(i,0,n-1)a[i]*=b[i];
fft(a,-1);inc(i,0,n-1)a[i]/=n;
inc(i,0,n-1)_a[i]=a[i].real()+0.5;
inc(i,0,n-1)a[i]=b[i]=0;
return p+m;
}
}fft;


int main(){
n=read();m=read();
inc(i,0,n)a[i]=read();
inc(i,0,m)b[i]=read();
n=fft.plu(a,b,n,m);
inc(i,0,n)printf("%lld ",a[i]);putchar('\n');
return 0;
}



$NTT$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y)/2
#define NM 2100005
#define nm 105
using namespace std;
const double pi=acos(-1);
const ll inf=998244353;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}



int n,m;
ll a[NM],b[NM];

ll qpow(ll x,ll t){return t?qpow(sqr(x)%inf,t>>1)*(t&1?x:1ll)%inf:1ll;}

struct FFT{
int n,m,bit,rev[NM];
ll b[NM],invn;
void fft(ll*a,int f){
inc(i,0,n-1)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int k=1;k<n;k<<=1){
ll t=qpow(3,(inf-1)/k/2);if(f==-1)t=qpow(t,inf-2);
for(int i=0;i<n;i+=k<<1){
ll w=1;
for(int j=0;j<k;j++,w=w*t%inf){
ll x=a[i+j],y=w*a[i+j+k]%inf;
a[i+j]=(x+y)%inf;a[i+j+k]=(x-y+inf)%inf;
}
}
}
}
int plu(ll*a,ll*_b,int p,int m){
inc(i,0,m)b[i]=_b[i];
for(n=p+m+1,bit=0;succ(bit)<n;bit++);n=succ(bit);
invn=qpow(n,inf-2);
inc(i,0,n-1)rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1));
fft(a,1);fft(b,1);inc(i,0,n-1)a[i]=a[i]*b[i]%inf;
fft(a,-1);inc(i,0,n-1)a[i]=a[i]*invn%inf;
inc(i,0,n-1)b[i]=0;
return m+p;
}
}fft;


int main(){
n=read();m=read();
inc(i,0,n)a[i]=read();
inc(i,0,m)b[i]=read();
n=fft.plu(a,b,n,m);
inc(i,0,n)printf("%lld ",a[i]);putchar('\n');
return 0;
}



另附常用NTT模数

说明: $n=r2^k+1$ ,$g$ 为 $n$ 的原根

n r k g
3 1 1 2
5 1 2 2
17 1 4 3
97 3 5 5
193 3 6 5
257 1 8 3
7681 15 9 17
12289 3 12 11
40961 5 13 3
65537 1 16 3
786433 3 18 10
5767169 11 19 3
7340033 7 20 3
23068673 11 21 3
104857601 25 22 3
167772161 5 25 3
469762049 7 26 3
1004535809 479 21 3
2013265921 15 27 31
2281701377 17 27 3
3221225473 3 30 5
75161927681 35 31 3
77309411329 9 33 7
206158430209 3 36 22
2061584302081 15 37 7
2748779069441 5 39 3
6597069766657 3 41 5
39582418599937 9 42 5
79164837199873 9 43 5
263882790666241 15 44 7
1231453023109121 35 45 3
1337006139375617 19 46 3
3799912185593857 27 47 5
4222124650659841 15 48 19
7881299347898369 7 50 6
31525197391593473 7 52 3
180143985094819841 5 55 6
1945555039024054273 27 56 5
4179340454199820289 29 57 3