其他分享
首页 > 其他分享> > BZOJ 3527: [Zjoi2014]力 FFT_卷积

BZOJ 3527: [Zjoi2014]力 FFT_卷积

作者:互联网

Code:

#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
 
#define setIO(s) freopen(s".in","r",stdin) 
 
const int maxn = 300000; 
const double pi=3.1415926535898;
int t, n, len=1, l, r[maxn*2];
 
struct Cpx{
    double x,y;
    Cpx (double t1=0,double t2=0){x=t1,y=t2;}
}A[maxn<<1],B[maxn<<1],C[maxn<<1],B2[maxn<<1],A2[maxn<<1],C2[maxn]; 
 
Cpx operator+(Cpx a,Cpx b){ return Cpx(a.x+b.x,a.y+b.y);}
Cpx operator - (Cpx a, Cpx b){ return Cpx(a.x-b.x, a.y-b.y); }
Cpx operator * (Cpx a, Cpx b){ return Cpx(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x); }
 
void FFT(Cpx *a,int n,int flag){
    for(int i=0;i<n;++i) if(i<r[i]) swap(a[i],a[r[i]]);
    for(int mid=1;mid<n;mid<<=1){
        Cpx wn(cos(pi/mid), flag*sin(pi/mid)),x,y;
        for(int j=0;j<n;j+=(mid<<1)){
            Cpx w(1,0);
            for(int k=0;k<mid;++k) {
                x=a[j+k],y=w*a[j+mid+k];
                a[j+k]=x+y;
                a[j+mid+k]=x-y;
                w=w*wn;
            }
        }
    }
}
int main(){
    //setIO("input"); 
    scanf("%d",&n);
    for(int i = 1;i <= n; ++i) scanf("%lf",&A[i].x),A2[n - i + 1] = A[i].x; 
    for(int i = 1;i <= n; ++i) B[i].x = 1.000 / (double) ((1.0 * i) * (1.0 * i)) ;
    while(len < n + n) len <<= 1,++l; 
    for(int i = 0;i < len; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l-1));  
    FFT(A,len,1),FFT(B,len,1),FFT(A2,len,1); 
    for(int i = 0;i < len ; ++i) C[i] = A[i] * B[i]; 
    for(int i = 0;i < len ; ++i) C2[i] = A2[i] * B[i]; 
    FFT(C,len,-1),FFT(C2,len,-1); 
    for(int i = 0;i < len; ++i) C2[i].x /= len,C[i].x /= len; 
    for(int i = 1;i <= n; ++i) printf("%.3f\n",C[i].x - C2[n - i + 1].x); 
    return 0; 
} 

  

标签:const,int,double,FFT,Cpx,3527,maxn,Zjoi2014,include
来源: https://www.cnblogs.com/guangheli/p/10540590.html