洛谷P3338 [ZJOI2014]力
作者:互联网
水题都不会,对卷积形式不敏感\(QwQ\)
题目大意:
给出\(n\)个数\(q_i\),定义\(f_j\)如下:
\[f_j=\sum\limits_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum\limits_{i>j}\frac{q_iq_j}{(i-j)^2}\]
求\(E_i=\frac{f_i}{q_i}\)
真的稍微观察一下就可以知道
\(E_i=\frac{f_i}{q_i}=\sum\limits_{i<j}\frac{q_i}{(i-j)^2}-\sum\limits_{i>j}\frac{q_i}{(i-j)^2}\)
我们设\(f[i]=q_i,g[i]=\frac{1}{i^2}\)
原式变化为
\[\sum\limits_{i<j}f[i]g[i-j]-\sum\limits_{i>j}f[i]g[i-j]\]
等价于
\[\sum\limits_{i=0}^{j-1}f[i]g[i-j]-\sum\limits_{i=j+1}^{n}f[i]g[i-j]\]
我们设\(f^{'}\)为\(f\)翻转后的数组,原式等于
\[\sum\limits_{i=0}^{j-1}f[i]g[i-j]-\sum\limits_{i=0}^{j-1}f^{'}[i]g[i-j]\]
这两个式子应该只有我认为它不是卷积吧\(qwq\)
#include<bits/stdc++.h>
using namespace std;
namespace red{
#define eps (1e-8)
inline int read()
{
int x=0;char ch,f=1;
for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
if(ch=='-') f=0,ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
const int N=5e5+10;
const double pi=acos(-1.0);
int n;
double f[N],rf[N],g[N],ans1[N],ans2[N];
int limit,len;
int pos[N];
struct complex
{
double x,y;
complex(double tx=0,double ty=0){x=tx,y=ty;}
inline complex operator + (const complex t) const
{
return complex(x+t.x,y+t.y);
}
inline complex operator - (const complex t) const
{
return complex(x-t.x,y-t.y);
}
inline complex operator * (const complex t) const
{
return complex(x*t.x-y*t.y,x*t.y+y*t.x);
}
}a[N],b[N];
inline void fft(complex *a,int inv)
{
for(int i=0;i<limit;++i)
if(i<pos[i]) swap(a[i],a[pos[i]]);
for(int mid=1;mid<limit;mid<<=1)
{
complex Wn(cos(pi/mid),inv*sin(pi/mid));
for(int r=mid<<1,j=0;j<limit;j+=r)
{
complex w(1,0);
for(int k=0;k<mid;++k,w=w*Wn)
{
complex x=a[j+k],y=w*a[j+k+mid];
a[j+k]=x+y;
a[j+k+mid]=x-y;
}
}
}
}
inline void work(double *f,double *g,double *ret)
{
for(int i=0;i<limit;++i)
{
a[i].x=f[i],b[i].x=g[i];
a[i].y=b[i].y=0;
}
fft(a,1);
fft(b,1);
for(int i=0;i<limit;++i)a[i]=a[i]*b[i];
fft(a,-1);
for(int i=0;i<=n;++i) ret[i]=a[i].x/limit;
}
inline void main()
{
n=read();
for(limit=1;limit<=(n<<1);limit<<=1) ++len;
for(int i=0;i<limit;++i) pos[i]=(pos[i>>1]>>1)|((i&1)<<(len-1));
for(int i=1;i<=n;++i)
{
scanf("%lf",&f[i]);
g[i]=1.0/i/i;
rf[i]=f[i];
}
reverse(rf+1,rf+n+1);
work(f,g,ans1);
work(rf,g,ans2);
for(int i=1;i<=n;++i) printf("%.3f\n",ans1[i]-ans2[n-i+1]);
}
}
signed main()
{
red::main();
return 0;
}
标签:ch,洛谷,limits,P3338,ij,ZJOI2014,frac,sum,getchar 来源: https://www.cnblogs.com/knife-rose/p/12037453.html