这道题考察对位运算的理解。
为表示简便,下文中用 代表 。
性质1: 若 是符合题意的三元组,则 。
证明不难:因为 , 所以 。又因为 , 所以 。其他同理。那么知道这个性质之后就可以将 的元素扔掉了。
性质2:若 是符合题意的三元组,则 的 只有两种情况: 个为 或 个为 。
这个也不难证,因为 ,所以这三个数的 之和一定为偶数,所以就这两种情况。
这样就可以分开算贡献了。对于第一种情况,双层循环枚举哪一位是 ,看有没有对应的有两位的数。对于第二种情况,可以发现 在二进制下一共只会有三位是 ,否则两两异或会导致 超过 。因此可以枚举三位看这三位上是 的情况,然后计算贡献。
总复杂度 。但这道题的时间卡的有点严,所以计算 的时候注意优化,只要超过 直接返回即可,反正也没有贡献。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5,P=1e9+7;
int T,n,ans1,ans2;
__int128 b,q[121];
void clear(){printf("%d\n",(ans1+ans2)%P);ans1=ans2=0;}
void read(__int128& x){
char a=getchar_unlocked();x=0;
while(a<'0'||a>'9') a=getchar_unlocked();
while(a<='9'&&a>='0') x=x*10+a-'0',a=getchar_unlocked();
}
int popcountt(__int128 x){
int cnt=0;
while(x&&cnt<=2) cnt++,x&=(x-1);
return cnt;
}
int main(){
for(int i=0;i<=120;i++) q[i]=!i?1:q[i-1]*2;
scanf("%d",&T);
while(T--){
map<__int128,int> mp1,mp2;
scanf("%d",&n);
for(int i=1,o;i<=n;i++){
read(b),o=popcountt(b);
if(o==2) mp2[b]++;
if(o==1) mp1[b]++;
}
for(int i=0;i<=120;i++)
for(int j=i+1;j<=120;j++)
ans1=(ans1+mp1[q[i]]*mp1[q[j]]%P*mp2[q[i]^q[j]]%P)%P;
for(int i=0;i<=120;i++)
for(int j=i+1;j<=120;j++)
for(int k=j+1;k<=120;k++)
ans2=(ans2+mp2[q[i]^q[j]]*mp2[q[i]^q[k]]%P*mp2[q[j]^q[k]]%P)%P;
clear();
}
return 0;
}由于我用了 map,所以时间很紧张,这里应该用数组离散化一下。