CF1368D AND, OR and square sum

思路:

只要还能操作,就有收益。所以数组最终的状态会呈现任意两个相邻的数字前一个是后一个的子集(如果将每个比特位上的1当作一个元素的话)。

实现:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n;
 6     while (cin >> n)
 7     {
 8         int x;
 9         vector<int> v(20, 0);
10         for (int i = 0; i < n; i++) 
11         {
12             cin >> x;
13             int j = 0;
14             while (x)
15             {
16                 if (x & 1) v[j]++;     
17                 j++;
18                 x >>= 1;
19             }
20         }
21         vector<int> res(n, 0);
22         for (int i = 0; i < 20; i++)
23         {
24             for (int j = 0; j < v[i]; j++)
25             {
26                 res[n - 1 - j] += 1 << i;
27             }
28         }
29         long long ans = 0;
30         for (int i = 0; i < n; i++)
31         {
32             ans += (long long)res[i] * res[i];
33         }
34         cout << ans << endl;
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/wangyiming/p/14764465.html