BZOJ 2844: albus就是要第一个出场

2844: albus就是要第一个出场

Time Limit: 6 Sec  Memory Limit: 128 MB
Submit: 1169  Solved: 496
[Submit][Status][Discuss]

Description

已知一个长度为n的正整数序列A(下标从1开始), 令 S = { x | 1 <= x <= n }, S 的幂集2^S定义为S 所有子
集构成的集合。定义映射 f : 2^S -> Zf(空集) = 0f(T) = XOR A[t] , 对于一切t属于T现在albus把2^S中每个集
合的f值计算出来, 从小到大排成一行, 记为序列B(下标从1开始)。 给定一个数, 那么这个数在序列B中第1
次出现时的下标是多少呢?

Input

第一行一个数n, 为序列A的长度。接下来一行n个数, 为序列A, 用空格隔开。最后一个数Q, 为给定的数.

Output

共一行, 一个整数, 为Q在序列B中第一次出现时的下标模10086的值.
 

Sample Input

3
1 2 3
1

Sample Output

3
样例解释:
N = 3, A = [1 2 3]
S = {1, 2, 3}
2^S = {空, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
f(空) = 0
f({1}) = 1
f({2}) = 2
f({3}) = 3
f({1, 2}) = 1 xor 2 = 3
f({1, 3}) = 1 xor 3 = 2
f({2, 3}) = 2 xor 3 = 1
f({1, 2, 3}) = 0
所以
B = [0, 0, 1, 1, 2, 2, 3, 3]

HINT

数据范围:

1 <= N <= 10,0000

其他所有输入均不超过10^9

Source

湖北省队互测

分析:

把题意简化一下就是求不去重异或空间里$x$的排名...首先我们求出线性基,记线性基的秩为$m$,则异或空间里的元素有$2^{m}$种,而总的元素个数有$2^{n}$种,我们考虑除去每一个异或空间里的数字都可以用线性基异或得到,所以线性基可以遍历整个异或空间,然后考虑非线性基的取法和线性基组合起来可以遍历异或空间一次,所以每种遍历都使得每个元素的次数$+1$,那么也就是说每种元素的出现次数是相同的也就是$2^{n-m}$...这样就很好判断了...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;

const int maxn=100000+5,mod=10086;

int n,q,cnt,a[maxn],b[maxn];

int ans;

inline int power(int x,int y){
	int res=1;
	while(y){
		if(y&1)
			(res*=x)%=mod;
		(x*=x)%=mod,y>>=1;
	}
	return res;
}

inline void xor_gauss(void){
	cnt=0;
	for(int i=1;i<=n;i++){
		for(int j=n;j>i;j--)
			if(a[j]>a[i])
				swap(a[i],a[j]);
		if(a[i])
			cnt++;
		else
			break;
		for(int j=31;j>=0;j--)
			if((a[i]>>j)&1){
				b[i]=j;
				for(int k=1;k<=n;k++)
					if(i!=k&&(a[k]>>j)&1)
						a[k]^=a[i];
				break;
			}
	}
}

signed main(void){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	xor_gauss();
	scanf("%d",&q);
	ans=0;
	for(int i=1;i<=cnt;i++)
		if((q>>b[i])&1)
			q^=a[i],(ans+=power(2,cnt-i))%=mod;
	printf("%d\n",(ans*power(2,n-cnt)%mod+1)%mod);
	return 0;
}

  


By NeighThorn

posted @ 2017-02-22 23:38  NeighThorn  阅读(395)  评论(1编辑  收藏  举报