关闭

ACdream-1023-Xor

189人阅读 评论(0) 收藏 举报
分类:

Description

For given multisets A

and B, find minimum non-negative x which Ax=B

.

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

, Ax={a1x,a2x,,anx}.

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

.

(1n105

, n is odd, 0ai,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;
}







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60501次
    • 积分:4124
    • 等级:
    • 排名:第7738名
    • 原创:370篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    最新评论