题意:给定b,c两个数组,其中
b[i]=∑nj=1a[i]&a[j],c[i]=∑nj=1a[i]|a[j]
。求是否存在a数组使得b,c数组满足条件,不存在输出-1,存在的话输出a数组。
分析:只要知道
a[i]+a[j]=a[i]|a[j]+a[i]&a[j]
就好做了,我们令
sum=∑ni=1a[i]
,然后我们会得到
c[i]=∑nj=1(a[i]+a[j])−b[i]=n∗a[i]+sum−b[i]
即
b[i]+c[i]=n∗a[i]+sum
然后就可以求出a数组啦,最后我们还要检查一下a数组是否能够使得b,c数组成立。
代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-5;
const int N=2e5+10;
const int M=4e6+10;
const ll MOD=1000000007;
const int mod=1000000007;
const int MAX=2000000010;
const double pi=acos(-1.0);
int w[60];
ll a[N],b[N],c[N];
int main()
{
int i,j,bo=1;
ll n,sum=0,B,C;
scanf("%I64d", &n);
for (i=1;i<=n;i++) scanf("%I64d", &b[i]),sum+=b[i];
for (i=1;i<=n;i++) scanf("%I64d", &c[i]),sum+=c[i];
if (sum%(2*n)!=0) bo=0;
sum/=2*n;
for (i=1;i<=n;i++) {
if ((b[i]+c[i]<sum)||(b[i]+c[i]-sum)%n!=0) bo=0;
a[i]=(b[i]+c[i]-sum)/n;
if (b[i]>n*a[i]||c[i]<n*a[i]) bo=0;
}
memset(w,0,sizeof(w));
for (i=1;i<=n;i++)
for (j=0;j<60;j++) w[j]+=a[i]>>j&1;
for (i=1;i<=n;i++) {
B=C=0;
for (j=0;j<60;j++)
if (a[i]>>j&1) B+=w[j]*(1ll<<j),C+=n*(1ll<<j);
else C+=w[j]*(1ll<<j);
if (B!=b[i]||C!=c[i]) bo=0;
}
if (!bo) printf("-1\n");
else {
for (i=1;i<n;i++) printf("%I64d ", a[i]);
printf("%I64d\n", a[n]);
}
return 0;
}