Intersection(线性基)

题目描述:
Bobo has two sets of integers A = {a1,a2,…,an}and B = {b1,b2,…,bn}
He says that x∈span(A) (or span(B)) if and only if there exists a subset of A (or B) whose exclusive-or sum equals to x.
Bobo would like to know the number of x where x∈span(A) and x∈span(B) hold simultaneously.
输入描述:
The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains an integer n.
The second line contains n integers a1,a2,…an
The third line contains n integers b1,b2,…,bn
*1≤n≤50

  • 0 ≤ ai, bi ≤ 2^60
  • The number of test cases does not exceed 5000.
    输出描述:
    For each case, output an integer which denotes the result.

题意:
给出A,B两个数组,定义x在Span(A)集合里为:A的一个子集的异或和为x(可以是空集),求有多少个x即在Span(A)也在Span(B)中。
题解:
构造A,B线性基,对A中的每个基在B中访问并存储,更新答案
code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[61],b[61];
bool insert(ll x,ll *t)
{
    for(int i=60;i>=0;i--)
    {
        if(!((1ll<<i)&x))continue;
        if(!t[i])
        {
            t[i]=x;
            return false;
        }
        else x^=t[i];
        if(!x)return true;
    }
    return true;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        ll p;
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&p);
            insert(p,a);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&p);
            insert(p,b);
        }
        ll ans=1;
        for(int i=60;i>=0;i--)
        {
            if(!b[i])continue;
            if(insert(b[i],a))ans<<=1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Macarons_i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值