Problem Description
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces n chips today, the i-th chip produced this day has a serial number si.
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
Input
The first line of input contains an integer T indicating the total number of test cases.
The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤109
There are at most 10 testcases with n>100
Output
For each test case, please output an integer indicating the checksum number in a line.
Sample Input
2
3
1 2 3
3
100 200 300
Sample Output
6
400
大致题意:给你n个数,让你从中选择三个不同的数,将其中两个数相加后异或上第三个数,使得结果最大,问最大结果为多少。
思路:先将这n个数建立成一颗01字典树,然后去枚举两个不同的数,将其从字典树中删除,然后再查询此时异或最大值,然后再向字典树中加入这两个数。
具体看代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef __int64 ll;
const int N=2;
const int M=1005;
typedef struct Node
{
int count;
Node *Next[N];
} Node;
void change(ll t,ll a[])//转换为二进制
{
for(ll i=0; i<32; i++)
a[i]=(t>>(32-i-1))&1;
}
Node * build()//建立节点
{
Node *node=(Node *)malloc(sizeof(Node));
node->count=0;
memset(node->Next,0,sizeof(node->Next));
return node;
}
void tire_insert(Node *root,ll a[])//插入
{
Node *p=root;
p->count++;
int i=0;
while(i<32)
{
if(p->Next[a[i]]==NULL)
p->Next[a[i]]=build();
p=p->Next[a[i]];
i++;
p->count++;
}
}
//删减和插入基本相似,就是 p->count++ 改为 p->count--
void tire_delate(Node *root,ll a[])//删减
{
Node *p=root;
p->count--;
int i=0;
while(i<32)
{
if(p->Next[a[i]]==NULL)
return;
p=p->Next[a[i]];
i++;
p->count--;
}
}
ll found(Node *root,ll a[])//查找函数
{
Node *p=root;
ll i=0,ans=0;
while(i<32)
{
if(p->Next[!a[i]]!=NULL&&p->Next[!a[i]]->count)
{//这里表示 如果a[i]的不为空,就相当于两个数的这一位不相同,然后在
//判断count是否为真
ans+=(1<<(32-i-1));
p=p->Next[!a[i]];
}
else
p=p->Next[a[i]];
i++;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int j,n,i;
Node *root=build();
ll a[32],tmp[M],ans=0;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%I64d",&tmp[i]);
change(tmp[i],a);
tire_insert(root,a);
}
for(i=0; i<n; i++)
{
change(tmp[i],a);//注意枚举的每个数要先转换成二进制
tire_delate(root,a);
for(j=i+1; j<n; j++)
{
change(tmp[j],a);//同上
tire_delate(root,a);
int t=tmp[i]+tmp[j];
change(t,a);
ans=max(ans,found(root,a));
change(tmp[j],a);
tire_insert(root,a);//并且这一步操作完记得再插入
}
change(tmp[i],a);
tire_insert(root,a);//同上
}
printf("%I64d\n",ans);
}
return 0;
}