Xor Sum

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

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
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
 
Sample Output
Case #1:
4
3
Case #2:
4
 字典树保存
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int t,n,m,T;
struct tree
{
struct tree *next[];
int val;
tree()
{
val=;
for(int i=;i<;i++)
next[i]=NULL;
}
};
tree *root;
void insert(int x)
{
int a[];
tree *p=root,*q;
for(int i=;i<;i++)
a[i]=(x&(<<i))?:;
for(int i=;i>=;i--)
{
if(p->next[a[i]]==NULL)
{
q=new tree();
p->next[a[i]]=q;
}
p=p->next[a[i]];
}
p->val=x;
}
void query(int x)
{
tree *p=root;
int a[];
for(int i=;i<;i++)
a[i]=x&(<<i)?:;
for(int i=;i>=;i--)
{
if(!p->next[a[i]^]) p=p->next[a[i]];
else p=p->next[a[i]^];
}
printf("%d\n",p->val);
}
void del(tree *root)
{
for(int i=;i<;i++)
if(root->next[i]!=NULL)
del(root->next[i]);
free(root);
}
int main()
{
scanf("%d",&t);
T=t;
while(t--)
{
root=new tree();
scanf("%d%d",&n,&m);
for(int i=,x;i<n;i++)
{
scanf("%d",&x);
insert(x);
}
printf("Case #%d:\n",T-t);
for(int i=,x;i<m;i++)
{
scanf("%d",&x);
query(x);
}
del(root);
}
return ;
}

最新文章

  1. Xamarin.Forms 免费电子书
  2. python函数和常用模块(三),Day5
  3. ios 宏定义 系统版本 判定
  4. [hadoop] 一些基础概念
  5. java中好玩的案例
  6. 检测是否支持HTML5中的Video标签
  7. 禁用nginx的access日志
  8. 在C#中如何确定一个文件是不是文本文件,以及如何确定一个文件的类型
  9. orchard相关网址
  10. JS实现鼠标移上去图片停止滚动移开恢复滚动效果
  11. [算法题] Search in Rotated Sorted Array ii
  12. FastDFS 简介
  13. named let 递归和闭包的利器
  14. PHP实现开心消消乐的算法示例
  15. java 日期格式化
  16. ==、Equals 、ReferenceEquals它们的异同
  17. sequence测试中的使用
  18. 用户 'XXX\SERVERNAME$' 登录失败。 原因: 找不到与提供的名称匹配的登录名。 [客户端: ]
  19. GIL学习
  20. ajax readystatue

热门文章

  1. Java获取项目路径下的方法(全)
  2. vue Render scopedSlots
  3. bzoj4868: [Shoi2017]期末考试(三分法)
  4. zzulioj--1712--神秘的数列(水题)
  5. xBIM 高级03 更改日志创建
  6. js小知识 正则表达
  7. 内部div自动扩张剩余宽度
  8. 【原创】MemCached中的参数解释
  9. 解决IE7、IE8样式不兼容问题
  10. HDU 1556 Color the ball【树状数组】