您的位置:首页 > 其它

ACdream-1023-Xor

2016-06-01 22:26 232 查看
Description

For given multisets A

and B,
find minimum non-negative x
which A⊕x=B
.

Note that for A={a1,a2,…,an}

, A⊕x={a1⊕x,a2⊕x,…,an⊕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 a1,a2,…,an,
which denote the set A
.

The thrid line contains n

integers b1,b2,…,bn,
which denote the set B
.

(1≤n≤105

, n
is odd, 0≤ai,bi<230

)

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


烦人的异或问题啊

因为异或满足交换律,x^x为0

全部异或起来就是(a1^b1)……就是奇数个x异或必为x,
0^x=x,如果有解,那么一定存在两两异或等于x

然后比对找无解的情况

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
int a[100005],b[100005];
while(scanf("%d",&n)!=EOF)
{
bool flag=true;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int j=0;j<n;j++) scanf("%d",&b[j]);
int ans=0;
for(int i=0;i<n;i++)
ans^=a[i];
for(int j=0;j<n;j++)
ans^=b[j];
for(int i=0;i<n;i++)
a[i]^=ans;
sort(a,a+n);
sort(b,b+n);
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
flag=false;
}
if(flag) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: