zoukankan      html  css  js  c++  java
  • HDOJ 3949 XOR(状态压缩,高斯消元)

    因为不同,所以存在。(由异或得到的想法)
    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 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.
    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
    -1

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

    思路:
    1.一个异或空间,我们一定能找出基,使得基的异或组合,等价于原数的异或组合。因为a=ba=b时,a xor b=0a xor b=0,我们把aba,b看成二进制数位的话,就可以用a把b消掉.因为a xor b xor b=aa xor b xor b=a,所以这种消元是恒等变化。(高斯消元)因为每个原数在[1,101810^{18}]中,所以可以用long long存每一行的系数。(状态压缩)
    细节:消元时按从大到小的顺序消。(从大到小也行,不过不方便讲思路)
    2.消元后,已知找到了t个主元,那么就可以把前t行抽出(之所以说行,是因为高斯消元是处理系数矩阵。但代码是用一个long long 存一行的)。因为消元后得到了一个阶梯形矩阵,若矩阵中的ai,ja_{i,j}为一主元,那么第j列就一定只剩下第i行有系数。又因为异或相当于不进位加法,那么第i个二进制位(表示2i2^i)为1,就一定比二进制上只有0~i-1有1的大。换句话说,选了第1 个数,就一定比不选第1 个数的异或值大。选了第1个数后,选第2个数,一定比不选第2 个数的异或值大.……以此类推,那么选了第1个数,就相当与排行大了2t12^{t-1}(因为后面任选),选了第2个数,就相当与排行大了2t22^{t-2}(因为后面任选)……选了第i个数,就相当与排行大了2ti2^{t-i}(因为后面任选)。那么已知排行k,通过求二进制下,那些位为1,就知道选了那些数。若k>>i&1,也就是k的第i位为1(第0位为1,即为1(为了方便说))那么就是选了a[t-i].
    3.特别地,题目不允许ai xor aia_i xor a_i,但是异或空间允许。那么当有自由元(零行)时,最小值其实为0,那么我们必须用k-1求二进制位。(前面的说法是默认没有自由元)。当无零行时,就按照上述2的方法做就行。如果用zero(bool)表示是否有零行,那么先执行k-=zero,当k>=2t2^t时无解,因为二进制下t位都为1(最大值)为2t12^t -1.

    代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N=1e4+10;
    ll a[N],bin[66],ans;
    int n,m,T,t;
    #define g getchar()
    template<class o>void qr(o &x)//快读 
    {
    	char c=g;x=0;
    	while(!('0'<=c&&c<='9'))c=g;
    	while('0'<=c&&c<='9')x=x*10+c-'0',c=g;
    }
    void write(ll x)//快写 
    {
    	if(x/10)write(x/10);
    	putchar(x%10+'0');
    }
    int main()
    {
    	bin[0]=1;for(int i=1;i<=60;i++)bin[i]=bin[i-1]<<1;//预处理以2为底的幂 
    	qr(T);
    	for(int c=1;c<=T;c++)
    	{
    		bool zero=0;
    		qr(n);
    		for(int i=1;i<=n;i++)qr(a[i]);
    		t=n;
    		for(int i=1;i<=n;i++)
    		{
    			for(int j=i+1;j<=n;j++)if(a[j]>a[i])swap(a[j],a[i]);
    			if(!a[i]){t=i-1;zero=1;break;}
    			for(int k=60;k>=0;k--)
    				if(a[i]&bin[k])//找为1的数位,a[i]&bin[k]等价于a[i]>>k&1(右移的优先级大) 
    				{
    					for(int j=1;j<=n;j++)
    						if(j!=i&&(a[j]&bin[k]))a[j]^=a[i];
    					break;
    				}
    		}
    		qr(m);
    		printf("Case #%d:
    ",c);
    		while(m--)
    		{
    			ll k;qr(k);k-=zero;
    			if(k>=bin[t]){puts("-1");continue;}//puts("abcd")等价于printf("abcd
    ");
    			ans=0;
    			for(int i=t-1;i>=0;i--)//找为1的数位,k&bin[i]等价于k>>i&1(右移的优先级大) 
    				if(k&bin[i])ans^=a[t-i];
    			write(ans);puts("");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    区块链的入门
    数组元素查找(查找指定元素第一次在数组中出现的索引)
    数组查表法之根据键盘录入索引,查找对应星期
    数组元素反转
    数组获取最大值
    数组的遍历
    数组操作的两个常见小问题越界和空指针
    方法重载练习比较数据是否相等
    方法之根据键盘录入的数据输出对应的乘法表
    方法之根据键盘录入的行数和列数,在控制台输出星形
  • 原文地址:https://www.cnblogs.com/zsyzlzy/p/12373932.html
Copyright © 2011-2022 走看看