IntersectionBobo has two sets of integers A={a1,a2,…,an} and B={b1,b2,…,bn} . He says that x∈span(A) (or span(B) ) if and only if there exists a subset of A (or B ) whose exclusive-or sum equals to x . Bobo would like to know the number of x where x∈span(A) and x∈span(B) hold simultaneously. InputThe input contains zero or more test cases and is terminated by end-of-file. For each test case: The first line contains an integer n . The second line contains n integers a1,a2,…,an . The third line contains n integers b1,b2,…,bn .
OutputFor each case, output an integer which denotes the result. Sample Input2
0 0
0 0
2
1 2
1 3
Sample Output1
4
NoteFor the second sample, span(A)=span(B)={0,1,2,3} . |
分析:我们先求出A的秩a然后再求出B的秩b,然后再把A和B摞在一起求秩c,那么答案就是2^(a+b-c).
#include <bits/stdc++.h>
#define N 55
using namespace std;
typedef long long ll;
ll a[N],b[N],c[2*N];
int n;
int gaosi(ll a[],int n)
{
int sta = 1;
for(int i = 1;i <= 60;i++)
{
int t = sta;
while(t < n && !(a[t] & (1ll<<(i-1)))) t++;
if(t > n || !(a[t] & (1ll<<(i-1)))) continue;
swap(a[t],a[sta]);
for(int j = sta+1;j <= n;j++)
if((a[j] & (1ll<<(i-1)))) a[j] ^= a[sta];
sta++;
}
return sta-1;
}
int main()
{
while(scanf("%d",&n) != EOF)
{
for(int i = 1;i <= n;i++)
{
scanf("%lld",&a[i]);
c[i] = a[i];
}
for(int i = 1;i <= n;i++)
{
scanf("%lld",&b[i]);
c[n+i] = b[i];
}
int A = gaosi(a,n),B = gaosi(b,n),C = gaosi(c,2*n);
cout<<(1ll<<(A+B-C))<<endl;
}
}