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
Sample Output
烦人的异或问题啊
因为异或满足交换律,x^x为0
全部异或起来就是(a1^b1)……就是奇数个x异或必为x,
0^x=x,如果有解,那么一定存在两两异或等于x
然后比对找无解的情况
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; }
相关文章推荐
- Wpf基础之数据绑定
- jsp html中文乱码
- C++ 利用函数, 制作一个三角函数表
- Runtime的简单使用
- adapter(适配器)
- 程序员面试金典(一)
- [原创]java WEB学习笔记34:Session 案例 之 解决表单重复提交
- 第二次冲刺第六天
- Node和express中利用Jquery的$.ajax和HTML5的FormData完成文件上传示例
- nexus私服update repair index索引失败解决方案
- Linux命令行使用总结
- Linked List
- Android模拟ListView点击下载和局部刷新
- 理解面向对象
- Delete Node in a Linked List
- 银行储蓄程序(C++,simple)
- C++ 指针与引用的偶遇
- cocos2dx跨平台
- oracle sql*plus常用命令
- 第13周项目4:立体类族共有的抽象类