考察:位运算
思路:
遍历每个数字,统计每个位为1的次数。
最后遍历位数,如果该位次数>1,则计算组合数$C_ {2}^ {a}$ (a是该位的次数)
ac code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long,long> pii;
#define int ll
const int dx[9]={-1, 0, 1, 0, 1, -1, 1, -1,0};
const int dy[9]={0, 1, 0, -1, -1, 1, 1, -1,0};
#define INF 0x3f3f3f3f
const int N=2e5+10;
int bit[32];
int arr[N];
void add(int x)
{
for(int i=0;i<32;i++)
{
int f = (x>>i)&1; //判断该位是否为1
if(f==1) bit[i]++;
}
}
void solve()
{
int n,ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
add(arr[i]);
}
for(int i=0;i<32;i++)
{
if(bit[i]>1)
{
int a=bit[i];
ans+=a*(a-1)/2;
}
}
cout<<ans<<endl;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
//cin >> T;
while(T--) solve();
}