分析
https://www.luogu.org/blog/dedicatus545/solution-p3723
代码
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define int long long
const int inf = 1e17+9;
int n,m,r[2000100],len;
struct node {
double x,y;
};
node a[2000100],b[2000100];
inline node operator + (const node x,const node y){return (node){x.x+y.x,x.y+y.y};}
inline node operator - (const node x,const node y){return (node){x.x-y.x,x.y-y.y};}
inline node operator * (const node x,const node y){return (node){x.x*y.x-x.y*y.y,x.x*y.y+x.y*y.x};}
inline void fft(node a[],int f){
int i,j,k;
for(i=0;i<n;i++)if(i<r[i])swap(a[i],a[r[i]]);
for(i=1;i<n;i<<=1){
node wn=(node){cos(pi/i),f*sin(pi/i)};
for(j=0;j<n;j+=(i<<1)){
node w=(node){1,0},p,q;
for(k=0;k<i;k++,w=w*wn){
p=a[j+k],q=a[i+j+k]*w;
a[j+k]=p+q,a[i+j+k]=p-q;
}
}
}
if(f==-1)for(i=0;i<n;i++)a[i].x=a[i].x/n;
}
signed main(){
int i,j,k;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%lf",&a[i].x);
for(i=1;i<=n;i++)scanf("%lf",&b[i].x);
int res=0,Ans=inf,sum=0;
for(i=1;i<=n;i++)res+=a[i].x*a[i].x+b[i].x*b[i].x,sum+=2*(a[i].x-b[i].x);
reverse(a+1,a+n+1);
for(i=1;i<=n;i++)a[i+n]=a[i];
int n1=3*n;
for(n=1;n<=n1;n<<=1)len++;
for(i=0;i<n;i++)r[i]=((r[i>>1]>>1)|((i&1)<<(len-1)));
fft(a,1),fft(b,1);
for(i=0;i<n;i++)a[i]=a[i]*b[i];
fft(a,-1);
n=n1/3;
for(i=1;i<=n;i++)
for(j=-m;j<=m;j++)
Ans=min(Ans,res+j*j*n+j*sum-2*(int)(a[i+n].x+0.5));
cout<<Ans<<"\n";
return 0;
}