Codeforces 734 F Anton and School
Discription
Anton goes to school, his favorite lessons are arraystudying. He usually solves all the tasks pretty fast, but this time the teacher gave him a complicated one: given two arrays b and c of length n, find array a, such that:
where a and b means bitwise AND, while a or b means bitwise OR.
Usually Anton is good in arraystudying, but this problem is too hard, so Anton asks you to help.
Input
The first line of the input contains a single integers n (1 ≤ n ≤ 200 000) — the size of arrays b and c.
The second line contains n integers bi (0 ≤ bi ≤ 109) — elements of the array b.
Third line contains n integers ci (0 ≤ ci ≤ 109) — elements of the array c.
Output
If there is no solution, print - 1.
Otherwise, the only line of the output should contain n non-negative integers ai — elements of the array a. If there are multiple possible solutions, you may print any of them.
Example
4
6 8 4 4
16 22 10 10
3 5 1 1
5
8 25 14 7 16
19 6 9 4 25
-1
有一个显然的性质就是 (a&b)+(a|b)=a+b。
这个考虑每一位的贡献就行了。
于是我们可以得到c[i]+b[i]=a[i]*n+∑a[],从而可以开开心心的算出每一个a,然后带回去验证一下就可以了
/* We know that b[i]+c[i]=n*a[i]+∑a[] */ #include<bits/stdc++.h> #define ll long long #define maxn 200005 using namespace std; int b[maxn],c[maxn]; int a[maxn],n,m,ci[60]; ll tot=0,now; int main(){ ci[0]=1; for(int i=1;i<=30;i++) ci[i]=ci[i-1]<<1; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",b+i),tot+=(ll)b[i]; for(int i=1;i<=n;i++) scanf("%d",c+i),tot+=(ll)c[i]; //tot is used to calculate the sum of array a[] if(tot%(n<<1)){ puts("-1"); return 0; } tot/=(n<<1); for(int i=1;i<=n;i++){ now=c[i]+b[i]-tot; if(now%n){ puts("-1"); return 0; } a[i]=now/n; } for(int i=0;i<=30;i++){ int cnt=0; for(int j=1;j<=n;j++) if(a[j]&ci[i]) cnt++; for(int j=1;j<=n;j++){ if(a[j]&ci[i]) b[j]-=cnt*ci[i],c[j]-=n*ci[i]; else c[j]-=cnt*ci[i]; } } for(int i=1;i<=n;i++) if(c[i]||b[i]){ puts("-1"); return 0; } for(int i=1;i<=n;i++) printf("%d ",a[i]); return 0; }