ZYB loves Xor I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 306 Accepted Submission(s): 151
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
Recommend
hujie | We have carefully selected several similar problems for you:
5271
5270
5268
5267
5266
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
using namespace std;
const int mod=998244353;
const int maxm=30*50010;
int a[100010];
ll ans;
int bit[30];
int sz;
struct trie
{
int next[2];
void init()
{
memset(next,0,sizeof(next));
}
}trie[maxm];
int num[maxm];
void insert(int x)
{
int u=0;
for(int i=0;i<30;i++)
{
int id;
if(x&bit[i]) id=1;
else id=0;
if(trie[u].next[id^1]!=0)
{
ans+=(ll)bit[i]*num[trie[u].next[id^1]]%mod;
}
if(!trie[u].next[id])
{
trie[u].next[id]=++sz;
trie[sz].init();
}
u=trie[u].next[id];
num[u]++;
}
}
int main()
{
bit[0]=1;
for(int i=1;i<30;i++)
bit[i]=2*bit[i-1];
int t;
int cas=1;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
sz=0;
memset(num,0,sizeof(num));
ans=0;
trie[0].init();
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
insert(a[i]);
}
ans=ans*2%mod;
printf("Case #%d: %I64d\n",cas++,ans);
}
}