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 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,5104]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
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 
 

Statistic | Submit | Discuss | Note

解析:建一个字典树,两个标记位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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 订机票时没有用常旅客卡怎么办 京东退货退款卖家已签收了怎么办 京东账号手机号换了密码忘了怎么办 qq飞车手游录像下载失败怎么办 手机知道qq号怎么破解密码怎么办 网上购物已付钱迟迟不发货怎么办 有人冒充微信头像和熟人借钱怎么办 微信红包输了50多万怎么办 脖子里的绳结接口磨脖子怎么办 母比格犬生了小狗毛色变了怎么办 母比格犬生了小狗毛色很差怎么办 貂皮大衣上的暗扣掉了怎么办 呢子夹克袖子长但又不想改短怎么办 黑色的衣服熨的有点发亮怎么办 宿管阿姨工作中与学生起冲突怎么办 中通快递发的衣服不合适怎么办 加盟母婴店如果不干了货怎么办 双十一搞活动买的东西退货怎么办 天猫预售30天不发货怎么办 天猫搞活动的商品总不发货怎么办 淘宝买的东西物流把货物损坏怎么办 苹果手机5s每部存满怎么办 天猫退货分开发货退的话怎么办 淘宝优惠券电脑端显示不出来怎么办 天猫退货退款寄错了怎么办 天猫已退款货又发过来了怎么办 小米商城预售订单点错退款了怎么办 淘宝发布宝贝类目价格受限制怎么办 2019天猫续签评分不达标怎么办 天猫店铺动态不达标不能续签怎么办 京东自提发现货有问题怎么办 京东试用成功商家不发货怎么办 在淘宝主页搜不到我的店铺名怎么办 淘宝发货单号填到别人那去了怎么办 买家申请淘宝介入后同意退款怎么办 淘宝卖家手机版购买装修模块怎么办 天猫店铺和淘宝店铺想要装修怎么办 支付宝转账银行卡卡号错误怎么办 淘宝图片空间照片全部删除了怎么办 我把淘宝图片空间照片删除了怎么办 淘宝发布宝贝怎么没知道品牌怎么办