HDU 3949 XOR (linear base, template)

Source: Internet
Author: User
Description

XOR is a kind of bit operator, we define that as follow:for the binary base number A and B, let C=a XOR b Bit of C, we can get it value by check for 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 is given N numbers, and you can choose some of them (even a single number) to doing XOR on them, and you can get the man Y different numbers. Now I want your tell me which number is the k-th smallest number among them.

Input

First line of the input was a single integer T (t<=30), indicates there is t test cases.

For each test case, the first line was 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

The For each test Case,first output case #C: A line,c means the number of the "the" the "Test Case" which is from 1 to T. Then for each query, you should output a contains the ki-th smallest number in them, if there is 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
-1

Test Instructions

Given an array of length n n N, there is a q Q-q query, and each time a query is made of a subset of the array or the result of the K K K decimal value is what.

Ideas

Inserts all the elements of the array into a linear base and then rebuild them: starting from the high to the low, enumeration J<i J < I j for each I I I, and if d[i] d [i] d[i] is 1 1 1, make it different Or once d[j] d [j] D[j] (eliminates the current bit).

After the operation, for D[i] d [i] d[i] only I I i this bit is 1 1 1, the remaining bits are 0 0 0, we put the non 0 elements in the order from small to large in P p p.

At the time of querying the K-K k large difference or value, we construct according to the binary system K K K, for 1 1 1 bit, will be different or on the corresponding p[i] p [i] p[i], think about such a construction method is correct.

It is important to note that the result of the difference or out of the linear basis is non-zero, and the value of K K K in the query may be zero, so we need to construct a linear basis to determine whether the current number can be different or 0 0 0, and then the final query to the results of a special sentence.

AC Code

 #include <bits/stdc++.h> #define IO \ Ios::sync_with_stdio (FALSE) ;                  \ cin.tie (0);
\ cout.tie (0);
using namespace Std;
typedef long Long LL;
const int MAXN = 1e5 + 10;

int n, q;
    Class Linearbase {public:const static int maxn = 64-1;
    LL D[MAXN], P[MAXN], tot;
        void Clear () {tot = 0;
        memset (d, 0, sizeof (d));
    memset (p, 0, sizeof (p));
                } bool Insert (LL val) {for (int i = maxn-1; I >= 0; i--) {if (Val >> i) & 1) { if</

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.