ZYB loves Xor I(hud5269)

ZYB loves Xor I

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


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
思路:字典树;
题意:
ZYB喜欢研究Xor,现在他得到了一个长度为nn的数组A。于是他想知道:对于所有数对(i,j)(i \in [1,n],j \in [1,n])(i,j)(i[1,n],j[1,n]),lowbit(A_i xor A_j)lowbit(Ai​​xorAj​​)之和为多少.由于答案可能过大,你需要输出答案对998244353取模后的值
定义lowbit(x)=2^k2k​​,其中k是最小的满足(xx andand 2^k)>02k​​)>0的数
特别地:lowbit(0)=0

将数转化为二进制,我们只需要枚举第k位满足亦或为1,那么分别统计这位上是0 x1,和1 x2,并且保证在x1,x2在[k-1,0]这些位上是相同的,那么我们可以建立字典树来统计字典树保证了当前节点前的所有位都相同。复杂度O(n*30);
  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<string.h>
  5 #include<math.h>
  6 #include<stdlib.h>
  7 using namespace std;
  8 typedef long long LL;
  9 const LL mod = 998244353;
 10 struct node
 11 {
 12     node *p[2];
 13     LL val;
 14     node()
 15     {
 16         memset(p,0,sizeof(p));
 17         val = 0;
 18     }
 19 };
 20 int a[100005];
 21 short int id[100005][30];
 22 int si[100005];
 23 node *head;
 24 void fr(node *h);
 25 void in(node *h,int num);
 26 LL ask(node *h,LL x);
 27 int main(void)
 28 {
 29     int n,m,t,__cn = 0;;
 30     scanf("%d",&t);
 31     while(t--)
 32     {
 33         memset(id,0,sizeof(id));
 34         scanf("%d",&n);
 35         head = new node();
 36         for(int i = 1; i <= n; i++)
 37         {
 38             scanf("%d",&a[i]);
 39             int t = 0;
 40             int tt = 29;
 41             while(tt)
 42             {
 43                 id[i][t++] = a[i]%2;
 44                 a[i]/=2;
 45                 tt--;
 46             }
 47             si[i] = t;
 48             in(head,i);
 49         }
 50         LL sum = ask(head,1);
 51         fr(head);
 52         printf("Case #%d: ",++__cn);
 53         printf("%lld\n",sum);
 54     }
 55     return 0;
 56 }
 57 void in(node *h,int  num)
 58 {
 59     int i,j;
 60     node *ak = h;
 61     for(i = 0; i < si[num]; i++)
 62     {
 63         int c = id[num][i];
 64         if(ak->p[c]==NULL)
 65         {
 66             ak->p[c] = new node();
 67         }
 68         ak->p[c]->val++;
 69         //printf("%d",c);
 70         ak = ak->p[c];
 71     }
 72     //printf("\n");
 73 }
 74 void fr(node *h)
 75 {
 76     int i;
 77     for(i = 0; i < 2; i++)
 78     {
 79         if(h->p[i]!=NULL)
 80         {
 81             fr(h->p[i]);
 82         }
 83     }
 84     free(h);
 85 }
 86 LL ask(node *h,LL x)
 87 {
 88     node *ak = h;
 89     int i,j;
 90     LL sum = 0;
 91     if(h->p[0]!=NULL&&h->p[1]!=NULL)
 92     {
 93         LL ac = (1<<x)%mod;
 94         sum = sum + (ac*(h->p[0]->val*h->p[1]->val)%mod)%mod;
 95         sum%=mod;
 96     }
 97     for(i = 0; i < 2; i++)
 98     {
 99         if(h->p[i]!=NULL)
100         {
101             sum = (sum+ask(h->p[i],x+1))%mod;
102         }
103     }
104     return sum;
105 }

 

 

 

posted @ 2016-12-16 14:54  sCjTyC  阅读(377)  评论(0编辑  收藏  举报