hdu 5269 ZYB loves Xor I(字典树)
题目链接: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 ;
}
hdu 5269 ZYB loves Xor I(字典树)的更多相关文章
- 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 ...
- hdu 5269 ZYB loves Xor I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- hdu 5269 ZYB loves Xor I && BestCoder Round #44
题意: ZYB喜欢研究Xor,如今他得到了一个长度为n的数组A. 于是他想知道:对于全部数对(i,j)(i∈[1,n],j∈[1,n]).lowbit(AixorAj)之和为多少.因为答案可能过大,你 ...
- 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取 ...
- HDU 5269 ZYB loves Xor I (二分法)
题意: 给出一个序列,对每两个数求异或结果后取最低位的1出来作为一个数,然后求这些数字的和.比如:{a,b,c},结果是lowbit(a^b)+lowbit(a^c)+lowbit(b^a)+lowb ...
- 【HDU】5269 ZYB loves Xor I
[算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...
- bestcoder r44 p3 hdu 5270 ZYB loves Xor II
这是昨晚队友跟我说的题,不知道当时是什么玄幻的事件发生了,,我看成了两两相乘的XOR 纠结了好长时间间 不知道该怎么办 今天早上看了下这道题,发现是两两相加的XOR 然后就想了想昨晚的思路 发现可做 ...
- HDU--5269 ZYB loves Xor I (字典树)
题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制 我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...
- ZYB loves Xor I(hud5269)
ZYB loves Xor I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
随机推荐
- if和switch
在 JavaScript 中,我们可使用以下条件语句:if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码if...else 语句 - ...
- Qt(QML)本地化
Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all Literial UI strings ...
- I - Doing Homework again
I - Doing Homework again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- java——HashCode和equal方法
equals()反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用,也可能是值类型的值. 而hashCode()是对象或变量通过哈希算法计算出的哈希值. 之所以有hashCode方 ...
- Android 电源系列小结s
package com.ritterliu.newBatteryWidget; import android.app.Activity; import android.app.Service; imp ...
- java调用C++ DLL库方法
最近一个项目要开发网页端人脸识别项目,人脸识别的算法已经写好,是C++版,但是网页端要求使用Java后台,这就涉及到Java调用DLL的问题.经过查找,实现了一个简单的例子. 1.第一步,先在Java ...
- ecshop删除商品函数
/** * 从回收站删除多个商品 * @param mix $goods_id 商品id列表:可以逗号格开,也可以是数组 * @return void */ function delete_goods ...
- 时尚B2B方兴未艾-Maker’s Row 获100万美元种子投资 |华丽志
时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 |华丽志 华丽志 » 网internet, 时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 由 luxeco 发 ...
- 字符串的MD5的32位加密和16位加密
import java.security.MessageDigest; import java.util.Locale; public class MD5Util { public static St ...
- 取得phpcms网站下所有栏目的内容链接
今天做了一个小功能,就是取得公司网站的所有文章的内容地址,公司网站是用phpcms 做的,感觉还蛮简单的,记录下: <?php $conf['DB_USER'] = 'user'; $conf[ ...