Hdu-3949:xor (linear basis)

Source: Internet
Author: User
Tags cas

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 the 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.

Inputfirst line of the input was a single integer T (t<=30), indicates there was 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. Outputfor each test case,first output case #C: In a single line,c means the number of the "the" test case which are 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

221 241 2 3 431 2 351 2 3 4 5

Sample Output

Case #1:123-1case #2:0123-1

Test Instructions: given n number, all sets of different xor and, in order to get from small to large k, does not exist then output-1.

idea: We know that the linear base can be expressed in a number of not more than 64, representing the XOR of all sets, then 0 of the site is not considered, we ask for K, is equivalent to the binary ... It's OK.

The linear basis is first obtained and the P array is given. It then ignores the 0, and the previous p is the result of the subsequent effect. One of the points to note is that 0, because the linear basis we do not consider 0, so 0 is considered separately, if the size of the linear base and the original array size, it can be expressed, then k--;

#include <bits/stdc++.h>#definell Long Long#defineRep (i,a,b) for (int i=a;i<=b;i++)#defineREP2 (I,A,B) for (int i=a;i>=b;i--)using namespacestd;Const intmaxn=100010; ll p[ the],x;intMain () {intt,n,q,cas=0; scanf ("%d",&T);  while(t--) {scanf ("%d",&N); Rep (I,0, the) p[i]=0; Rep (I,1, N) {scanf ("%lld",&x); Rep2 (J, the,0){                if(x& (1ll<<j)) {                    if(P[j]) x^=P[j]; Else{p[j]=x; Break;} }}} ll num=0, Ans,k; Rep (I,0, the)if(P[i]) {P[num++]=P[i]; Rep (J,i+1, the)if((p[j]>>i) &1) p[j]^=P[i]; } scanf ("%d",&Q); printf ("Case #%d:\n",++Cas);  while(q--) {scanf ("%lld", &k);if(N!=num) k--;//here,notice! Considering the existence of 0            if(k>= (1ll<<num)) puts ("-1"); Else{ans=0; Rep (J,0, the) {                    if(k& (1LL&LT;&LT;J)) ans^=p[j];//can not add, or with XOR, may have tails, offset each other} printf ("%i64d\n", ans); }        }    }    return 0;}

Hdu-3949:xor (linear basis)

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.