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. goto 的用法

    #include <stdio.h> int main() { printf("go to cpy \n"); goto FLASH_CPY; printf(" ...

  2. 在Idea上用JDBC连接mysql数据库

    一.前言 本次操作建立在idea中java环境已配置的基础上 二.操作步骤 1.建立Web项目后,添加驱动包 mysql-connector-java-5.0.8-bin.jar (1)下载mysql ...

  3. Cx_Oracle 安装

    1. 下载安装 2.把oci.ddl  oraociei11.dll 放到C:\Python33\Lib\site-packages路径下

  4. entfrm-boot开发平台一览【entfrm开源模块化无代码开发平台】

    介绍 entfrm-boot是一个以模块化为核心的无代码开发平台,能够让中小企业快速从零搭建自己的开发平台:开箱即用,可插拔可自由组合:以模块化的方式,最大化的代码复用,避免重复开发:无代码可视化开发 ...

  5. 【编程思想】【设计模式】【创建模式creational】Pool

    Python版 https://github.com/faif/python-patterns/blob/master/creational/pool.py #!/usr/bin/env python ...

  6. GO类型转换

    golang []byte转string golang中,字符切片[]byte转换成string最简单的方式是 package main import ( "fmt" _ &quo ...

  7. linux环境下安装jdk,tomcat

    一.安装tomcat 1.使用docker安装(你得linux服务器上已经安装了docker) 1)执行命令: docker search tomcat; 2)选择第一个镜像进行下载,执行命令:doc ...

  8. show_slave_status参数详解

    #这个是指slave 连接到master的状态 #当前在等待主发送事件 Slave_IO_State: Waiting for master to send event #master地址 Maste ...

  9. SQLserver 2014使用Convert()函数获取时间

    select convert(char(100),GetDate(),120) as Date 第3个参数就是用来设置日期类型数据的显示样式的,下面介绍几种样式的参数 SELECT CONVERT(v ...

  10. Tableau如何绘制双柱折线组合图

    一.数据准备如下所示 二.将日期拖拽至列,销售额拖拽至行,结果如下所示 三.右键日期排序-选择手动排序 四.将指标拖拽至标记卡上 五.创建计算字段增长率 SUM(IF YEAR(日期)=2017 th ...