码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5269 数据结构Trie

时间:2015-06-14 09:34:19      阅读:150      评论:0      收藏:0      [点我收藏+]

标签: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,5?104]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时,AB表示成二进制数后末p?1位肯定相同
于是我们维护一颗字母树,将每个数表示成二进制数后翻转可以下,插入字母树
统计答案时,我们找出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;
}



hdu5269 数据结构Trie

标签:hdu5269   trie   

原文地址:http://blog.csdn.net/fangpinlei/article/details/46485619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!