hdu 5269 ZYB loves Xor I (01 字典树和 lowbit 的巧妙结合)

ZYB loves Xor I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1188    Accepted Submission(s): 532


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
 
 
2 5 4 0 2 7 0 5 2 6 5 4 0
 

Sample Output
 
 
Case #1: 36 Case #2: 40

题意: 给你一个序列,然后你的目的是求出任意两个数的xor的lowbit。

思路: 我们知道lowbit(x) 的大小取决于最低位为1 的位是哪一位。所以我们就可以利用01 字典树的两个孩子的个数相乘再乘树深对应的价值就可以。(当然对于一个数就要从第0 位开始建树)

代码:

#include<bits/stdc++.h>

using namespace std;

#define Memset(x, a) memset(x, a, sizeof(x))
typedef long long ll;
const int maxn = 5e4+5;//集合中的数字个数
int ch[32*maxn][2];         //节点的边信息
ll val[32*maxn];            //节点存储的值
int sz;                     //树中当前节点个数
int num[32*maxn];           // 出现的次数

void init(){
    Memset(ch[0],0);           //树清空
    memset(num,0,sizeof(num));
    sz=1;
}

void _insert(ll a){//在字典树中插入 a
                  //和一般字典树的操作相同 将X的二进制插入到字典树中
    int rt=0;
    for(int i=0;i<=30;i++){
        int c=((a>>i)&1);
        //printf("%d\n",c);
        if(!ch[rt][c]){
            Memset(ch[sz],0);
            val[sz]=0;
            num[sz]=0;
            ch[rt][c]=sz++;
        }
        rt=ch[rt][c];
        num[rt]++;
    }
    //printf("sz %d\n",sz);
    val[rt]=a;     //最后的节点插入value
}

ll ans;
#define mod 998244353

const int N=5e4+5;
ll x;
int n;
ll f[35];

void init1()
{
    f[0]=1;
    for(int i=1;i<=32;i++) f[i]=f[i-1]*2;
    return ;
}

void dfs(int rt,int dep)
{
    int l,r;
    l=ch[rt][0];  r=ch[rt][1];
    ll c1,c2;
    if(l!=0&&r!=0){
        c1=num[l];  c2=num[r];
        //printf("num : %d %d cnt : %lld %lld  dep : %d\n",l,r,c1,c2,dep);
        ans=(ans+(c1*c2*f[dep])%mod)%mod;
        //cout<<ans<<endl;
        dfs(l,dep+1);  dfs(r,dep+1);
    }
    else if(l!=0)
    {
        dfs(l,dep+1);
    }
    else if(r){
        dfs(r,dep+1);
    }
    return ;
}

int main()
{
    int T;
    init1();
    scanf("%d",&T);
    int kk=0;
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            _insert(x);
        }
        //printf("%d\n",sz);

        //for(int i=1;i<=sz;i++) printf("%d ",num[i]);
        //cout<<endl; 
        
        ans=0;
        dfs(0,1);
        printf("Case #%d: ",++kk);
        cout<<ans<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值