給定序列 ,, 可以任意排列。令 ,則價值則為 ,求價值最大值。
如果答案的某一位是 ,則 該位的 和 數量必然和 該位的 和 數量分別相同。同樣的,如果答案 可行,則 得到的所有值的數量與 得到的所有值的數量相同( 當作取反符號)。從高位到低位,判斷 當前位為 是否可行,可行則或上。
void solve()
{
int n;
std::cin >> n;
std::vector<unsigned int> a(n), b(n);
for (auto &i : a) std::cin >> i;
for (auto &i : b) std::cin >> i;
auto check = [&a, &b](unsigned int ans) {
std::map<unsigned int, int> cnt;
for (auto i : a) cnt[i & ans]++;
for (auto i : b) cnt[~i & ans]--;
for (auto i : cnt) {
if (i.second != 0) return false;
}
return true;
};
unsigned int ans = 0;
for (int i = 31; i >= 0; i--) {
if (check(ans | (1 << i))) {
ans |= (1 << i);
}
}
std::cout << ans << std::endl;
}