2019年牛客多校第一场 H题XOR 线性基

该博客介绍了如何利用线性基方法解决一个编程竞赛中的问题:求n个数中所有异或和为0的子集大小之和。博主首先解释了题意,然后阐述了转换思路,即计算每个数对子集大小的贡献。通过找到所有数的线性基,博主说明非基底元素的贡献是2n-r-1,基底元素x的贡献则需要进一步利用新的线性基进行计算。最后,博主提供了代码实现,并提醒注意枚举x时应使用原始数据,避免影响其他可能的解决方案。
摘要由CSDN通过智能技术生成

题目链接

传送门

题意

n n n个数中子集内所有数异或为 0 0 0的子集大小之和。

思路

对于子集大小我们不好维护,因此我们可以转换思路变成求每个数的贡献。
首先我们将所有数的线性基的基底 b b b求出来(设秩为 r r r),然后非基地元素的贡献就是 2 n − r − 1 2^{n-r-1} 2nr1,即选择这个数然后其他所有非基底元素都可以选择或者不选择两种方法,选择非基底元素后我们再从基底里面挑出能过把它异或为 0 0 0的数选出来就可以达到题目的要求。
对于基底元素 x x x,我们将非基底的 n − r n-r nr个元素再跑一个线性基 o t h e r other other出来,然后用 b b b中除去 x x x外的剩余元素和 o t h e r other other构成的新的线性基 D D D来进行选择看能不能将 x x x消掉(理由同上),如果可以消掉那么 x x x的贡献是 2 n − ∣ D ∣ − 1 2^{n-|D|-1} 2nD1
注意后面枚举 x x x要用最初始题目给的数而不能用 b b b中的数,反例:
7 7 7  8 8 8  6 6 6  8 8 8  9 9 9  8 8 8
如果直接从基里挑会直接把 7 7 7 6 6 6(的第 2 , 3 2,3 2,3个二进制位)一起挑出来, 6 6 6本来还可以提供个 0110 0110 0110的,但是往基里一放就被7搞没了。
我说的可能不太清楚,那么可以看这篇博客~

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n, r, tot;
bool vis[maxn];
vector<LL> vec;
LL a[maxn], b[105], other[105], tmp[105];

LL qpow(LL x, int n) {
    LL res = 1;
    while(n) {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

bool ins(LL x, LL base[]) {
    for(int i = 63; i >= 0; --i) {
        if(x & (1LL << i)) {
            if(base[i]) x ^= base[i];
            else {
                base[i] = x;
                return true;
            }
        }
    }
    return false;
}

int main() {
    while(~scanf("%d", &n)) {
        r = tot = 0;
        vec.clear();
        for(int i = 0; i <= 63; ++i) b[i] = other[i] = 0;
        for(int i = 1; i <= n; ++i) {
            scanf("%lld", &a[i]);
            vis[i] = 0;
            if(ins(a[i], b)) vis[i] = 1, ++r, vec.emplace_back(a[i]);
        }
        if(r == n) {
            printf("0\n");
            continue;
        }
        LL ans = qpow(2, n - r - 1) * (n - r) % mod;;
        for(int i = 1; i <= n; ++i) {
            if(vis[i]) continue;
            ins(a[i], other);
        }
        for(int i = 0; i < vec.size(); ++i) {
            tot = 0;
            for(int j = 0; j <= 63; ++j) tmp[j] = 0;
            for(int j = 0; j < vec.size(); ++j) {
                if(i == j) continue;
                if(ins(vec[j], tmp)) ++tot;
            }
            for(int j = 0; j <= 63; ++j) {
                if(other[j] && ins(other[j], tmp)) ++tot;
            }
            if(!ins(vec[i], tmp)) {
                ans = (ans + qpow(2, n - tot - 1)) % mod;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值