2018 ACM-ICPC 沈阳站 Problem I. Distance Between Sweethearts FWT

本文深入探讨了FFT算法在处理大规模数据集时的高效性,详细解释了快速沃尔特变换(FWT)及其逆变换(UFWT)的过程,通过实例演示了如何使用FFT进行多项式乘法,特别关注于算法的实现细节和优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int maxn=1e4+5;
int UIB,UAB,UGB,UIG,UAG,UGG,I[maxn],A[maxn],G[maxn],pre_op,now_op;
ll temp[maxn],num[2][maxn],res[maxn],ans;
void FWT(ll a[],int n)
{
    for(int d=1;d<n;d<<=1)
        for(int m=d<<1,i=0;i<n;i+=m)
            for(int j=0;j<d;++j)
            {
                ll x=a[i+j];
                ll y=a[i+j+d];
                a[i+j]=x+y;
                a[i+j+d]=x-y;
                //xor:a[i+j]=x+y,a[i+j+d]=x-y;
                //and:a[i+j]=x+y;
                //or:a[i+j+d]=x+y;
            }
}
void UFWT(ll a[],int n)
{
    for(int d=1;d<n;d<<=1)
        for(int m=d<<1,i=0;i<n;i+=m)
            for(int j=0;j<d;++j)
            {
                ll x=a[i+j];
                ll y=a[i+j+d];
                a[i+j]=(x+y)>>1;
                a[i+j+d]=(x-y)>>1;
                //xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
                //and:a[i+j]=x-y;
                //or:a[i+j+d]=y-x;
            }
}
void solve(ll a[],ll b[],int n)
{
    FWT(a,n);
    FWT(b,n);
    for(int i=0;i<n;++i)
        a[i]*=b[i];
    UFWT(a,n);
}
void pre_fill(ll a[],int b[],int n)
{
    for(int i=0;i<n;++i)
        a[i]=(ll)b[i];
}
int get_length(int n)
{
    int res=1;
    while(res<=n)
        res<<=1;
    return res;
}
void up_date(int a[],int n,int m,int k)
{
    if(k)
        for(int i=0;i<=n;++i)
        {
            if(i-k>=0)
                ++a[i^(i-k)];
            if(i+k<=m)
                ++a[i^(i+k)];
        }
    else
        a[0]=n+1;
}
void init()
{
    ans=now_op=pre_op=0;
    memset(I,0,sizeof(I));
    memset(A,0,sizeof(A));
    memset(G,0,sizeof(G));
    memset(res,0,sizeof(res));
    memset(num,0,sizeof(num));
}
int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);
    int t,n,m,cas=1;
    scanf("%d",&t);
    while(t--&&scanf("%d%d%d%d%d%d",&UIB,&UAB,&UGB,&UIG,&UAG,&UGG)!=EOF)
    {
        init();
        if(UIB>UIG)
            swap(UIB,UIG);
        if(UAB>UAG)
            swap(UAB,UAG);
        if(UGB>UGG)
            swap(UGB,UGG);
        n=max(max(UIG,UAG),UGG);
        m=get_length(n);
        for(int i=0;i<=n;++i)
        {
            now_op=pre_op^1;
            up_date(I,UIB,UIG,i);
            up_date(A,UAB,UAG,i);
            up_date(G,UGB,UGG,i);
            pre_fill(num[now_op],I,m);
            pre_fill(temp,A,m);
            solve(num[now_op],temp,m);
            pre_fill(temp,G,m);
            solve(num[now_op],temp,m);
            for(int j=0;j<m;++j)
                res[j^i]+=num[now_op][j]-num[pre_op][j];
            pre_op=now_op;
        }
        for(int i=0;i<m;++i)
            ans+=res[i]*i;
        printf("Case #%d: %lu\n",cas++,ans);
    }
    return 0;
}
/*
10
3 1 2 4 3 3
1 2 1 2 1 3
3 2 5 4 3 5
2000 2000 2000 2000 2000 2000
2000 2000 2000 2000 2000 2000
2000 2000 2000 2000 2000 2000
2000 2000 2000 2000 2000 2000
2000 2000 2000 2000 2000 2000
2000 2000 2000 2000 2000 2000
2000 2000 2000 2000 2000 2000
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值