hdu 5269ZYB loves Xor I


ZYB loves Xor I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1105    Accepted Submission(s): 486


Problem Description
Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he wants to know the sum of all (lowbit(Ai xor Aj)) (i,j[1,n])
We define that lowbit(x)=2k,k is the smallest integer satisfied ((x and 2k)>0)
Specially,lowbit(0)=0
Because the ans may be too big.You just need to output ans mod 998244353
 

Input
Multiple test cases, the first line contains an integer T(no more than 10), indicating the number of cases. Each test case contains two lines
The first line has an integer n
The second line has n integers A1,A2....An
n[1,5104]Ai[0,229]
 

Output
For each case, the output should occupies exactly one line. The output format is Case #x: ans, here x is the data number begins at 1.
 

Sample Input
2 5 4 0 2 7 0 5 2 6 5 4 0
 

Sample Output
Case #1: 36 Case #2: 40
 

Source


一道trie

题意就是两个数二进制表示从后往前第一个不相同的位置

由此想到trie(虽然我肯定是想不到的QAQ)

然后就随便搞搞

我这方法好像有点慢

因为我每次都把树遍历一遍

然后初始化也是直接for

贼不靠谱

不过随便啦

过了就行

也就慢了个几倍

大家不要学我

其实完全可以边建边求

每次就算一下隔壁(二叉)的那些就好啦

#include<cstdio>
#include<cstring>
struct node
{
	int c[2];long long size;int dep;
}e[1500001];
const int mod=998244353;
int cnt=1	;long long ans=0;
void work(int now,int dep,int x)
{
	e[now].size++;e[now].dep=dep;
	if(dep==30)		return ;
	if(!e[now].c[x&1])	e[now].c[x&1]=++cnt;
	work(e[now].c[x&1],dep+1,x>>1);
}
void dfs(int ro)
{
	ans+=(1<<e[ro].dep)*e[e[ro].c[0]].size%mod*e[e[ro].c[1]].size%mod*2;
	ans%=mod;
	if(e[ro].c[0])dfs(e[ro].c[0]);
	if(e[ro].c[1])dfs(e[ro].c[1]);
}
int main()
{
	int t;scanf("%d",&t);
	for(int j=1;j<=t;j++)
	{
		int n,k;ans=0;
		scanf("%d",&n);
		for(int i=0;i<=1500000;i++)e[i].size=0;
		for(int i=1;i<=n;i++)		scanf("%d",&k),work(1,0,k);
		dfs(1);
		printf("Case #%d: %lld
",j,ans);
	}
	return 0;
} 




原文地址:https://www.cnblogs.com/Brian551/p/7353023.html