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
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,5∗104] , Ai∈[0,229]
The first line has an integer n
The second line has n integers A1 , A2 .... An
n∈[1,5∗104] , 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: 36Case #2: 40当 A xor B 的答案为 2p 时, A 和 B 表示成二进制数后末 p−1 位肯定相同 于是我们维护一颗字母树,将每个数表示成二进制数后翻转可以下,插入字母树 统计答案时,我们找出 Ai 的二进制数翻转后在字母树上的路径,对于路径上每个点 x ,设他走的边是 v ,且当前为第 k 位,则和他 xor 后lowbit为 2k 的数的个数为trans(x,v^1)的子树大小。 trans(x,v)表示字母树上在结点x,走连出去的字母为v的边到达的结点// // main.cpp // bc44_B // // Created by Fangpin on 15/6/13. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct Trie{ int v; Trie *next[2]; Trie(){next[0]=next[1]=NULL;v=1;} }*rt; const int M=998244353; void build(){ rt=new Trie; } int a[31],f[31]; long long ans=0; void insert(){ Trie *p=rt,*q; for(int i=0;i<31;++i){ if(p->next[a[i]^1]!=NULL) ans=(ans+p->next[a[i]^1]->v*f[i])%M; if(p->next[a[i]]==NULL){ q=new Trie; p->next[a[i]]=q; p=q; } else{ p=p->next[a[i]]; ++p->v; } } } void solve(Trie *root){ for(int i=0;i<2;++i){ if(root->next[i]!=NULL) solve(root->next[i]); } delete root; } void init(){ f[0]=1; for(int i=1;i<31;++i){ f[i]=f[i-1]<<1; } } int main(int argc, const char * argv[]) { // insert code here... int t; init(); cin>>t; for(int ca=1;ca<=t;++ca){ int n; scanf("%d",&n); build(); ans=0; for(int i=0;i<n;++i){ int tem; scanf("%d",&tem); for(int j=0;j<31;++j){ a[j]=(tem>>j)&1; } insert(); } solve(rt); cout<<"Case #"<<ca<<": "<<(ans*2)%M<<endl; } return 0; }