3C - Youmu

(ans & arr[j]) == ans 保证高位已有值不失效.

((ans[j] >> (i - 1)) & 1) == 1 当前位为1,cnt++,

cnt >= 2 两把剑在这一位上相与为1

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int arr[1000005];
 5 
 6 int main()
 7 {
 8     ios::sync_with_stdio(false);
 9     cin.tie(0);
10 
11     int n;
12     cin >> n;
13     for (int i = 1; i <= n; i++)
14         cin >> arr[i];
15     int ans = 0;
16     for (int i = 31; i >= 1; i--) {
17         int cnt = 0;
18         for (int j = 1; j <= n; j++)
19             if (((ans & arr[j]) == ans) && (((arr[j] >> (i - 1)) & 1) == 1))
20                 cnt++;
21         if (cnt >= 2)
22             ans += (1 << (i - 1));
23     }
24     cout << ans << endl;
25     return 0;
26 }

猜你喜欢

转载自www.cnblogs.com/AntonLiu/p/10738650.html
今日推荐