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:
maxi,j,k(si+sj)⊕sk
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
位运算真心方便。。参考大佬的博客写的。。自己写要长一半
(x>>i)&1 求出x转化为二进制后第i位的值,比%2不知道高到哪里去。。
num|=(1<
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxnode=32*10000;
int ch[maxnode][2],a[1005];
int id,cnt[maxnode];
int ans;
void Insert(int x)
{
int u=0;
for(int i=31;i>=0;i--)
{
int tmp=(x>>i)&1;
if(!ch[u][tmp])
{
memset(ch[id],0,sizeof(ch[id]));
cnt[id]=0;
ch[u][tmp]=id++;
}
u=ch[u][tmp];
cnt[u]++;
}
}
void find(int x)
{
int u=0,num=0;
for(int i=31;i>=0;i--)
{
int tmp=(x>>i)&1;
if(ch[u][tmp^1]&&cnt[ch[u][tmp^1]]) //tmp^1代表的是能与tmp异或为1的值
{
num=num|(1<<i); //给num的第i位赋值为1
u=ch[u][tmp^1];
}
else
u=ch[u][tmp];
}
ans=max(ans,num);
}
void del(int x)
{
int u=0;
for(int i=31;i>=0;i--)
{
int tmp=(x>>i)&1;
u=ch[u][tmp];
cnt[u]--;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
id=1;
memset(ch[0],0,sizeof(ch[0]));
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
Insert(a[i]);
}
ans=0;
for(int i=0;i<n;i++) //枚举i,j,将a[i]和a[j]从树种删除后再找最大值
{
del(a[i]);
for(int j=i+1;j<n;j++)
{
del(a[j]);
find(a[i]+a[j]);
Insert(a[j]);
}
Insert(a[i]);
}
cout<<ans<<endl;
}
return 0;
}