HDU3949 XOR
? 解题记录 ? ? HDU ? ? 线性基 ?    2018-04-02 23:33:03    465    0    0

 
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.
 


Sample Input
2
2
1 2
4
1 2 3 4
3
1 2 3
5
1 2 3 4 5
 


Sample Output
Case #1:
1
2
3
-1
Case #2:
0
1
2
3
-1Hint
If you choose a single number, the result you get is the number you choose.
Using long long instead of int because of the result may exceed 2^31-1.
 


Author
elfness
 


Source
 


Recommend
xubiao
 

本题是线性基的一个小运用。问的是第k大的异或值。考虑到如何直观的表达第K大。如果每一位上的1都互不影响,那么我们按贪心策略从高位小的基底一直枚举到高位大的基底一定是从小到大地在枚举答案了。具体的说:我们可以对每一个基底操作,枚举这个基底的每一位,如果该位上为1,则用对应位置的基底去异或该基底,依次递归下去。这样操作完之后我们的线性基每一位上一定只有一个1了。比如:

1 0 1 1     1 0 0 0    1 0 0 0

0 1 0 1     0 1 0 1    0 1 0 0

0 0 1 1 -> 0 0 1 1->0 0 1 0

0 0 0 1     0 0 0 1     0 0 0 1

Code:

#include<cstdio>
#include<cstring>
#include<stack>
#define LL long long
using namespace std;
const LL stmax = 1LL << 62;

stack<LL > stk;int top, n, m, zero, t;LL a;
struct Linear_Base {
    LL LB[70];
    void init() {memset(LB, 0, sizeof(LB));}
    void insert(LL u) {
        register int p = 62;
        for(register LL i = stmax; i > 0; i >>= 1, --p) {
            if(!(u & i)) continue;
            if(!LB[p]) {LB[p] = u;break;}
            else u ^= LB[p];
        }
        if(~p) return ;
        zero = 1;
    }
    void gauss() {
        for(register int i = 62; i > 0; --i) {
            for(register LL p = 1LL << i - 1, j = i - 1; j >= 0; --j, p >>= 1)
                if(LB[i] & p) LB[i] ^= LB[j];
            if(LB[i]) stk.push(LB[i]);
        }
        if(LB[0]) stk.push(LB[0]);
        memset(LB, 0, sizeof(LB));
        while(!stk.empty()) LB[top++] = stk.top(), stk.pop();
    }
    LL query(LL k) {
        LL ans = 0;
        for(register LL p = stmax, j = 62; p > 0; --j, p >>= 1)
            if(k & p) if(j < top) ans ^= LB[j]; else return -1;
        return ans;
    }
}A;

int main() {
    scanf("%d", &t);
    for(register int cs = 1; cs <= t; ++cs) {
        A.init();
        scanf("%d", &n), zero = 0, top = 0;
        for(register int i = 1; i <= n; ++i)
            scanf("%lld", &a), A.insert(a);
        A.gauss(), scanf("%d", &m);
        printf("Case #%d:\n", cs);
        for(register int i = 1; i <= m; ++i) {
            scanf("%lld", &a);
            printf("%lld\n", a - zero ? A.query(a - zero) : 0);
        }
    }
    return 0;
}


上一篇: 洛谷P3567 [POI2014]KUR-Couriers

下一篇: 洛谷P4151 [WC2011]最大XOR和路径

465 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航