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

185 篇文章 0 订阅
116 篇文章 0 订阅

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值