hdu5269 数据结构Trie

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

     
     
      
      A
     
      
     
     
      
      xor
     
      
     
     
      
      B
     
     的答案为
     
     
      
      2p
     
     时,
     
     
      
      A
     
     
     
     
      
      B
     
     表示成二进制数后末
     
     
      
      p1
     
     位肯定相同
于是我们维护一颗字母树,将每个数表示成二进制数后翻转可以下,插入字母树
统计答案时,我们找出
     
     
      
      Ai
     
     的二进制数翻转后在字母树上的路径,对于路径上每个点
     
     
      
      x
     
     ,设他走的边是
     
     
      
      v
     
     ,且当前为第
     
     
      
      k
     
     位,则和他
     
     
      
      xor
     
     后lowbit为
     
     
      
      2k
     
     的数的个数为trans(x,v^1)的子树大小。
trans(x,v)表示字母树上在结点x,走连出去的字母为v的边到达的结点

   
   
//
//  main.cpp
//  bc44_B
//
//  Created by Fangpin on 15/6/13.
//  Copyright (c) 2015年 FangPin. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct Trie{
    int v;
    Trie *next[2];
    Trie(){next[0]=next[1]=NULL;v=1;}
}*rt;
const int M=998244353;

void build(){
    rt=new Trie;
}

int a[31],f[31];
long long ans=0;


void insert(){
    Trie *p=rt,*q;
    for(int i=0;i<31;++i){
        if(p->next[a[i]^1]!=NULL)
            ans=(ans+p->next[a[i]^1]->v*f[i])%M;
        if(p->next[a[i]]==NULL){
            q=new Trie;
            p->next[a[i]]=q;
            p=q;
        }
        else{
            p=p->next[a[i]];
            ++p->v;
        }
    }
}



void solve(Trie *root){
    for(int i=0;i<2;++i){
        if(root->next[i]!=NULL)
            solve(root->next[i]);
    }
    delete root;
}

void init(){
    f[0]=1;
    for(int i=1;i<31;++i){
        f[i]=f[i-1]<<1;
    }
}

int main(int argc, const char * argv[]) {
    // insert code here...
    int t;
    init();
    cin>>t;
    for(int ca=1;ca<=t;++ca){
        int n;
        scanf("%d",&n);
        build();
        ans=0;
        for(int i=0;i<n;++i){
            int tem;
            scanf("%d",&tem);
            for(int j=0;j<31;++j){
                a[j]=(tem>>j)&1;
            }
            insert();
        }
        solve(rt);
        cout<<"Case #"<<ca<<": "<<(ans*2)%M<<endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值