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,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
思路:字典树;
题意:

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(A​i​​xorA​j​​)之和为多少.由于答案可能过大,你需要输出答案对998244353取模后的值
定义lowbit(x)=2^k2​k​​,其中k是最小的满足(xx andand 2^k)>02​k​​)>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 }
 

ZYB loves Xor I(hud5269)的更多相关文章

  1. HDU--5269 ZYB loves Xor I (字典树)

    题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制  我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...

  2. hdu 5269 ZYB loves Xor I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  3. ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

    Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...

  4. hdu 5269 ZYB loves Xor I &amp;&amp; BestCoder Round #44

    题意: ZYB喜欢研究Xor,如今他得到了一个长度为n的数组A. 于是他想知道:对于全部数对(i,j)(i∈[1,n],j∈[1,n]).lowbit(AixorAj)之和为多少.因为答案可能过大,你 ...

  5. bestcoder r44 p3 hdu 5270 ZYB loves Xor II

    这是昨晚队友跟我说的题,不知道当时是什么玄幻的事件发生了,,我看成了两两相乘的XOR 纠结了好长时间间 不知道该怎么办 今天早上看了下这道题,发现是两两相加的XOR  然后就想了想昨晚的思路 发现可做 ...

  6. hdu5269 ZYB loves Xor I

    分治法和字典树都可以,都是递归,但字典树耗内存 从第一bit开始,若相同则xor为0,分到同一部分,不相同则统计,且此时lowbit为这一bit,最后结果要乘以2 /*分治法*/ #include&l ...

  7. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  8. hdu 5269 ZYB loves Xor I 分治 || Trie

    题目大意: 长度为\(n\)的数组A.求对于所有数对\((i,j)(i \in [1,n],j \in [1,n])\),\(lowbit(A_i xor A_j)\)之和.答案对998244353取 ...

  9. HDU 5269 ZYB loves Xor I (二分法)

    题意: 给出一个序列,对每两个数求异或结果后取最低位的1出来作为一个数,然后求这些数字的和.比如:{a,b,c},结果是lowbit(a^b)+lowbit(a^c)+lowbit(b^a)+lowb ...

随机推荐

  1. C++ STL算法之:copy

    C++ STL算法:copy 目录(?)[+] 前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法.所谓变易算法(Mutating a ...

  2. Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦

    Machine Learning读书会,面试&算法讲座,算法公开课,创业活动,算法班集锦 近期活动: 2014年9月3日,第8次西安面试&算法讲座视频 + PPT 的下载地址:http ...

  3. java中的Arrays类

    今天刚接触了数组,学到了几个比较常用的方法 Fill方法:给数组赋值 sort方法:给数组升序 equals方法:比较数组中元素 值是否相等 binarySearch方法:对排序好的数组进行二分查找法 ...

  4. A Child's History of England.22

    CHAPTER 8 ENGLAND UNDER WILLIAM THE FIRST, THE NORMAN CONQUEROR Upon the ground where the brave Haro ...

  5. abuse

    abuse 近/反义词: ill-treat, maltreat, mistreat, misuse, prostitute, spoil; defame, disparage, malign, re ...

  6. ehcache详解

    Ehcache是现在最流行的纯Java开 源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多, 如果你有这方 ...

  7. jenkins之授权和权限管理

    #:创建角色,给角色授权,然后创建用户,将用户加入到角色(前提先安装插件) #:先将之前的卸载掉 #:然后重启服务,在可选插件搜索Role #:装完重启服务 root@ubuntu:~# system ...

  8. Linux:while read line与for循环的区别

    while read line:是一次性将文件信息读入并赋值给变量line , while中使用重定向机制,文件中的所有信息都被读入并重定向给了整个while 语句中的line 变量. for:是每次 ...

  9. 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式

    bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...

  10. 【Java基础】Java中如何获取一个类中泛型的实际类型

    泛型的术语 <>: 念做typeof List<E>: E称为类型参数变量 ArrayList<Integer>: Integer称为实际类型参数 ArrayLis ...