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
Source
Recommend
liuyiding | We have carefully selected several similar problems for you: 6263 6262 6261 6260 6259
直接给数据跪了啊。
我有一个读入用cin读的,然后就T飞了。
对于这个题来说,对于每个元素,插到一颗0/1 Trie树里面,
对于读入的数,在0/1 Trie树上贪心的走,根据异或的原理,先走不同的,否则走相同的
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int MAXN=3500005;
const int INF=1e8+10;
inline int read()
{
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
struct node
{
int v,ch[2];
node(){v=0;}
void clear(){v=ch[0]=ch[1]=0;}
}T[MAXN];
int root=0,tot=0;
void Insert(int val)
{
int now=root;
for(int i=31;i>=0;i--)
{
int opt=(val&(1<<i))?1:0;
if(T[now].ch[opt]==0) T[now].ch[opt]=++tot;
now=T[now].ch[opt];
}
T[now].v=val;
}
int Query(int val)
{
int now=root;
for(int i=31;i>=0;i--)
{
int opt=(val&(1<<i))?1:0;
if(T[now].ch[opt^1]) now=T[now].ch[opt^1];
else now=T[now].ch[opt];
}
return T[now].v;
}
int main()
{
int Test=read(),cnt=0;
while( (++cnt)<=Test )
{
tot=0;root=0;
int N=read(),M=read();
for(int i=0;i<=MAXN;i++) T[i].clear();
for(int i=1;i<=N;i++)
{
int p=read();
Insert(p);
}
printf("Case #%d:\n",cnt);
while(M--)
{
int p=read();
printf("%d\n",Query(p));
}
}
return 0;
}