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
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]
样例解释:
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
线性基+高斯消元~
假设线性基有k个,那么它们异或出来的值就有2^k种,而总共n个数子集个数为2^n个,因为所有种类数都相等(为什么?),所以每种的重复次数就是2^(n-k)种,然后用高斯消元求一下线性基,从小到大求和就可以了。
注意因为要求的是最小出现位置,所以ans要初始化为1~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define modd 10086
int n,m,a[100001],q[64],k,ans;
void guass(int &k)
{
k=n;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++) if(a[i]<a[j]) swap(a[j],a[i]);
if(!a[i])
{
k=i-1;break;
}
for(int j=31;~j;j--)
if((a[i]>>j)&1)
{
q[i]=j;
for(int z=1;z<=n;z++)
if(i!=z && (a[z]>>j)&1) a[z]^=a[i];break;
}
}
}
int mi(int u,int v)
{
int tot=1;
while(v)
{
if(v&1) tot=tot*u%modd;
u=u*u%modd;v>>=1;
}
return tot;
}
int main()
{
scanf("%d",&n);ans=1;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);scanf("%d",&m);
guass(k);
for(int i=1;i<=k;i++)
if((m>>q[i])&1)
{
m^=a[i];
ans=(ans+mi(2,n-i))%modd;
}
printf("%d\n",ans);
return 0;
}