loj#2020 「AHOI / HNOI2017」礼物 ntt

时间:2022-01-24 21:01:54

loj#2020 「AHOI / HNOI2017」礼物

链接

bzoj没\(letex\),差评

loj

luogu

思路

最小化\(\sum\limits_1^n(a_i-b_i)^2\)

设改变量为k

\(\sum\limits_1^n(a_i-(b_i+k))^2\)

\(\sum\limits_1^n(a_i^2-2*a_i*(b_i+k)+(b_i+k)^2)\)

\(\sum\limits_1^n(a_i^2-2*a_i*b_i-2*a_i*k+b_i^2+2*b_i*k+k^2)\)

\(\sum\limits_1^n(a_i^2+b_i^2)+k\sum\limits_1^{n}(2b_i-2a_i+k^2)-\sum\limits_1^n 2*a_i*b_i\)

枚举k求前面最大,ntt求后面最大

代码

#include <bits/stdc++.h>
using namespace std;
const int N=4e5+7,mod=998244353;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,m,k,limit=1,l,a[N],b[N],r[N];
int q_pow(int a,int b) {
int ans=1;
while(b) {
if(b&1) ans=1LL*ans*a%mod;
a=1LL*a*a%mod;
b>>=1;
}
return ans;
}
void ntt(int *a,int type) {
for(int i=0;i<limit;++i)
if(i<r[i]) swap(a[i],a[r[i]]);
for(int mid=1;mid<limit;mid<<=1) {
int Wn=q_pow(3,(mod-1)/(mid<<1));
for(int i=0;i<limit;i+=(mid<<1)) {
for(int j=0,w=1;j<mid;++j,w=1LL*w*Wn%mod) {
int x=a[i+j],y=1LL*w*a[i+j+mid]%mod;
a[i+j]=(x+y)%mod;
a[i+j+mid]=(x+mod-y)%mod;
}
}
}
if(type==-1) {
reverse(&a[1],&a[limit]);
int inv=q_pow(limit,mod-2);
for(int i=0;i<=limit;++i) a[i]=1LL*a[i]*inv%mod;
}
}
int main() {
n=read(),k=read();
m=n<<1;
n--,m--;
int sum_a=0,sum_b=0,sum=0;
for(int i=0;i<=n;++i) a[i]=read(),sum_a+=a[i],sum+=a[i]*a[i];
for(int i=n;i>=0;--i) b[i+n+1]=b[i]=read(),sum_b+=b[i],sum+=b[i]*b[i];
while(limit<=n+m) limit<<=1,l++;
for(int i=0;i<=limit;++i)
r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
ntt(a,1),ntt(b,1);
for(int i=0;i<=limit;++i) a[i]=1LL*a[i]*b[i]%mod;
ntt(a,-1);
int ma=0;
for(int i=n;i<=n+n;++i) ma=max(ma,a[i]);
int ans=0x7fffffff;
for(int k=-100;k<=100;++k) {
int tmp=sum+k*(2*sum_b-2*sum_a+(n+1)*k)-2*ma;
ans=min(ans,tmp);
}
cout<<ans<<"\n";
return 0;
}