Description
给出n个数qi,给出Fj的定义如下:
令Ei=Fi/qi,求Ei.
Input
第一行一个整数n。
接下来n行每行输入一个数,第i行表示qi。
n≤100000,0<qi<1000000000
Output
n行,第i行输出Ei。与标准答案误差不超过1e-2即可。
Sample Input
5
4006373.885184
15375036.435759
1717456.469144
8514941.004912
1410681.345880
Sample Output
-16838672.693
3439.793
7509018.566
4595686.886
10903040.872
Solution
题目让求的是:
[E_k=sum_{i=1}^{k-1}q_ifrac{1}{(k-i)^2}-sum_{i=k+1}^{n}q_ifrac{1}{(i-k)^2}
]
令:
[A_i=q_i,B_i=q_{n-i+1},C_i=frac{1}{i^2}
]
其实(B)就是(q)数组的(reverse)。
那么答案就是:
[egin{align}
E_k&=sum_{i=1}^{k-1}A_iC_{k-i}-sum_{i=k+1}^{n}A_iC_{i-k}\
&=sum_{i=1}^{k-1}A_iC_{k-i}-sum_{i=k+1}^nB_{n-i+1}C_{i-k}\
&=sum_{i=1}^{k-1}A_iC_{k-i}-sum_{i=1}^{n-k}B_{n-k-i+1}C_{i}\
end{align}
]
然后两项都是卷积的形式,直接(FFT)优化就好了。
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('
');}
const int maxn = 1e6+10;
#define lf double
struct complex {
lf r,i;
complex () {}
complex (lf _r,lf _i) {r=_r,i=_i;}
complex operator - (const complex &rhs) const {return complex(r-rhs.r,i-rhs.i);}
complex operator + (const complex &rhs) const {return complex(r+rhs.r,i+rhs.i);}
complex operator * (const complex &rhs) const {return complex(r*rhs.r-i*rhs.i,r*rhs.i+i*rhs.r);}
};
int pos[maxn],N,bit,n;
complex a[maxn],b[maxn],c[maxn];
const lf pi = acos(-1);
void fft(complex *r,int op) {
for(int i=0;i<N;i++) if(pos[i]>i) swap(r[i],r[pos[i]]);
for(int i=1;i<N;i<<=1) {
complex wn=complex(cos(pi/i),op*sin(pi/i));
for(int j=0;j<N;j+=(i<<1)) {
complex w=complex(1,0);
for(int k=0;k<i;k++,w=w*wn) {
complex x=r[j+k],y=w*r[i+j+k];
r[j+k]=x+y,r[i+j+k]=x-y;
}
}
}
if(op==-1) for(int i=0;i<N;i++) r[i].r/=N,r[i].i=0;
}
int main() {
read(n);
N=1,bit=0;while(N<=(n<<1)) N<<=1,bit++;
for(int i=0;i<N;i++) pos[i]=pos[i>>1]>>1|((i&1)<<(bit-1));
for(int i=1;i<=n;i++) scanf("%lf",&a[i].r),b[n-i+1]=a[i],c[i].r=1.00/i/i; //注意这里千万不要写成1.00/(i*i),否则会爆int
fft(a,1),fft(b,1),fft(c,1);
for(int i=0;i<N;i++) a[i]=a[i]*c[i],b[i]=b[i]*c[i];
fft(a,-1),fft(b,-1);
for(int i=1;i<=n;i++) printf("%.3lf
",a[i].r-b[n-i+1].r);
return 0;
}