HDU 5269 ZYB loves Xor I [01字典树]【思维】
来源:互联网 发布:怎么重启sybase数据库 编辑:程序博客网 时间:2024/06/08 06:23
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5269
—————————————————————————————-.
ZYB loves Xor I Accepts: 142 Submissions: 696
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
问题描述
ZYB喜欢研究Xor,现在他得到了一个长度为nn的数组A。于是他想知道:对于所有数对
之和为多少.由于答案可能过大,你需要输出答案对998244353取模后的值
定义
,其中k是最小的满足
特别地:lowbit(0)=0
输入描述
一共
第一行一个正整数n,表示数组长度
第二行n个非负整数,第
输出描述
每组数据输出一行Case #x: ans。x表示组数编号,从1开始。ans为所求值。
输入样例
2
5
4 0 2 7 0
5
2 6 5 4 0
输出样例
Case #1: 36
Case #2: 40
—————————————————————————————-.
解题思路:
这道题首先想的是用数组处理二进制每一位的0,1的个数,然后进行统计,在处理一下细节,但是最后发现只有在处理二进制下最后一位为1的数才好统计.于是GG
最后看了题解,提到了字典树,,,,顿时茅塞顿开,
我们只要从低位开始插入字典树中,并统计个数,每次统计就是二进制位上和它相反的数的个数*
边插入边统计就好了.
附本题代码
—————————————————————————————-.
#include <bits/stdc++.h>using namespace std;#define INF (~(1<<31))#define INFLL (~(1ll<<63))#define pb push_back#define mp make_pair#define abs(a) ((a)>0?(a):-(a))#define lalal puts("*******");#define s1(x) scanf("%d",&x)#define Rep(a,b,c) for(int a=(b);a<=(c);a++)#define Per(a,b,c) for(int a=(b);a>=(c);a--)#define no puts("NO")typedef long long int LL ;typedef unsigned long long int uLL ;const int MOD = 998244353;const int N = 1500000+5;const double eps = 1e-6;const double PI = acos(-1.0);/***********************************************************************/int trie[N][2],val[N],cnt;void trieinsert(int x){ int now = 0; for(int i=0;i<30;x>>=1,++i){ if(!trie[now][x%2]) trie[now][x%2]=++cnt; now = trie[now][x%2]; val[now]++; }}LL query(int x){ int now = 0; LL ans =0ll; for(int i=0;i<30;x>>=1,++i){ if(!trie[now][x%2]) trie[now][x%2]=++cnt; ans += (2ll<<i)*val[trie[now][1-x%2]]; ans%=MOD; now = trie[now][x%2]; val[now]++; } return ans ;}int main(){ int _ = 1,kcase ; while(~scanf("%d",&_)){ kcase = 0; while(_--){ int n; scanf("%d",&n); cnt = 0; LL x; LL ans = 0ll; for(int i=0;i<n;i++){ scanf("%I64d",&x); ans+=query(x); ans%=MOD; //printf("%I64d\n",ans); //trieinsert(x); } for(int i=0;i<=cnt;i++){ trie[i][0]=trie[i][1]=val[i]=0; } printf("Case #%d: %I64d\n",++kcase,ans); } } return 0;}
- HDU 5269 ZYB loves Xor I [01字典树]【思维】
- hdu 5269 ZYB loves Xor I(字典树)
- hdu 5269 ZYB loves Xor I(字典树)
- HDU 5269 ZYB loves Xor I( 01 Trie 树)
- 字典树 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 01trie
- HDU 5269 && BestCoder #44 1002 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 贡献+Trie
- HDOJ 5269 ZYB loves Xor I
- eclipse插件之preference 开发
- 162. Find Peak Element
- HDU5727 Necklace (全排列+二分匹配)
- QT、MFC、vc++
- mfc 编辑控件显示系统时间
- HDU 5269 ZYB loves Xor I [01字典树]【思维】
- python--递归计算n的价乘
- ios网络请求框架,基于AFNetworking封装,其中缓存模块依赖TMCache,JSON解析模块依赖MJExtension,使用更加简单方便
- Androd 7.0编译错误合集
- 【VS开发】【数据库开发】libevent windows下基于VS2010的编译
- pytorch学习笔记(二):gradient
- 保存BITMAP位图为各种图片
- 167. Two Sum II - Input array is sorted
- 使用MVP+RxAndroid+DroiBaaS打造云后台App—校园日记