LS 2 Xor(数论)

Xor

For given multisets A and B , find minimum non-negative x which Ax=B .

Note that for A={a 1 ,a 2 ,,a n } , Ax={a 1 x,a 2 x,,a n x} . stands for exclusive-or.

Input

The first line contains a integer n , which denotes the size of set A (also for B ).

The second line contains n integers a 1 ,a 2 ,,a n , which denote the set A .

The thrid line contains n integers b 1 ,b 2 ,,b n , which denote the set B .

( 1n10 5 , n is odd, 0a i ,b i <2 30 )

Output

The only integer denotes the minimum x . Print 1 if no such x exists.

Sample input

3
0 1 3
1 2 3

Sample output

2

Source

Contest #3 on acmdream.net by ftiasch


思路:如果A^x=B 那么统计A,B集合中所有数各个位的1的个数和作比较,如果不等那么x的该位必须为1,因为0异或操作不改变任何结果


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=100210;
const int bit=31;
int n;
bool p[mm];
int a[mm],b[mm],ca[bit],cb[bit];
void calculate(int *c,int *cc)
{
  for(int i=0;i<n;i++)
    for(int j=0;j<bit;j++)
    cc[j]+=(c[i]>>j)&1;
}
int main()
{
  while(cin>>n)
  { memset(ca,0,sizeof(ca));
    memset(cb,0,sizeof(cb));
    for(int i=0;i<n;i++)
      cin>>a[i];
    for(int i=0;i<n;i++)
      cin>>b[i];
      calculate(a,ca);calculate(b,cb);
    int x=0;
    for(int i=0;i<bit;i++)
      if(ca[i]^cb[i])x|=1<<i;
    bool yes=1;
    for(int i=0;i<n;i++)
      a[i]^=x;
    sort(a,a+n);sort(b,b+n);
    for(int i=0;i<n;i++)
      if(a[i]^b[i])yes=0;
    cout<<(yes?x:-1)<<"\n";
  }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值