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 nn chips today, the ii-th chip produced this day has a serial number sisi.
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)⊕skmaxi,j,k(si+sj)⊕sk
which i,j,ki,j,k are three different integers between 11 and nn. 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 TT indicating the total number of test cases.
The first line of each test case is an integer nn, indicating the number of chips produced today. The next line has nn integers s1,s2,..,sns1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤10001≤T≤1000
3≤n≤10003≤n≤1000
0≤si≤1090≤si≤109
There are at most 1010 testcases with n>100n>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
1,直接暴力:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=1050;
ll a[maxn];
struct Node
{ int first;
int second;
ll all;
Node(int _a,int _b,ll _c){first=_a;second=_b;all=_c;}
};
vector<Node>sum;
int main()
{ int m;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&m);
sum.clear();
for(int i=1;i<=m;i++)
scanf("%lld",&a[i]);
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++){
sum.push_back(Node(i,j,a[i]+a[j]));
}
}
ll ans=-1;
for(int i=0;i<sum.size();i++){
for(int j=1;j<=m;j++){
if(sum[i].first!=j&&sum[i].second!=j){
ans=max(ans,a[j]^sum[i].all);
}
}
}
printf("%lld\n",ans);
}
return 0;
}
2.01字典树
#include <bits/stdc++.h>
using namespace std;
/* 这道题也可以用01字典树。
这个是带删除的方法,
用数组模拟。
通过给
ch里存的是下一个节点的下标。
而下标并不是就开好的,而是慢慢加的,
val是当前点的经过。
*/
const int maxn=32000+1000;
int ch[maxn][2];
int val[maxn];
int a[maxn];
int sz;//表示标号。
void init(){
sz=1;//初始那个标号为1,
memset(ch,0,sizeof(ch));
}
void update(int v,int d){
int u=0;
for(int i=31;i>=0;i--){
int sign=(v>>i)&1;
if(!ch[u][sign]){
//memset(ch[sz],0,sizeof(sz));
ch[u][sign]=sz;
val[sz]=0;
sz++;
//val[sz]=0;
}
u=ch[u][sign];
val[u]+=d;
}
}
int query(int v) {
int ans = 0, u = 0;
for(int i = 31; i >= 0; i--) {
int c = (v >> i) & 1;
if(ch[u][c^1] && val[ch[u][c^1]]) {
ans |= (1 << i);
u = ch[u][c^1];
}
else u = ch[u][c];
}
return ans;
}
int main()
{ int t,m;
scanf("%d",&t);
while(t--){
memset(ch,0,sizeof(ch));
//memset(val,0,sizeof(val));
init();
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&a[i]);
update(a[i],1);
}
int ans=-1;
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(i==j) continue;
update(a[i],-1);//删掉
update(a[j],-1);//删掉一个
ans=max(ans,query(a[i]+a[j]));
//cout<<ans<<endl;
update(a[i],1);//再加上
update(a[j],1);//再加上、
}
}
printf("%d\n",ans);
}
return 0;
}