Hdu 3949 XOR Linear base

Source: Internet
Author: User
Tags cas

XOR Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3357 Accepted Submission (s): 1155


Problem Description XOR is a kind of bit operator, we define this as Follow:for binary base number A and B, let C=a X OR B, then for each bit of C, we can get it 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 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.
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 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 K I 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 Hint If you choose a single number, the result of you get is the number of you choose. Using a long long instead of int because of the result may exceed 2^31-1.
Author elfness
Source multi-university Training Contest 11-host by UESTC


The number of different or K-large in a series.


The Gaussian elimination element is a linear base, then the K binary is split, and the base vector is obtained from the lowest bit in sequence.


#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map > #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm&
Gt #include <math.h> #include <cmath> #include <stack> #include <iomanip> #define MEM0 (a) memset (a)
0,sizeof (a)) #define Meminf (a) memset (A,0x3f,sizeof (a)) using namespace Std;
typedef long Long LL;
typedef long double LD;
typedef double DB;  
const int maxn=10005,maxk=60,inf=0x3f3f3f3f;   
const LL LLINF=0X3F3F3F3F3F3F3F3F;
Const LD Pi=acos ( -1.0L);

ll A[MAXN],X[MAXN];
				BOOL Linear_basis_insert (ll P) {for (int i=maxk;i>=0;i--) {if ((p>>i)%2==1) {if (!x[i]) {x[i]=p;
                for (int j=i-1;j>=0;j--) if (x[j]&& ((x[i]>>j)%2==1)) x[i]^=x[j];
				for (int j=i+1;j<=maxk;j++) if ((x[j]>>i)%2==1) x[j]^=x[i];
			return true;
		} else p=p^x[i];
}} return p!=0; } int Main () {int cas,cot=0;
	BOOL Zero;
	scanf ("%d", &cas);
		while (cas--) {mem0 (x);
		Zero=true;
		cot++;
		printf ("Case #%d:\n", cot);
		int n,i,j,q,tot=0;
		ll P;
		scanf ("%d", &n);
			for (i=1;i<=n;i++) {scanf ("%lld", &a[i]);
		Zero=zero&linear_basis_insert (A[i]);
		} scanf ("%d", &q);
		for (i=0;i<=maxk;i++) if (x[i]) tot++;
			for (i=1;i<=q;i++) {scanf ("%lld", &p);
			ll Ans=0;
			if (!zero) p--;
			j=0;
				while (p) {while (!X[J]&AMP;&AMP;J&LT;=MAXK) j + +;
				if (j>maxk) {ans=-1;break;
				} if (p%2) ans^=x[j];
				j + +;
			p=p>>1;
		} printf ("%lld\n", ans);
}} return 0; }

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.