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.
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.
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.
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[i]=(b[i]+c[i]+Σ(b[i]+c[i])/(2n))/n;
然后根据异或合取性质检验答案;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") #define freopen freopen("in.txt","r",stdin) const int maxn=2e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,t; ll a[maxn],b[maxn],c[maxn],d[maxn],k[maxn],num[30][maxn],sum; bool flag; int main() { int i,j; flag=true; scanf("%d",&n); rep(i,1,n)b[i]=read(); rep(i,1,n)c[i]=read(); rep(i,1,n)d[i]=b[i]+c[i],sum+=d[i]; sum/=(2*n); rep(i,1,n)a[i]=(d[i]-sum)/n; rep(i,1,n) { rep(j,0,29)if((a[i]>>j)&1)num[j][i]=1,k[j]++; } rep(i,1,n) { ll tmp1=0,tmp2=0; rep(j,0,29) { if(num[j][i])tmp1+=(1<<j)*k[j]; else tmp2+=(1<<j)*k[j]; } if(tmp1!=b[i]&&tmp2!=c[i]) { flag=false; break; } } if(flag) { rep(i,1,n)printf("%lld ",a[i]); } else puts("-1"); //system("Pause"); return 0; }