CSU 1911 Card Game 快速沃尔什变换(FWT)模板题

题意:给你两个二进制数的集合,给出q次询问,输出两个集合之间元素或的值等于查询值的种数。

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
const int maxn = 1<<18;
LL a[maxn],b[maxn];
int n,m;
char s[20];

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++)
            {
                int x=a[i+j],y=a[i+j+d];
                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++)
            {
                int x=a[i+j],y=a[i+j+d];
                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]=a[i]*b[i];
    UFWT(a,n);
}

int main()
{
    int t,q;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        scanf("%d%d",&n,&m);
        int l,x;
        for(int i=0;i<n;i++)
        {
            x=0;
            scanf("%s",s);
            l=strlen(s);
            for(int j=0;j<l;j++) x=(x<<1)+s[j]-'0';
            a[x]++;
        }
        for(int i=0;i<n;i++)
        {
            x=0;
            scanf("%s",s);
            l=strlen(s);
            for(int j=0;j<l;j++) x=(x<<1)+s[j]-'0';
            b[x]++;
        }
        solve(a,b,(1<<m));
        scanf("%d",&q);
        printf("Case #%d:\n",cas);
        while(q--)
        {
            scanf("%s",s);
            x=0;
            l=strlen(s);
            for(int i=0;i<l;i++) x=(x<<1)+s[i]-'0';
            printf("%lld\n",a[x]);
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值