hdu 5269 ZYB loves Xor I(字典树)
来源:互联网 发布:六爻排盘软件下载 编辑:程序博客网 时间:2024/06/08 15:02
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269
ZYB loves Xor I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1149 Accepted Submission(s): 510
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 outputans mod 998244353
We define that lowbit(x)=
Specially,lowbit(0)=0
Because the ans may be too big.You just need to output
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 integern
The second line hasn integers A1 ,A2 ....An
n∈[1,5∗104] ,Ai∈[0,229]
The first line has an integer
The second line has
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
254 0 2 7 052 6 5 4 0
Sample Output
Case #1: 36Case #2: 40
Source
BestCoder Round #44
Recommend
hujie | We have carefully selected several similar problems for you: 6193 6192 6191 6190 6189
解析:建一个字典树,两个标记位0 1,分别存到当前0 1的个数,创建完成后,再遍历所有的数字,求下ans即可
代码:
#include<bits/stdc++.h>using namespace std;const int N = 50009;typedef long long LL;const LL mod = 998244353ll;int a[N];LL ans;typedef struct node{ int cnt[2]; struct node *nx[3]; node() { memset(cnt, 0, sizeof(cnt)); memset(nx, 0, sizeof(nx)); }}Q, *T;void Creat(int n, T &root){ T q = root; int k = 0; while(true) { int id = n % 2; if(q->nx[id] == NULL) q->nx[id] = new node(); q->cnt[id]++; q = q->nx[id]; k++; if(k == 30) break; n /= 2; }}void Find(int n, T &root, int len){ T q = root; int bits[33]; for(int i = 0; i <= 30; i++) { bits[i] = n % 2; n /= 2; } int k = 1, num = 0; for(int i = 0; i <= 30; i++) { int id = bits[i]; num += q->cnt[!id]; ans += k*q->cnt[!id]; ans %= mod; q = q->nx[id]; if(num >= len-1) break; k *= 2; }}void F(T S){ if(S == NULL) return ; for(int i = 0; i < 2; i++) { if(S->nx[i]) F(S->nx[i]); } delete S; S = NULL;}int main(){ int t, n, cnt = 0; scanf("%d", &t); while(t--) { T root = new node(); scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); Creat(a[i], root); } ans = 0; for(int i = 1; i <= n; i++) { Find(a[i], root, n); } printf("Case #%d: %lld\n", ++cnt, ans); F(root); } return 0;}
阅读全文
0 0
- hdu 5269 ZYB loves Xor I(字典树)
- HDU 5269 ZYB loves Xor I [01字典树]【思维】
- hdu 5269 ZYB loves Xor I(字典树)
- 字典树 hdu5269 ZYB loves Xor I
- hdu5269 ZYB loves Xor I (字典树)
- HDU 5269 ZYB loves Xor I——BestCoder Round #44(字典树)
- hdu 5269 ZYB loves Xor I
- hdu 5269 ZYB loves Xor I
- hdu 5269 ZYB loves Xor I
- HDU 5269 ZYB loves Xor I
- HDU-5269 ZYB loves Xor I
- HDU 5269 ZYB loves Xor I( 01 Trie 树)
- HDU 5269 ZYB loves Xor I(位运算)
- HDU 5269 && BestCoder #44 1002 ZYB loves Xor I (分治)
- hdu 5269 ZYB loves Xor I && BestCoder Round #44
- HDU 5269 ZYB loves Xor I 01trie
- HDU 5269 ZYB loves Xor I 贡献+Trie
- HDOJ 5269 ZYB loves Xor I
- C++ 批量读取文件夹中的文件名
- windows+php+apache本地配置HTTPS
- 《hbase学习》-05-HBase分布式安装
- 统计学中的 DATA:Nominal,Ordinal, Interval and Ratio怎么区别
- 常用HTTP状态码分类大全
- hdu 5269 ZYB loves Xor I(字典树)
- HDU 3952 Fruit Ninja(线段与直线相交枚举)
- PAT-1057 Stack (树状数组 + 二分查找)
- Oracle10g修改默认的8080端口
- 神经网络中注意力机制(Attention Mechanism)
- springview
- 致己
- Qt 之 QSqlRelationalDelegate Class
- android Context 详解