Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <limits>
- #include <vector>
- #include <algorithm>
- #include <bitset>
- #include <set>
- #include <unordered_set>
- using namespace std;
- using ll = long long;
- using ui = unsigned int;
- #define int ll
- vector<ui> a;
- unordered_set<ui> bits;
- int diff = 0;
- bool check(ui x) {
- ui cnt = __builtin_popcount(a[0] ^ x);
- for (ui y: a) if (__builtin_popcount(y ^ x) != cnt) return false;
- return true;
- }
- void rec(int i = 0, ui mask = 0, ui cnt = 0) {
- if (cnt > diff) {
- return;
- }
- if (i == 32) {
- if (cnt == diff) {
- if (check(mask)) {if (cnt > diff) {
- return;
- }
- cout << mask << endl;
- exit(0);
- }
- }
- return;
- }
- rec(i + 1, mask, cnt);
- if (bits.count(i)) rec(i + 1, mask | (1 << i), cnt + 1);
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- int n;
- cin >> n;
- a.resize(n);
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- bitset<32> A(a[0]);
- bitset<32> B(a[0]);
- for (int x: a) {
- if (__builtin_popcount(x) != A.count()) {
- B = x;
- break;
- }
- }
- if (A.count() == B.count()) {
- cout << 0 << endl;
- return 0;
- }
- if (A.count() < B.count()) swap(A, B);
- diff = A.count() - B.count();
- if (diff % 2) {
- cout << -1 << endl;
- return 0;
- }
- diff /= 2;
- cout << A << " " << B << endl;
- for (int i = 0;i < 32; i++) {
- if (A[i] && !B[i]) {
- bits.insert(i);
- }
- }
- rec();
- cout << -1 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement