首页 > 代码库 > Team Formation

Team Formation

技术分享
 1 #include<bits/stdc++.h>
 2 
 3 const int Maxn = 1000005;
 4 
 5 #define ll long long
 6 using namespace std;
 7 int x[Maxn], pos[33];
 8 int main() {
 9     int T, N;
10     cin >> T;
11     while (T--) {
12         scanf("%d", &N);
13         for (int i = 0; i < N; i++) scanf("%d", &x[i]);
14         sort(x, x + N);
15 
16         long long ans = 0;
17         for (int i = 1; i < N; i++) {
18             memset(pos, 0, sizeof(pos));
19 
20             int k = x[i], cnt = 0, p = 0; //  cnt -> x[i]二进制中0的个数,p表示0的位置
21             while (k) {
22                 if ((k & 1) == 0) pos[cnt++] = p;
23                 k >>= 1;
24                 p++;
25             }
26 
27             for (int j = 0; j < cnt; j++) {
28                 int a = 1 << pos[j], b = (1 << (pos[j] + 1)) - 1;
29                 ans += upper_bound(x, x + i, b) - lower_bound(x, x + i, a);
30             }
31         }
32         printf("%lld\n", ans);
33     }
34 }
View Code

 

Team Formation