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. 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享三:问题2

    框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 搜索框是该项目重要的一环,由于涉及 ...

  2. 对Javascript中的对象Object改变内存及其变量改变的图解

    Object 存储变量时,变量属性的内存改变图解 左边: 对象的内存   中间:变量属性的内存   右边:属性值的内存 [图一]创建一个对象,存obj1 变量--里面存age 属性和属性值--12. ...

  3. java运行报错 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    解决方法: 解决办法: 在项目的属性里设置jdk版本,方法是右击项目-->properties-->java compiler --> Enable project specific ...

  4. Flink(六)【ParameterTool类】

    ParameterTool 工具类 object ParameterToolTest { def main(args: Array[String]): Unit = { val params: Par ...

  5. 关于浏览器,从输入URL到呈现页面过程!(主讲TCP/IP协议)

    一.文本对话--从请求到响应 我们在浏览器中输入一个 URL,回车之后便会在浏览器中观察到页面内容.实际上这个过程是: (1)浏览器向网站所在的服务器发送了一个 Request(请求) (2)网站服务 ...

  6. 实现new Date(), 获取当前时间戳

    JS 获取时间戳: 我相信大家找了很久了吧! 希望我写的这个对您有些帮助哦~ 大家是不是以为时间戳是关于时间的,都去 new Date() 里面找方法了啊,我来告诉你们正确的吧 其实大家用 JS 里的 ...

  7. 索引以及Mysql中的索引

    一.什么是索引 索引是表的目录,会保存在额外的文件中,针对表中的指定列建立,专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取 ...

  8. redis实例cpu占用率过高问题优化

    目录 一.简介 一.简介 前情提要: 最近接了大数据项目的postgresql运维,刚接过来他们的报表系统就出现高峰期访问不了的问题,报表涉及实时数据和离线数据,离线读pg,实时读redis.然后自然 ...

  9. inode节点

    目录 一.简介 二.信息 inode的内容 inode的大小 3.inode号码 三.目录文件 四.硬连接 五.软链接 六.inode的特殊作用 一.简介 理解inode,要从文件储存说起. 文件储存 ...

  10. 【web】docker复现环境踩坑

    在先知看到有师傅发了个学习 P 牛的代码审计的文章,在 github 上下下来复现环境,结果 docker 各种问题,气死 安装 docker-compose:pip install -i https ...