题解:P10853 【MX-X2-T2】「Cfz Round 4」Xor Counting
Butterfly_qwq · · 题解
简单题。
考虑什么时候会满足条件,设
现在
然后
显然只需统计满足这些条件的数目就行了。枚举
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,ans,a[500005],hb[500005],ct1[500005],ct2[500005];
int highbit(int x)
{
if(!x)return 114514;
for(int j=30;~j;j--)if(x&(1<<j))return j;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
memset(ct1,0,sizeof(ct1));
memset(ct2,0,sizeof(ct2));
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)
{
hb[i]=highbit(a[i]);
ct2[hb[i]]++;
for(int j=30;~j;j--)if(a[i]&(1<<j))ct1[j]++;
}ans=0;
for(int i=1;i<=n;i++)
{
if(a[i])ans+=ct1[hb[i]]-ct2[hb[i]];
else ans+=n;
}
cout<<ans<<'\n';
}
}