LS 2 Xor(数论)
Xor
For given multisets
Note that for
Input
The first line contains a integer
The second line contains
The thrid line contains
(
Output
The only integer denotes the minimum
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"; } }
The article write by nealgavin