最后一场了,还是写写吧;
线性只因 40pts
赛时把与看成或了,最后才发现,结果我的神奇代码交上去得了40pts。。。
从高位到低位依次考虑,若这一位是1的数大于m则统计并删除其它的数;
否则直接跳过;
点击查看代码
#include <iostream>
#include <cstdio>
using namespace std;
int n, m;
int a[5000005];
int ans;
bool vis[5000005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int c = 0, o = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 30; i >= 0; i--) {
int sum = 0;
for (int j = 1; j <= n; j++) {
if (a[j] & (1 << i) && !vis[j]) sum++;
}
if (sum >= m) {
ans += (1 << i);
for (int j = 1; j <= n; j++) {
if (!(a[j] & (1 << i))) vis[j] = true;
}
}
}
cout << ans;
return 0;
}