Xiangtan Invitation Contest 2017 - C Similar Subsequence(高斯消元)

Intersection

Bobo has two sets of integers A={a1,a2,,an} and B={b1,b2,,bn}. He says that xspan(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 xspan(A) and xspan(B) hold simultaneously.

Input

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

  • 1n50
  • 0ai,bi<260
  • The number of test cases does not exceed 5000.

Output

For each case, output an integer which denotes the result.

Sample Input

2
0 0
0 0
2
1 2
1 3

Sample Output

1
4

Note

For 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;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章