1. 程式人生 > >loj#2020. 「AHOI / HNOI2017」禮物

loj#2020. 「AHOI / HNOI2017」禮物

題意:給定xy陣列求
\(\sum_{i=0}^{n-1}(x_i+y_{(i+k)%n}+c)^2\)
題解:先化簡可得
\(n*c^2+2*\sum_{i=0}^{n-1}x_i-y_i+\sum_{i=0}^{n-1}x_i^2+y_i^2-2*\sum_{i=0}x_i*y_{(i+k)%n}\)
主要問題是求最後一項的最大值,把x反過來重複一遍即可fft,相當於\(2*n...n...1\)\(1....n\)fft,第2*n+1項到n+2項就是不斷平移的答案

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const ull ba=233;
const db eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=200000+10,inf=0x3f3f3f3f;

struct cd{
    db x,y;
    cd(db _x=0.0,db _y=0.0):x(_x),y(_y){}
    cd operator +(const cd &b)const{
        return cd(x+b.x,y+b.y);
    }
    cd operator -(const cd &b)const{
        return cd(x-b.x,y-b.y);
    }
    cd operator *(const cd &b)const{
        return cd(x*b.x - y*b.y,x*b.y + y*b.x);
    }
    cd operator /(const db &b)const{
        return cd(x/b,y/b);
    }
}x[N<<3],y[N<<3];
int rev[N<<3];
void getrev(int bit)
{
    for(int i=0;i<(1<<bit);i++)
        rev[i]=(rev[i>>1]>>1) | ((i&1)<<(bit-1));
}
void fft(cd *a,int n,int dft)
{
    for(int i=0;i<n;i++)
        if(i<rev[i])
            swap(a[i],a[rev[i]]);
    for(int step=1;step<n;step<<=1)
    {
        cd wn(cos(dft*pi/step),sin(dft*pi/step));
        for(int j=0;j<n;j+=step<<1)
        {
            cd wnk(1,0);
            for(int k=j;k<j+step;k++)
            {
                cd x=a[k];
                cd y=wnk*a[k+step];
                a[k]=x+y;a[k+step]=x-y;
                wnk=wnk*wn;
            }
        }
    }
    if(dft==-1)for(int i=0;i<n;i++)a[i]=a[i]/n;
}
int a[N],b[N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int sz=0;
    while((1<<sz)<2*n)sz++,sz++;
    getrev(sz);
    int ans=0,bb=0;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),x[n+1-i].x=x[2*n+1-i].x=a[i],ans+=a[i]*a[i],bb+=2*a[i];
    for(int i=1;i<=n;i++)scanf("%d",&b[i]),y[i].x=b[i],ans+=b[i]*b[i],bb-=2*b[i];
    int c=-bb/(2*n);
    ans+=min(min(n*c*c+bb*c,n*(c+1)*(c+1)+bb*(c+1)),n*(c-1)*(c-1)+bb*(c-1));
    fft(x,(1<<sz),1);fft(y,(1<<sz),1);
    for(int i=0;i<(1<<sz);i++)x[i]=x[i]*y[i];
    fft(x,(1<<sz),-1);
    int ma=0;
    for(int i=n+2;i<=2*n+1;i++)ma=max(ma,(int)(x[i].x+0.5));//,printf("%d\n",(int)(x[i].x+0.5));
    printf("%d\n",ans-2*ma);
    return 0;
}
/********************

********************/