P3803 【模板】多项式乘法(FFT) · znzryb
## 思路讲解
[https://www.bilibili.com/video/BV1za411F76U/](https://www.bilibili.com/video/BV1za411F76U/)
## AC代码
[https://www.luogu.com.cn/record/229525769](https://www.luogu.com.cn/record/229525769)
::: 源代码
```cpp
// by Gospel_rock
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
#define all(vec) vec.begin(),vec.end()
#define pb push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define lson(var) (var<<1)
#define rson(var) (var<<1|1)
#define fsp(x) fixed<<setprecision(x)
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
#endif
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<DB>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using cd=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=(ll)1e9+7; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
inline void __();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>lT;
// while(lT--)
__();
return 0;
}
/*
*/
// 理论讲解 https://www.bilibili.com/video/BV1za411F76U/
// 参考(抄的) https://cp-algorithms.com/algebra/fft.html
// 多项式乘法 https://www.luogu.com.cn/record/229525769
using cd=complex<double>;
void fft(vector<cd> &a,bool invert){
ll n=SZ(a);
if(n==1) return;
vector<cd> a0(n/2),a1(n/2); // a0 even a1 odd
FOR(i,0,n/2-1){
a0[i]=a[2*i];
a1[i]=a[2*i+1];
}
fft(a0,invert),fft(a1,invert);
double ang=2*pi/n*(invert?-1:1);
cd w(1),wlen={cos(ang),sin(ang)}; // wlen 是步进长度
FOR(i,0,n/2-1){
// A(x)=Ae(x^2)+x*Ao(x^2) A(-x)=Ae(x^2)-x*Ae(x^2)
// a[i]=a0[i]+a1[i] -a[i]=a0[i]-a1[i];
// a[i] 和 a[i+n/2] 是单位圆上相反的一对
a[i]=a0[i]+w*a1[i];
a[i+n/2]=a0[i]-w*a1[i];
if(invert){
// 每次都除了2,一共有log2(n)层,所以一共除了2^log2(n)=n
a[i]/=2;
a[i+n/2]/=2;
}
w*=wlen;
}
}
vll multiply(vll &a,vll &b){
vector<cd> fa(all(a)),fb(all(b));
ll n=1;
while(n<SZ(a)+SZ(b)){
n<<=1;
}
fa.resize(n);fb.resize(n);
fft(fa,false);fft(fb,false);
FOR(i,0,n-1) fa[i]*=fb[i];
fft(fa,true);
vll res(n);
// 避免精度损失
FOR(i,0,n-1) res[i]=llround(fa[i].real());
return res;
}
inline void __(){
cin>>N>>M;
vll A(N+1),B(M+1);
FOR(i,0,N) cin>>A[i];
FOR(i,0,M) cin>>B[i];
vll ans=multiply(A, B);
// while (!ans.empty() && ans.back()==0) {
// ans.pop_back();
// }
if(ans.empty()) cout<<0;
FOR(i,0,N+M) cout<<ans[i]<<" ";
cout<<"\n";
}
/*
WA https://www.luogu.com.cn/record/229525510
AC https://www.luogu.com.cn/record/229525769
*/
```
:::
## 心路历程(WA,TLE,MLE……)
那么WA了几发,发现是因为这个多项式乘法他要求的输出个数是N+M。
不用去除后导0。