XOR

题目描述

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 234=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.

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.

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.

代码

按照高消的套路,我们最终会得到大小为u的基底 a 数组.
a数组有一个显而易见的性质:令c[i]表示a[i]二进制最高位的位置,有$c[1] > c[2] >… > c[u]$(由高斯消元得到的a数组是按降序排列的)
然后我们把询问中的k-1(二进制是从0开始计数的)进行二进制分解就好啦
如果k-1的第j位为1,就把ansa[t-j]异或一次
仔细想想就能想明白吧…
然后书上还友善地提醒了一个边界情况:

异或空间中第k小的整数与题目中所要求的第k小的整数略有不同.题目不允许$x_i$^$x_i$的运算,而异或空间中允许.造成的差别是异或空间一定包括整数0,但从$a_1,a_2,…,a_n$中选出几个不同的数异或,可能得不到0.
实际上,我们只要检查阶梯矩阵中是否存在"零行"即可判断是否可以取到0.
若不能得到0,就把k而不是k-1进行二进制分解(二进制的特性使得第一小的数表现为0)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
ull k;
ull a[10100];
//似乎不用unsigned long long?
//但是之前打挂了一个地方就把这里改了而且懒得改回来了:)

int main(){
t = read();
int cnt = 1;
while(cnt <= t){
printf("Case #%d:\n", cnt);
++ cnt;
n = read();
zero = 0;
for(int i = 1; i <= n; ++ i) a[i] = read();
for(int i = 1; i <= n; ++ i){
for(int j = i + 1; j <= n; ++ j)
if(a[j] > a[i]) swap(a[i], a[j]);
if(a[i] == 0) { n = i - 1; zero = 1; break;}
for(int j = 63; j >= 0; -- j){
//这里把 j >= 0 打成 j 了...还查不出来..蠢死算了
if((a[i] >> j) & 1){
for(int k = 1; k <= n; ++ k)//是从1开始啊
if(i != k && ((a[k] >> j) & 1)) a[k] ^= a[i];
break;
}
}
}
m = read();
ull ans;
for(int i = 1; i <= m; ++ i){
ans = 0;
k = read();
if(zero) -- k;//就是这里啦
if(k >= (ull)1 << n) { printf("-1\n"); continue;}
//这里之前打成了 k > ((ull)1 << n) - 1 ...脑回路清奇呢
for(int j = n - 1; j >= 0; -- j)
if(k >> j & 1) ans ^= a[n - j];//所以这里还是很好理解的
printf("%lld\n", ans);
}
}
return 0;
}

日常

看英文感觉巨爽