Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "bits/stdc++.h"
- using namespace std;
- #define all(a) a.begin(), a.end()
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- typedef long double ld;
- #define int long long
- pair<bool, pair<vector<pair<int, int>>, vector<pair<int, int>>>> ok(vector<pair<int, int>>& b, int bit) {
- int cnt[2][2] = {};
- vector<pair<int, int>> bit1, bit0;
- for (int i = 0; i < (int)b.size(); i++) {
- cnt[(b[i].first >> bit) & 1ll][b[i].second]++;
- int bt = (b[i].first >> bit) & 1ll;
- if (bt && b[i].second == 1 || !bt && b[i].second == 0) {
- bit1.push_back(b[i]);
- }
- else {
- bit0.push_back(b[i]);
- }
- }
- if (cnt[0][0] == cnt[1][1] && cnt[1][0] == cnt[0][1]) {
- return { true, {bit1, bit0} };
- }
- else {
- return { false, {{},{}} };
- }
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int t; cin >> t;
- while (t--) {
- int n;
- cin >> n;
- vector<vector<pair<int, int>>> group(1);
- for (int i = 0; i < n; i++) {
- int x; cin >> x;
- group[0].push_back({ x, 0 });
- }
- for (int i = 0; i < n; i++) {
- int x; cin >> x;
- group[0].push_back({x, 1});
- }
- int ans = 0;
- for (int bit = 31; bit >= 0; bit--) {
- bool psh = true;
- vector<vector<pair<int, int>>> new_group;
- for (auto& i : group) {
- auto ret = ok(i, bit);
- if (ret.first == false) {
- psh = false;
- break;
- }
- if(!ret.second.first.empty())new_group.push_back(ret.second.first);
- if (!ret.second.second.empty())new_group.push_back(ret.second.second);
- }
- if (psh) {
- ans |= (1ll << bit);
- group.swap(new_group);
- }
- new_group.clear();
- }
- cout << ans << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement