ZYB loves Xor I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 445 Accepted Submission(s): 233
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
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: 36 Case #2: 40
Source
#include <cstdio>
typedef long long LL;
const LL MOD = 998244353;
const int N = 50010;
LL bit[30];
LL ans;
struct Trie{
int next[N * 30][2];
int cnt[N * 30];
int total, root;
int new_node() {
cnt[total] = 0;
next[total][0] = next[total][1] = -1;
return total++;
}
void init() {
total = 0;
root = new_node();
}
void insert(int val) {
int cur = root;
for (int i = 0; i < 30; ++i) {
int id = val & 1;
val >>= 1;
if (next[cur][id ^ 1] != -1) {
LL tmp = (bit[i] * cnt[next[cur][id ^ 1]]) % MOD;
ans = (ans + tmp) % MOD;
}
if (next[cur][id] == -1)
next[cur][id] = new_node();
++cnt[cur];
cur = next[cur][id];
}
}
}tree;
void Init() {
bit[0] = 1;
for (int i = 1; i < 30; ++i)
bit[i] = (bit[i - 1] * 2) % MOD;
}
int main() {
int t, cnt = 0;
scanf("%d", &t);
Init();
while (t--) {
int n, tmp;
ans = 0;
scanf("%d", &n);
tree.init();
for (int i = 0; i < n; ++i) {
scanf("%d", &tmp);
tree.insert(tmp);
}
ans = (ans * 2) % MOD;
printf("Case #%d: %lld\n", ++cnt, ans);
}
return 0;
}