hdu3949 XOR (线性基求第k小的数)详解

Problem Description

XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A XOR B, then for each bit of C, we can get its value by check the digit of corresponding position in A and B. And for each digit, 1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 0 = 0. And we simply write this operator as ^, like 3 ^ 1 = 2,4 ^ 3 = 7. XOR is an amazing operator and this is a question about XOR. We can choose several numbers and do XOR operatorion to them one by one, then we get another number. For example, if we choose 2,3 and 4, we can get 2^3^4=5. Now, you are given N numbers, and you can choose some of them(even a single number) to do XOR on them, and you can get many different numbers. Now I want you tell me which number is the K-th smallest number among them.

Input

First line of the input is a single integer T(T<=30), indicates there are T test cases.
For each test case, the first line is an integer N(1<=N<=10000), the number of numbers below. The second line contains N integers (each number is between 1 and 10^18). The third line is a number Q(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,......KQ.

Output

For each test case,first output Case #C: in a single line,C means the number of the test case which is from 1 to T. Then for each query, you should output a single line contains the Ki-th smallest number in them, if there are less than Ki different numbers, output -1.

这题是我刚开始学线性基时做的题,关于线性基,不懂的同学可以看这位大佬的博客(大学线代可有好好听的基本都能看懂)

https://blog.sengxian.com/algorithms/linear-basis

这题在这位大佬的博客里也有讲,不过我在他的代码的基础上加了详细的注释,有兴趣的同学可以看看

#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stdio.h>
#include <map>

#include<ctype.h>
using namespace std;
typedef long long LL;

const int MAX_N = 100000 + 50, MAX_BASE = 60;

int n, fg0 = 0; // fg0标记可不可以异或出0

LL a[MAX_N], b[MAX_BASE + 3]; // a存原数组,b存对角矩阵

vector<LL> mymap; //将不为0的b[i]加进去

void prepare() {
    int cnt = 0;
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; ++i){
        for (int j = MAX_BASE; j >= 0; --j){
            if (a[i] >> j & 1) {
                if (b[j]){
                    a[i] ^= b[j];
                } else {
                    b[j] = a[i];
                    cnt++;
                    for (int k = j - 1; k >= 0; --k){
                        if (b[k] && ((b[j] >> k) & 1)){
                            b[j] ^= b[k];
                        }
                    }
                    for (int k = j + 1; k <= MAX_BASE; ++k){
                        if ((b[k] >> j) & 1){
                            b[k] ^= b[j];
                        }
                    }
                    break;
                }
            }
        }
    }
    if(cnt != n){ // 对角矩阵中1的个数小于n,说明存在线性相关组,所以可异或出0
        fg0 = 1;
    }

    mymap.clear();
    for (int i = 0; i <= MAX_BASE; ++i){
        if (b[i]){ //第i上不为0则放入数组
            mymap.push_back(b[i]);
        }
    }
}

LL Query(LL k){
    if(fg0){
        k--;
    }
    int len = mymap.size();
    if(k >= (1LL << len)){ //对角矩阵中有n个1,则该基可异或出2^n - 1个数,所以当k >= (2 ^ n - 1)时,结果不存在, 注意1一定要强转为LL,当初因为这个wa了好几发
        return -1;
    }  
    LL ans = 0;
    for(int i = 0; i < len; i++){ // 注意!!mymap中的数不一定是1,2,4,8这样连续的,可能是1,2,8,16这样(在对角矩阵中1的个数小于n的情况下)
        if((k >> i) & 1){
            ans ^= mymap[i];
        }
    }
    
    return ans;
}

int main() {
    int t;
    scanf("%d", &t);
    for(int m = 1; m <= t; m++){
        fg0= 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            scanf("%lld", &a[i]);
        }

        prepare();
        int q;
        scanf("%d", &q);
        printf("Case #%d:\n", m);
        while(q--){
            LL k;
            scanf("%lld", &k);
            printf("%lld\n", Query(k));
        }
    }
    return 0;
}
    
 
 

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/88560863