FFT板子
Posted hn-wrp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FFT板子相关的知识,希望对你有一定的参考价值。
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define R register
#define LL long long
const int inf=0x3f3f3f3f;
const int MAXN=4e6+10;
const double PI=3.1415926535897932384626;
inline int read() {
char a=getchar(); int x=0,f=1;
for(;a>‘9‘||a<‘0‘;a=getchar()) if(a==‘-‘) f=-1;
for(;a>=‘0‘&&a<=‘9‘;a=getchar()) x=x*10+a-‘0‘;
return x*f;
}
namespace Data {
struct Complex {
double x, y;
Complex (double X = 0, double Y = 0) { x = X; y = Y; }
};
inline Complex operator + (Complex a, Complex b) { return Complex( a.x+b.x, a.y+b.y ); }
inline Complex operator - (Complex a, Complex b) { return Complex( a.x-b.x, a.y-b.y ); }
inline Complex operator * (Complex a, Complex b) { return Complex( a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x ); }
}
namespace Operation {
using Data::Complex;
int lim,bit;
int rev[MAXN];
inline void FFT ( Complex *A, int type ) {
for(R int i=0; i<lim; i++) if( i < rev[i] ) swap( A[i] , A[rev[i]] );
for(R int dep=1; dep < lim; dep<<=1) {
Complex Wn( cos(PI/dep), type*sin(PI/dep) );
for(R int len=dep<<1,j=0 ;j<lim ;j+=len) {
Complex w(1,0);
for(R int k=0 ;k<dep ;k++,w=w*Wn) {
Complex x=A[j+k],y=w*A[j+dep+k];
A[j+k]=x+y;
A[j+dep+k]=x-y;
}
}
}
if(type==-1)
for(R int i=0;i<=lim;i++) A[i].x/=lim;
}
inline void Turn(Complex *A,Complex *B,int n,int m) {
lim=1; bit=0;
while( lim <= n+m ) lim<<=1,bit++;
for(R int i=0; i<lim; i++) rev[i] = (rev[i>>1]>>1)|((i&1)<<(bit-1));
FFT(A,1); FFT(B,1);
for(R int i=0; i<=lim; i++) A[i] = A[i] * B[i];
FFT(A,-1);
}
}
using namespace Data;
int n,m;
Complex f[MAXN],g[MAXN];
int main() {
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n=read(); m=read();
for(R int i=0;i<=n;i++) f[i].x=read();
for(R int i=0;i<=m;i++) g[i].x=read();
Operation::Turn(f,g,n,m);
for(R int i=0;i<=n+m;i++) printf("%d ",(int)(f[i].x+0.5));
return 0;
}
以上是关于FFT板子的主要内容,如果未能解决你的问题,请参考以下文章