ZOJ - 3870-Team Formation二进制,位运算
阅读原文时间:2023年07月08日阅读:1

传送门http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870

题意:找出一个数列中的两个数,所有通过异或和使得结果同时大于这两个数;

思路:先找出每个数在二进制下最高位 1 所在的下标;

   对于每个数,

     1/ 判断这个数的最后一位是不是 0,如果是 0,说明可以 和 最高位是这个位子(1)的数进行异或得到更大的数;

       结果加上有多少个这样的数;

      2/   对这个数进行( >>1 ), 右移一位的操作;

 ac代码

#include
#include
#include
using namespace std;
const int maxn = ;
int a[maxn],sum[];
int get(int n)
{
int s=n,re=;
while(s)
{
s>>=;
re++;
}
return re;
}
int main(){
//freopen("in","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++) { scanf("%d",&a[i]); sum[get(a[i])]++; } long long ans=; for(int i=;i<=n;i++) { int wei=; while(a[i]) { if((a[i] & )== ) { ans+=sum[wei]; } a[i]>>=;
wei++;
}
}
printf("%lld\n",ans);
}

return ;  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章