2019牛客暑期多校訓練營(第一場)H-XOR(線性基計數)

鏈接:https://ac.nowcoder.com/acm/contest/881/H
來源:牛客網
 

題目描述

Bobo has a set A of n integers a1,a2,…,an.
He wants to know the sum of sizes for all subsets of A whose xor sum is zero modulo(10^9+7).
Formally, find \left ( \sum _{S\subseteq A,[\bigoplus _{x\in S} x] = 0} |S|} \right )mod(10^9+7). Note that ⊕ denotes the exclusive-or (XOR).

輸入描述:

The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains an integer n.
The second line contains n integers a1,a2,…,an.

1\leq n\leq 10^5
0\leq a_i\leq 10^{18}
* The number of test cases does not exceed 100.
* The sum of n does not exceed 2*10^6.

輸出描述:

For each test case, print an integer which denotes the result.

示例1

輸入

1
0
3
1 2 3
2
1000000000000000000 1000000000000000000

輸出

1
3
2

題目分析:

題意要求計算n個數a[1...n]中,選出若干元素構成集合S,滿足異或和爲0的集合S的大小之和。

可以將所求轉換成計算每個元素出現在異或和爲0的集合中多少次,將每個元素的次數累加起來就是答案。

做法:

n個元素構造出一組線性基R,構成線性基的r個數暫存起來,

剩下的n-r個數字,總共構成了2^{n-r}個集合,

每個集合都能被線性基R唯一表示出來(添加若干個基後集合異或和變爲0),

n-r個元素中,每個元素出現在異或和爲0的集合中的次數爲2^{n-r-1}(自己在集合中,其他元素任意)

 

先用其他n-r個元素構造一組線性基B.

對於構成線性基的數字r_i的計數:

複製線性基B到線性基P,將線性基R中除了r_i之外其他r-1個元素加入P中,

檢驗r_i能否被P所表示,若可以,答案貢獻爲2^{n-p-1}(p表示線性基P中元素個數),

若不可以,說明r_i與其他n-1個數字均線性無關,r_i答案貢獻爲0

代碼:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

const ll mod = 1e9+7;

ll poww(ll a, ll b){
    ll res = 1;
    while(b){
        if(b&1) res = res * a %mod;
        a = a*a % mod;
        b >>= 1;
    } return res;
}

int n;
ll a[100005];

ll r[64], b[64], p[64];

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

int main(){
    while(~scanf("%d", &n)){
        vector<ll> R;
        for(int i = 0; i < 64; i ++) r[i] = b[i] = 0;
        int bcnt = 0;
        for(int i = 1; i <= n; i++){
            scanf("%lld", &a[i]);
            if(add(r, a[i]))
                R.push_back(a[i]);
            else
                bcnt += add(b, a[i]);
        }
        ll ans = poww(2,n-R.size()-1)*(n-R.size())%mod;
        for(int i = 0; i < R.size(); i++){
            memcpy(p, b, sizeof b);
            int pcnt = bcnt;
            for(int j = 0; j < R.size(); j++)
                if(j != i) pcnt += add(p, R[j]);
            if(!add(p, R[i]))
                ans += poww(2,n-pcnt-1);
        }
        ans %= mod;
        printf("%lld\n", ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章