题意:求所有lowbit(Ai xor Aj)的和。即Ai^Aj的值第一个1出现的位置k,求k<<1的和。
思路:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define inf 0x3f3f3f3f
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define maxn 1000005
#define mod 998244353
struct node
{
int sum;
int l,r;
}f[1000000];
void init(int x){
f[x].l=f[x].r=f[x].sum=0;
}
int tot;
ll cal(int root,int k){//统计总和。
if(root==0||k>29) return 0;
return (2ll*(f[f[root].l].sum*(1<<k)%mod)*f[f[root].r].sum%mod+cal(f[root].l,k+1)%mod+cal(f[root].r,k+1)%mod)%mod;
}
void Insert(int root,int x,int k){//向二叉树插入值。
if(k>29) {
f[root].sum++;
return ;
}
if(x&1){
if(f[root].l==0) {
f[root].l=++tot;
init(tot);
}
Insert(f[root].l,x>>1,k+1);
}
else{
if(f[root].r==0) {
f[root].r=++tot;
init(tot);
}
Insert(f[root].r,x>>1,k+1);
}
f[root].sum=f[f[root].l].sum+f[f[root].r].sum;
}
int main()
{
int T,t=0;
int n,f[10];
int x;
rd(T);
while(T--){
tot=1;
init(0);
init(1);
rd(n);
for(int i=1;i<=n;i++)
{
rd(x);
Insert(1,x,0);
}
printf("Case #%d: %I64d\n",++t,cal(1,0));
}
return 0;
}