1101 G. (Zero XOR Subset)-less
如果一个数组[
a
1
,
a
2
,
a
3
.
.
.
,
a
n
a_1,a_2,a_3...,a_n
a1,a2,a3...,an]的所有子集的异或和都
≠
0
\neq0
=0的话,则basis vectors的size=n。
这其实很好理解:如果basis vectors的size
≠
0
\neq0
=0的话,那么有一些元素必然没有被加入也就是那些元素可以被已加入的元素"替代",那么这些替代它的元素 xor 那个没被加入的元素一定= 0。
好了,那么假设分段的右边界是[
r
1
,
r
2
,
r
3
.
.
.
,
r
d
−
1
,
n
r_1,r_2,r_3...,r_{d-1},n
r1,r2,r3...,rd−1,n],异或前缀和为[
p
r
e
1
,
p
r
e
2
,
.
.
.
,
p
r
e
n
pre_1,pre_2,...,pre_n
pre1,pre2,...,pren]
则basis vectors{
p
r
e
r
1
,
p
r
e
r
1
⊕
p
r
e
r
2
,
p
r
e
r
2
⊕
p
r
e
r
3
.
.
.
p
r
e
r
d
−
1
⊕
p
r
e
n
pre_{r_1},pre_{r_1} ⊕pre_{r_2},pre_{r_2} ⊕pre_{r_3}...pre_{r_{d-1}} ⊕pre_{n}
prer1,prer1⊕prer2,prer2⊕prer3...prerd−1⊕pren}.size=d
那么basis vectors{
p
r
e
r
1
,
p
r
e
r
2
p
r
e
r
3
.
.
.
p
r
e
n
pre_{r_1},pre_{r_2}pre_{r_3}...pre_{n}
prer1,prer2prer3...pren}.size=d
可以这么理解:
p
r
e
r
x
⊕
p
r
e
r
y
=
(
(
p
r
e
r
x
⊕
p
r
e
r
x
+
1
)
⊕
(
p
r
e
r
x
+
1
⊕
p
r
e
r
x
+
2
)
.
.
.
(
p
r
e
r
y
−
1
⊕
p
r
e
r
y
)
{\color{Blue} pre_{r_x} ⊕pre_{r_y}=((pre_{r_x}⊕pre_{r_{x+1}})⊕(pre_{r_{x+1}}⊕pre_{r_{x+2}})...(pre_{r_{y-1}}⊕pre_{r_{y}})}
prerx⊕prery=((prerx⊕prerx+1)⊕(prerx+1⊕prerx+2)...(prery−1⊕prery)
如果
所以就把所有
p
r
e
i
pre_i
prei加到basis vectors最终答案就是basis vectors.size.
其中:
p
r
e
n
pre_n
pren必须要加到basis vectors 中!否则答案为-1.
Code:
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr<<A.FIR<<" "<<A.SEC<<endl;
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
const int d=30;
int store[d];
int main(){
fastio;
int n;
cin>>n;
int pref=0;
rb(i,1,n){
int ai;
cin>>ai;
pref^=ai;
ai=pref;
rep(j,d){
if(store[j]&&(1&(ai>>j))){
ai^=store[j];
}
}
if(ai){
rep(j,d){
if(1&(ai>>j)){
store[j]=ai;
break;
}
}
}
}
if(!pref){
cout<<"-1"<<endl;
}
else{
int ans=0;
rep(i,d)
ans+=bool(store[i]);
cout<<ans<<endl;
}
return 0;
}
959 F. Mahmoud and Ehab and yet another xor task
总思路对于所有的位置i建立basis vectors 。
问题就转化为:
有一个数组[ a 1 , a 2 , a 3 . . . a n a_1,a_2,a_3...a_n a1,a2,a3...an],取出一些数其中他们xor起来=k,有多少种取法?
basis vectors 有一个特性如果一个数可以用basis vectors 里的数xor来表示的话,最多只有一种方法,因为
f
(
x
)
f(x)
f(x)各不相同。
其中没有被加入到basis vectors 里的元素都可已被basis vectors 里的元素替代,也就只有一种替代方法。所以如果可以被表示,那么答案就是
2
没
被
加
入
到
b
a
s
i
s
v
e
c
t
o
r
s
里
的
数
的
个
数
2^{没被加入到basis vectors里的数的个数}
2没被加入到basisvectors里的数的个数,否则是0。
code:
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr<<A.FIR<<" "<<A.SEC<<endl;
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
const int d = 20;
int store[100000+1][d];
int n,q,a[100000+2];
LL pre[100000+2];
int main(){
fastio;
pre[0]=1;
rb(i,1,100000)
pre[i]=pre[i-1]<<1,pre[i]%=(LL)(1e9+7);
cin>>n>>q;rb(i,1,n) cin>>a[i];
rb(i,1,n){
rep(j,d)
store[i][j]=store[i-1][j];
rep(j,d){
if(store[i][j]&&(1&(a[i]>>(j)))){
a[i]^=store[i][j];
}
}
rep(j,d){
if(1&(a[i]>>j)){
store[i][j]=a[i];
break;
}
}
}
while(q--){
int l,x;
cin>>l>>x;
rep(j,d){
if(store[l][j]&&(1&(x>>j))){
x^=store[l][j];
}
}
if(x){
cout<<0<<endl;
}
else{
int sz=0;
rep(j,d)
sz+=bool(store[l][j]);
sz=l-sz;
cout<<pre[sz]<<endl;
}
}
return 0;
}