题目链接:http://codeforces.com/problemset/problem/734/F
题意:见链接。
题解:得知a + b = a&b + a|b 后,对所有项求和就可以迅速求出数组a[i]。但是求解过程中只利用到了b[i]+c[i],所以有可能求出的a[i]并不符合条件,则需要按位验算。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <functional>
#include <map>
#include <bitset>
#define INF 0x7fffffff
#define REP(i,j,k) for(int i = j;i <= k;i++)
#define squr(x) (x) * (x)
#define lowbit(x) (x&(-x))
#define getint(x) scanf("%d", &(x))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 10;
int n;
int b[MAXN], c[MAXN], a[MAXN];
int num1[MAXN];
LL tot = 0;
int getbit(int x, int pp) {
return (x >> pp) & 1;
}
bool check() {
REP(i, 0, 31) {
REP(j, 1, n) {
num1[i] += getbit(a[j], i);
}
}
REP(i, 1, n) {
LL tb = 0, tc = 0;
REP(j, 0, 31) {
if (getbit(a[i], j) == 0) {
tc += (LL)num1[j] * (1 << j);
} else {
tb += (LL)(num1[j]) * (1 << j);
tc += (LL)n * (1 << j);
}
}
//cout << tb << " " << tc << endl;
if (tb != b[i]) {
return false;
}
if (tc != c[i]) {
return false;
}
}
return true;
}
int main(int argc, const char * argv[]) {
getint(n);
REP(i, 1, n) {
getint(b[i]);
tot += (LL)b[i];
}
REP(i, 1, n) {
getint(c[i]);
tot += (LL)c[i];
}
if (tot % (2 * (LL)n) != 0) {
printf("-1\n");
return 0;
}
tot /= 2 * (LL)n;
REP(i, 1, n) {
LL t = b[i] + c[i] - tot;
if (b[i] > c[i]) {
printf("-1\n");
return 0;
}
if (t < 0) {
printf("-1\n");
return 0;
}
if (t % n != 0) {
printf("-1\n");
return 0;
}
a[i] = (int)t / n;
}
if (!check()) {
printf("-1\n");
return 0;
}
REP(i, 1, n) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}