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

思路分析:当lowbit(AxorB)=2p 时,表示A与B的二进制表示的0-p-1位相等,第p位不同;考虑维护一棵字母树,将所有数字

转换为二进制形式并且从第0位开始插入树中,并在每个节点中记录通过该结点的数字数目;最后统计答案,对于每一个数字,

对于在其路径中的每一个结点X,假设其为第K层,统计通过与该结点不同的值的结点的数目count,则结果增加count*2k;

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = * + ;
int num[MAX_N];
struct Node{
Node *child[];
int count;
Node(){
count = ;
memset(child, , sizeof(child));
}
Node(int value){
count = value;
memset(child, , sizeof(child));
}
}; void Insert(Node *head, int value){
Node *pre = head, *next = NULL;
for (int i = ; i < ; ++ i){
if (pre->child[value & ] == NULL){
pre->child[value & ] = new Node();
pre = pre->child[value & ];
}else{
next = pre->child[value & ];
next->count++;
pre = next;
}
value >>= ;
}
} void MakeEmpty(Node *node){
node->count = ;
if (node->child[])
MakeEmpty(node->child[]);
if (node->child[])
MakeEmpty(node->child[]);
} long long Query(Node *head, int value){
Node *pre = head, *next = NULL, *other;
long long ret = ; for (int i = ; i < ; ++ i){
next = pre->child[value & ];
other = pre->child[(value & ) ^ ];
if (other)
ret = (ret + other->count * ( << i)) % ;
pre = next;
value >>= ;
}
return ret;
} int main(){
int case_times, n;
int case_id = ; scanf("%d", &case_times);
while (case_times--){
Node *head = new Node(); scanf("%d", &n);
for (int i = ; i < n; ++i){
scanf("%d", &num[i]);
Insert(head, num[i]);
} long long ans = ;
for (int i = ; i < n; ++i)
ans = (ans + Query(head, num[i])) % ;
MakeEmpty(head);
printf("Case #%d: %I64d\n", ++case_id, ans);
}
return ;
}

最新文章

  1. WPF学习系列 游戏-选张图片做成9宫格拼图
  2. android WebView介绍
  3. Oracle开机自启动
  4. null和undefined区别
  5. php 教程列表
  6. JQuery插件validate的Remote使用
  7. DOM(五)事件对象
  8. LVS负载均衡集群服务搭建详解(一)
  9. [LeetCode] Ugly Number II (A New Question Added Today)
  10. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)
  11. 手机网站中 限制图片宽度 JS图片等比例缩放
  12. TCP Connection Establishment and Termination
  13. android:versionCode和android:versionName
  14. PRG(Post/Redirect/Get)
  15. 每个Web开发人员应该知道的12个终端命令
  16. 当安全遇到java
  17. Java -- 基于JDK1.8的ArrayList源码分析
  18. java基础-2
  19. mysql 闪回原理
  20. Arrow functions and the ‘this’ keyword

热门文章

  1. BZOJ 1037: [ZJOI2008]生日聚会Party( dp )
  2. Java 动态代理(转)
  3. 替换 window.location当中的某个参数的值(而其它值不变)JS代码
  4. UPPH、UPH
  5. HDU 5877 Weak Pair(树状数组)
  6. 化简复杂逻辑,编写紧凑的if条件语句(二):依据if子句顺序化简条件
  7. &lt;td style=&quot;word-break:break-all&quot;&gt; 在html中控制自动换行
  8. 用DBMS_ADVISOR.SQLACCESS_ADVISOR创建SQL Access Advisor访问优化建议
  9. ANDROID自己定义视图——onLayout源代码 流程 思路具体解释
  10. AT&amp;T汇编试讲--获取CPU Vendor ID