hdu5269 ZYB loves Xor I【字典树】

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(AiAi xor AjAj)) (i,j∈[1,n])(i,j∈[1,n]) 
We define that lowbit(x)=2k2k,k is the smallest integer satisfied ((xx and 2k2k)>0) 
Specially,lowbit(0)=0 
Because the ans may be too big.You just need to output ansans 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 nn 
The second line has nn integers A1A1,A2A2….AnAn 
n∈[1,5∗104]n∈[1,5∗104],Ai∈[0,229]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 


4 0 2 7 0 

2 6 5 4 0 
Sample Output 
Case #1: 36 
Case #2: 40


思路:首先要明白lowbit(x)的意思,lowbit(x)就相当于x转换成二进制后1的最低位转换成十

进制的数;因为是这是求两个值的异或,所以当前两个数转换成二进制后前缀必须相同的,那

才能使这一位如果不相同才是1的最低位,所以可以把数字转换成二进制倒着插入字典数中,

然后遍历整棵树,每个节点都记录了当前相同前缀串的个数,所以左右相乘;

#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef __int64 ll;
const int N=2;
const int M=50005;
const ll mod=998244353;

typedef struct Node
{
    ll count;
    Node *Next[N];
} Node;

Node * build()
{
    Node *node=(Node *)malloc(sizeof(Node));
    node->count=0;
    memset(node->Next,0,sizeof(node->Next));
    return node;
}

void change(ll t,ll a[])//转换倒着的二进制并
{
    for(ll i=0; i<31; i++)
        a[i]=((t>>i)&1);
}

void tire_insert(Node *root,ll a[])
{
    Node *p=root;
    ll i=0;
    while(i<31)
    {
        if(p->Next[a[i]]==NULL)
            p->Next[a[i]]=build();
        p=p->Next[a[i]];
        i++;
        p->count++;
    }
}

ll found(Node *root,ll t)
{
    Node *p=root;
    ll ans=0,l,r;//l和r表示具当且节点有相同前缀的个数
    if(p->Next[0]==NULL)
        l=0;
    else//保证前缀相同
    {
        l=p->Next[0]->count;
        ans=(ans+found(p->Next[0],t+1))%mod;//累加并计算左子树
    }
    if(p->Next[1]==NULL)
        r=0;
    else//保证前缀相同
    {
        r=p->Next[1]->count;
        ans=(ans+found(p->Next[1],t+1))%mod;//累加并计算右子树
    }
    ans+=r*l*(1<<t)%mod;//如果这里l或者r为0,那么这里的乘积也为0
    return ans;
}

int main()
{
    int T,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        ll i,tmp[M],a[31],n;
        Node *root=build();
        scanf("%I64d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%I64d",&tmp[i]);
            change(tmp[i],a);
            tire_insert(root,a);
        }
        printf("Case #%d: %I64d\n",++cas,found(root,0)*2%mod);
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值