题目大意
![这里写图片描述](https://img-blog.csdn.net/20170422101359773?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2h1bmtpdGxhdQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
解题思路
我们把平方拆开,得出增加的值和旋转的方式无关,是一个二次函数可以直接算出,现在的问题是如何旋转。我们发现和位置有关,是对应位置xy的乘积和,我们只要将x翻转就变成卷积,将x倍长即可以考虑所有情况。
对于卷积,我们可以用fft来求。
code
using namespace std;
typedef complex<double> C;
LL const mn=5*1e4+9,inf=1e9;LD pi=acos(-1.0);
LL n,m,lim,x[mn],y[mn],re[mn*4];
LL calc(LL a,LL b,LL c){
return a*c*c+b*c;
}
C A[mn*4],B[mn*4],c[mn*4];
void dft(C *a,int tag){
fo(i,0,lim-1)
c[re[i]]=a[i];
for(int i=2;i<=lim;i<<=1){
int half=i>>1;
fo(j,0,half-1){
C w(cos(tag*pi*j/half),sin(tag*pi*j/half));
for(int k=j;k<lim;k+=i){
C x=c[k],y=c[k+half]*w;
c[k]=x+y;c[k+half]=x-y;
}
}
}
fo(i,0,lim-1)
a[i]=c[i];
}
int main(){
//freopen("gift.in","r",stdin);
//freopen("gift.out","w",stdout);
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
scanf("%lld%lld",&n,&m);LL tmp=0,tmp2=0;
fo(i,1,n)scanf("%lld",&x[i]),tmp+=x[i]*x[i],tmp2+=x[i];
fo(i,1,n)scanf("%lld",&y[i]),tmp+=y[i]*y[i],tmp2-=y[i];
LL a=n,b=2*tmp2,c,
c1=calc(a,b,-b/(2*a)-1),
c2=calc(a,b,-b/(2*a)),
c3=calc(a,b,-b/(2*a)+1);
c=(c1<c2)?c1:c2;
c=(c<c3)?c:c3;
tmp+=c;
int bit=((n==1)?0:log(n-1)/log(2))+2;lim=1<<bit;
fo(i,0,lim-1){
int tmp=i,tmp2=0;
fo(j,1,bit)tmp2=(tmp2<<1)+(tmp&1),tmp>>=1;
re[i]=tmp2;
}
fo(i,0,n*2-1)A[i].real()=x[(i>=n)?n*2-i:n-i];
fo(i,0,n)B[i].real()=y[i+1];
dft(A,1);dft(B,1);
fo(i,0,lim-1)A[i]*=B[i];
dft(A,-1);
LL ans=-inf;
fo(i,n-1,n*2-1)ans=max(ans,A[i].real()/lim+0.5);
printf("%lld",tmp-2*ans);
return 0;
}