Files
python/GESP/Level5/GESP202312/20231202.cpp

45 lines
981 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int a[N];
int ans;
/*
3
1 2 3
答案:2
5
5 6 2 10 13
答案:
8
*/
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
// 很明显,1e6的平方级算法会TLE,但40%的测试点是可以过的
set<int> s; // 哪个数字已经被淘汰掉了
for (int i = 31; i >= 0; i--) {
int cnt = 0;
for (int j = 1; j <= n; j++) { // 遍历所有数字
if (s.count(j) == 0 &&
a[j] >> i & 1) // 如果a[j]这个数字没有被淘汰掉,并且这个数字的第i位是1
cnt++;
}
if (cnt >= 2) {
// 如果此位存在1两个以上,那么此位是有效数位,所以此位是0的会被淘汰
for (int j = 1; j <= n; j++)
if (!(a[j] >> i & 1))
s.insert(j);
// 记录&的累加值
ans = ans + (1 << i);
}
}
cout << ans << endl;
return 0;
}