很好的一道题
记得化简,其实也可以不用枚举x属于【-100,100】,求对称轴就好了
https://www.luogu.com.cn/problem/P3723
#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N=1e6+10;
const int C=32768;
int p,x;
const double PI = acos(-1);
struct Complex{
double x,y;
Complex operator+(const Complex &t){return {x+t.x,y+t.y};}
Complex operator-(const Complex &t){return {x-t.x,y-t.y};}
Complex operator*(const Complex &t){return {x*t.x-y*t.y,x*t.y+y*t.x};}
}a[N],b[N],a2[N],c[N];
int n,m,tot=1,bit,r[N];
void fft(Complex a[],int inv){
for(int i=0;i<tot;i++){
if(i<r[i])swap(a[i],a[r[i]]);
}
for(int mid=1;mid<tot;mid*=2){
auto w1=Complex({cos(PI/mid),inv*sin(PI/mid)});
for(int i=0;i<tot;i+=mid*2){
auto wk=Complex({1,0});
for(int j=0;j<mid;j++,wk=wk*w1){
auto x=a[i+j],y=wk*a[i+j+mid];
a[i+j]=x+y;
a[i+j+mid]=x-y;
}
}
}
if(inv==1)return;
for(int i=0;i<tot;i++)a[i].x/=tot,a[i].y/=tot;
}
long long ans,res=1e9;
int suma,sumb;
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>x;
a[i+n].x=a[i].x=x;
ans+=x*x;
suma+=x;
}
for(int i=1;i<=n;i++){
cin>>x;
b[n-i+1].x=x;
ans+=x*x;
sumb+=x;
}
// x=sumb-suma;
// ans+=n*x*x+2*n*x*(suma-sumb);
while(tot<=n*3)tot*=2,bit++;
for(int i=0;i<tot;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(bit-1));
fft(a,1),fft(b,1);
for(int i=0;i<tot;i++){
a[i]=a[i]*b[i];
}
fft(a,-1);
for(int i=1+n;i<=n+n;i++){
for(int x=-m;x<=m;x++){
res=min(res,-2*(int)(a[i].x+0.5)+2*x*(suma-sumb)+n*x*x);
}
}
cout<<ans+res;
}