zoukankan      html  css  js  c++  java
  • FFT板子

    woc......FFT这玩意儿真坑......

    一上午除了打了几遍板子什么也没干......真是废了......

    你要加油啊......

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn=270010;
     7 const double pi=acos(-1.0),eps=1e-4;
     8 struct Complex{
     9     double a,b;
    10     Complex(double a=0.0,double b=0.0):a(a),b(b){}
    11     Complex operator+(const Complex &x)const{return Complex(a+x.a,b+x.b);}
    12     Complex operator-(const Complex &x)const{return Complex(a-x.a,b-x.b);}
    13     Complex operator*(const Complex &x)const{return Complex(a*x.a-b*x.b,a*x.b+b*x.a);}
    14 }A[maxn],B[maxn];
    15 void FFT(Complex*,int,int);
    16 int n,m,N=1;
    17 int main(){
    18     scanf("%d%d",&n,&m);
    19     n++;m++;
    20     while(N<n+m)N<<=1;
    21     for(int i=0;i<n;i++)scanf("%lf",&A[i].a);
    22     for(int i=0;i<m;i++)scanf("%lf",&B[i].a);
    23     FFT(A,N,1);
    24     FFT(B,N,1);
    25     for(int i=0;i<N;i++)A[i]=A[i]*B[i];
    26     FFT(A,N,-1);
    27     for(int i=0;i<n+m-1;i++)printf("%d ",(int)(A[i].a+eps));
    28     return 0;
    29 }
    30 void FFT(Complex *A,int n,int tp){
    31     for(int i=1,j=0;i<n-1;i++){
    32         int k=N;
    33         do{
    34             k>>=1;
    35             j^=k;
    36         }while(j<k);
    37         if(i<j)swap(A[i],A[j]);
    38     }
    39     for(int k=2;k<=n;k<<=1){
    40         Complex wn(cos(-tp*2*pi/k),sin(-tp*2*pi/k));
    41         for(int i=0;i<n;i+=k){
    42             Complex w(1.0,0.0);
    43             for(int j=0;j<(k>>1);j++,w=w*wn){
    44                 Complex a(A[i+j]),b(w*A[i+j+(k>>1)]);
    45                 A[i+j]=a+b;
    46                 A[i+j+(k>>1)]=a-b;
    47             }
    48         }
    49     }
    50     if(tp<0)for(int i=0;i<n;i++)A[i].a/=n;
    51 }
    View Code

    挖个大坑:

    COGS2216 你猜是不是KMP

  • 相关阅读:
    169. Majority Element
    283. Move Zeroes
    1331. Rank Transform of an Array
    566. Reshape the Matrix
    985. Sum of Even Numbers After Queries
    1185. Day of the Week
    867. Transpose Matrix
    1217. Play with Chips
    766. Toeplitz Matrix
    1413. Minimum Value to Get Positive Step by Step Sum
  • 原文地址:https://www.cnblogs.com/hzoier/p/6351433.html
Copyright © 2011-2022 走看看