# 描述

http://www.lydsy.com/JudgeOnline/problem.php?id=2194

$c[k]=\sum_{i=k}^{n-1}a[i]b[i-k]$.

# 分析

$$c[i]=\sum_{j=0}^ia[j]b[i-j]$$

1.把a倒置.

$$\sum_{j=k}^na[i]b[i-k]$$

$$原式\Longleftrightarrow\sum_{i=k}^na'[n-i][b[i-k]$$

$$\sum_{i=0}^{n-k}a'[n-(i+k)]b[(i+k)-k]$$

$$\sum_{i=0}^{n-k}a'[n-i-k]b[i]$$

$$c[k]=\sum_{i=0}^{n-k}a'[n-i-k]b[i]$$

$$c[k]=A[n-k]$$

2.把b倒置

$$\sum_{i=k}^na[i]b[i-k]$$

$$原式\Longleftrightarrow\sum_{i=k}^na[i]b'[n-i+k]$$

$$\sum_{i=0}^{n-k}a[i+k]b'[n_(i+k)+k]$$

$$\sum_{i=0}^{n-k}a[i+k]b'[n-i]$$.

$$\sum_{i=0}^{n+k}a[i+k]b'[n-i]$$.

$$c[k]=\sum_{i=0}^{n+k}a[n+k-i]b'[i]$$

$$c[k]=B[n+k]$$

 1 #include <bits/stdc++.h>
2 using namespace std;
3
4 const int N=1e5+5,maxn=N<<2;
5 const double pi=acos(-1.0);
6 int n;
7 int rev[maxn];
8 int f[N],f_[N],g[N],ans[N];
9 struct cp{
10     double r,i;
11     cp(double r=0,double i=0):r(r),i(i){}
12     cp operator + (const cp &x) const { return cp(r+x.r,i+x.i); }
13     cp operator - (const cp &x) const { return cp(r-x.r,i-x.i); }
14     cp operator * (const cp &x) const { return cp(r*x.r-i*x.i,r*x.i+i*x.r);}
15 }a[maxn],b[maxn],A[maxn];
16 void brc(int &n){
17     memset(rev,-1,sizeof rev);
18     int k=1,l=0;
19     while(k<n) k<<=1, l++;
20     n=k;
21     for(int i=1;i<n-1;i++){
22         if(rev[i]!=-1) continue;
23         int x=i,y=0,m=l;
24         while(m--) y<<=1, y|=(x&1), x>>=1;
25         rev[i]=y, rev[y]=i;
26     }
27 }
28 void dft(cp *a,int n,int op){
29     for(int i=1;i<n-1;i++) A[rev[i]]=a[i];
30     for(int i=1;i<n-1;i++) a[i]=A[i];
31     for(int m=2;m<=n;m<<=1){
32         cp wn(cos(2.0*pi/m*op),sin(2.0*pi/m*op));
33         for(int i=0;i<n;i+=m){
34             cp w(1); int k=m>>1;
35             for(int j=0;j<k;j++){
36                 cp u=a[i+j],t=w*a[i+j+k];
37                 a[i+j]=u+t;
38                 a[i+j+k]=u-t;
39                 w=w*wn;
40             }
41         }
42     }
43     if(op==-1)for(int i=0;i<n;i++) a[i].r/=n;
44 }
45 void fft(int *x,int *y,int *ans,int la,int lb){
46     int len=la+lb-1;
47     brc(len);
48     for(int i=0;i<n;i++) a[i]=cp(x[i]), b[i]=cp(y[i]);
49     dft(a,len,1); dft(b,len,1);
50     for(int i=0;i<len;i++) a[i]=a[i]*b[i];
51     dft(a,len,-1);
52     for(int i=0;i<len;i++) ans[i]=a[i].r+0.5;
53 }
54 int main(){
55     scanf("%d",&n);
56     for(int i=0;i<n;i++) scanf("%d%d",&f[i],&g[i]);
57     for(int i=0;i<n;i++) f_[i]=g[n-1-i];
58     fft(f,f_,ans,n,n);
59     for(int i=n-1;i<n+n-1;i++) printf("%d\n",ans[i]);
60     return 0;
61 }
View Code

posted @ 2016-06-13 17:55  晴歌。  阅读(224)  评论(0编辑  收藏  举报