HDU 4825 Xor Sum (字典树求异或最大值)

来源:互联网 发布:淘宝投放手机详情 编辑:程序博客网 时间:2024/06/08 07:37
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=1e7+5;int ch[maxn][2];int sum[maxn];int le;void add(long long x){    int p=0;    for(int i=32; i>=0; i--)    {        int a=(x>>i)&1;        if(ch[p][a]==0)        {            ch[le][1]=ch[le][0]=0;            sum[p]=0;            ch[p][a]=le++;        }        p=ch[p][a];    }    sum[p]=x;}void query(long long x){    int p=0;    long long ans=0;    for(int i=32; i>=0; i--)    {        int a=((x>>i)&1)^1;        if(!ch[p][a])        {            a=a^1;        }        p=ch[p][a];    }    ans=sum[p];    printf("%I64d\n",sum[p]);}int main(){    int t;    scanf("%d",&t);    int ui=1;    while(t--)    {        le=1;        sum[0]=0;        ch[0][0]=ch[0][1]=0;        int n,m;        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++)        {            __int64 x;            scanf("%I64d",&x);            add(x);        }        printf("Case #%d:\n",ui++);        for(int i=0; i<m; i++)        {            __int64 x;            scanf("%I64d",&x);            query(x);        }    }}

 


Xor Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 1489    Accepted Submission(s): 624


Problem Description
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
 

Input
输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
 

Output
对于每组数据,首先需要输出单独一行”Case #?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
 

Sample Input
23 23 4 5154 14 6 5 63
 

Sample Output
Case #1:43Case #2:4
 

Source
2014年百度之星程序设计大赛 - 资格赛
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5866 5865 5864 5863 5862 
 

Statistic | Submit | Discuss | Note
Home | TopHangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2016 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.015600(s) query 7, Server time : 2016-09-08 21:43:26, Gzip enabledAdministration

0 0
原创粉丝点击