您的位置:首页 > 其它

ZOJ 3870 Team Formation

2016-05-08 23:17 363 查看

题目链接:

http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3870

题解:

如果x xor y>max(x,y),那么就会有x和y的最高位不同(二进制表示),并且对于最高位小的那个数的最高位的位置,最高位大的那个数在相同的位置应为0.

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

typedef long long LL;
const int maxn = 1e5 + 10;

int n;

int arr[maxn];
//cnt[i]表示最高位为i的数有多少个
int cnt[32];

void init() {
memset(cnt, 0, sizeof(cnt));
}

int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
for (int i = 0; i<n; i++) {
scanf("%d", &arr[i]);
for (int j = 30; j >= 0; j--) {
if ((1 << j)&arr[i]) {
cnt[j]++; break;
}
}
}
LL ans = 0;
for (int i = 0; i<n; i++) {
int j;
for (j = 30; j >= 0; j--) {
if ((1 << j)&arr[i]) {
break;
}
}
for (j -= 1; j >= 0; j--) {
if (((1 << j)&arr[i]) == 0) {
ans += cnt[j];
}
}
}
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: