您的位置:首页 > 其它

ACdream 1023 抑或

2016-05-28 23:38 190 查看

Xor

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
[align=center]Submit Statistic Next Problem[/align]

Problem Description

For given multisets Aand B, find minimum non-negative xx which A⊕x=BA⊕x=B

Note that for A={a1,a2,…,an}A={a1,a2,…,an} , A⊕x={a1⊕x,a2⊕x,…,an⊕x}. ⊕stands for exclusive-or.

Input

The first line contains a integer nn , which denotes the size of set AA (also for BB ).

The second line contains nn integers a1,a2,…,ana1,a2,…,an , which denote the set AA .

The thrid line contains nn integers b1,b2,…,bnb1,b2,…,bn , which denote the set BB .

(1≤n≤1051≤n≤105 , nn is odd, 0≤ai,bi<2300≤ai,bi<230 )

Output

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

Sample Input

3
0 1 3
1 2 3


Sample Output

2


Source

ftiasch

Manager

nanae

题意:集合A 与x抑或 得到集合B 输出最小的x 若不存在输出 -1

题解: 1.求抑或 满足交换律
2.两个相同的数 抑或和为0
3.x^0=x
4.a^x=b 可以推出 a^b=x

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<cmath>
#define ll long long
#define pi acos(-1.0)
#define mod 1000000007
using namespace std;
int ans1,ans2;
int a[100005];
int b[100005];
int exm;
int sum1=0,sum2=0;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
ans1=ans2=0;
sum1=0;
sum2=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&exm);
a[i]=exm;
ans1=ans1^exm;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&exm);
b[i]=exm;
sum2+=exm;
ans2=ans2^exm;
}
ans1=ans1^ans2;
for(int i=1;i<=n;i++)
{
sum1=sum1+(ans1^a[i]);
}
if(sum1==sum2)
cout<<ans1<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: