loj#2020. 「HNOI2017」礼物(FFT)

先放代码,日后更(*3)

============================2018.3.24UPD============================
题面在这里

做法

感觉并没有什么可讲的。直接转化式子以后fft就行。自己推式子,注意要推得好看一点(雾
upd:妈呀我感觉我太不负责了。还是贴个比较详细的link吧。。。//专业甩锅

代码

/*
*   转化式子;
*   多项式套路:翻转后fft;
*/
#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x); i<=(y); i++)
#define per(i,x,y) for (int i=(x); i>=(y); i--)
#define N 400010
#define ll long long
#define inf 1000000000000000ll
#define sqr(x) ((x)*(x))
using namespace std;
int m,x[N],y[N],n1,r[N],len,n; ll ans=0,sumx,sumy,C,ret=inf;
const long double pi=acos(-1);
struct comp{
    long double a,b;
    comp(){ a=0,b=0; } comp(long double x,long double y){ a=x,b=y; }
}a[N],b[N],c[N],w[2][N];
comp operator + (comp x,comp y) { return comp(x.a+y.a,x.b+y.b); }
comp operator - (comp x,comp y) { return comp(x.a-y.a,x.b-y.b); }
comp operator * (comp x,comp y) { return comp(x.a*y.a-x.b*y.b,x.a*y.b+x.b*y.a); }
void fft(comp a[N],int all,int fl){
    rep (i,0,all) if (i<r[i]) swap(a[i],a[r[i]]);
    int n=2,m; comp t;
    while (n<=all){
        m=n/2;
        for (int i=0; i<all; i+=n)
            for (int k=0; k<m; k++){
                t=w[fl][all/n*k]*a[i+m+k];
                a[i+m+k]=a[i+k]-t;
                a[i+k]=a[i+k]+t;
            }
        n<<=1;
    }
    if (fl) rep (i,0,n-1) a[i].a/=all;
}
void solve(){
    rep (i,1,n1) x[i]=0;
    reverse(y+1,y+1+2*n1);
    len=0; n=1;
    for (; n<=4*n1; n<<=1,len++);
    rep (i,0,n-1) r[i]=(r[i>>1]>>1)|((i&1)<<len-1);
    rep (i,0,n-1) w[0][i]=comp(cos(2*i*pi/n),sin(2*i*pi/n));
    rep (i,0,n-1){ w[1][i]=w[0][i]; w[1][i].b=-w[1][i].b; }
    rep (i,0,n-1) a[i]=comp(x[i],0);
    rep (i,0,n-1) b[i]=comp(y[i],0);
    fft(a,n,0); fft(b,n,0);
    rep (i,0,n-1) c[i]=a[i]*b[i];
    fft(c,n,1);
    rep (i,2*n1+2,3*n1+1) ret=min(ret,-(ll)(c[i].a+0.5)*2);
}
int main(){
    /*freopen("C.in","r",stdin);
    freopen("C.out","w",stdout);*/
    scanf("%d%d",&n1,&m);
    rep (i,1,n1){ scanf("%d",&x[i]); x[i+n1]=x[i]; }
    rep (i,1,n1){ scanf("%d",&y[i]); y[i+n1]=y[i]; }
    sumx=sumy=0;
    rep (i,1,n1){ ans+=sqr(x[i])+sqr(y[i]); sumx+=x[i]; sumy+=y[i]; }
    if (sumx>sumy){ swap(x,y); swap(sumx,sumy); }
    C=(ll)(-(long double)(sumx-sumy)/n1+0.5);
    //printf("C=%lld\n",C);
    solve();
    printf("%lld\n",ans+ret+2ll*(sumx-sumy)*C+n1*sqr(C));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值