Codeforces Round #647 (Div. 2) B. Johnny and His Hobbies(枚举)

题目链接:https://codeforces.com/contest/1362/problem/B

题意

有一个大小及元素值均不超过 $1024$ 的正整数集合,求最小正整数 $k$,使得集合中的每个元素异或 $k$ 后得到的新集合与原集合相等,若这样的 $k$ 不存在输出 $-1$ 。

题解

数据范围较小,枚举即可。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n; cin >> n;
    set<int> st1;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        st1.insert(x);
    }
    for (int k = 1; k <= 1024; k++) {
        set<int> st2;
        for (auto s : st1)
            st2.insert(s ^ k);
        if (st1 == st2) {
            cout << k << "
";
            return;
        }
    }
    cout << -1 << "
";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}
原文地址:https://www.cnblogs.com/Kanoon/p/13052817.html