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. PC端页面转换成手机端页面的分辨率问题的理解

    PC端页面转换成手机端页面的分辨率问题的理解 px vw rem 假如就以a4纸模式为设计图 ,在a3纸模式中设计,然后设计出来展示在不同的a4纸模式上 通常是 750px -> 100vw / ...

  2. 学习java的第二十三天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  3. 生产调优1 HDFS-核心参数

    目录 1 HFDS核心参数 1.1 NameNode 内存生产配置 问题描述 hadoop-env.sh中配置 1.2 NameNode 心跳并发配置 修改hdfs-site.xml配置 1.3 开启 ...

  4. Yarn 公平调度器案例

    目录 公平调度器案例 需求 配置多队列的公平调度器 1 修改yarn-site.xml文件,加入以下从参数 2 配置fair-scheduler.xml 3 分发配置文件重启yarn 4 测试提交任务 ...

  5. Hadoop入门 概念

    Hadoop是分布式系统基础架构,通常指Hadoop生态圈 主要解决 1.海量数据的存储 2.海量数据的分析计算 优势 高可靠性:Hadoop底层维护多个数据副本,即使Hadoop某个计算元素或存储出 ...

  6. LeetCode两数之和

    LeetCode 两数之和 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...

  7. Vue函数防抖和函数节流

    函数防抖(debounce) 应用场景 登录.发短信等按钮避免用户点击太快,以致于发送了多次请求,需要防抖 调整浏览器窗口大小时,resize 次数过于频繁,造成计算过多,此时需要一次到位,就用到了防 ...

  8. 大数据学习day35----flume01-------1 agent(关于agent的一些问题),2 event,3 有关agent和event的一些问题,4 transaction(事务控制机制),5 flume安装 6.Flume入门案例

    具体见文档,以下只是简单笔记(内容不全) 1.agent Flume中最核心的角色是agent,flume采集系统就是由一个个agent连接起来所形成的一个或简单或复杂的数据传输通道.对于每一个Age ...

  9. Javascript 数组对象常用的API

    常用的JS数组对象API ES5及以前的Api ECMAScript5为数组定义了5个迭代方法,每个方法接收两个参数, 一个是每项运行的函数,一个是运行该函数的作用域对象(可选项),传入这些方法的函数 ...

  10. list.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><%@tag ...