HDU5536 Chip Factory(01字典树)

描述

传送门:我是传送门

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: $max_(i,j,k) (s_i + s_j)⊕s_k$

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?

输入

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≤10^9$
There are at most 10 testcases with $n>100$

输出

For each test case, please output an integer indicating the checksum number in a line.

样例

输入

2
3
1 2 3
3
100 200 300

输出

6
400

Note

2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)

思路

  • 01字典树模板题
  • 暴力求解

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* =========================================================================
*
* Filename: hdu5536.cpp
*
* Link: http://acm.hdu.edu.cn/showproblem.php?pid=5536
*
* Version: 1.0
* Created: 2018/08/30 20时46分09秒
* Revision: none
* Compiler: g++
*
* Author: 杜宁元 (https://duny31030.top/), duny31030@126.com
* Organization: QLU_浪在ACM
*
* =========================================================================
*/
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pre(i,a,n) for(int i=a;i>=n;i--)
#define ll long long
#define max3(a,b,c) fmax(a,fmax(b,c))
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 10100;
int T,n,a[N];
struct node
{
int next[2];
int size;
}trie[32*N];

int tot = 1,root = 1;

void Init()
{
rep(i,1,tot)
{
trie[i].next[0] = 0;
trie[i].next[1] = 0;
trie[i].size = 0;
}
tot = 1;
}

void Insert(int x)
{
int o = root;
trie[o].size++;
for(int k = 30;k >= 0;k--)
{
int tmp = 0;
if(x&(1<<k))
tmp = 1;
if(!trie[o].next[tmp])
trie[o].next[tmp] = ++tot;

o = trie[o].next[tmp];
trie[o].size++;
}
}

void Delete(int x)
{
int o = root;
trie[o].size--;
for(int k = 30;k >= 0;k--)
{
int tmp = 0;
if(x&(1<<k))
tmp = 1;
o = trie[o].next[tmp];
trie[o].size--;
}
}

int Query(int x)
{
int o = root;
for(int k = 30;k >= 0;k--)
{
int tmp = 0;
if(x&(1<<k))
tmp = 1;
if(tmp)
{
if(trie[o].next[0] && trie[trie[o].next[0]].size)
o = trie[o].next[0];
else
o = trie[o].next[1], x^=(1<<k);
}
else
{
if(trie[o].next[1] && trie[trie[o].next[1]].size)
o = trie[o].next[1],x^=(1<<k);
else
o = trie[o].next[0];
}
}
return x;
}
int main()
{
ios
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
scanf("%d",&T);
while(T--)
{
int maxn = -INF;
scanf("%d",&n);
rep(i,1,n)
{
scanf("%d",&a[i]);
Insert(a[i]);
}
rep(i,1,n-1)
{
Delete(a[i]);
rep(j,i+1,n)
{
Delete(a[j]);
maxn = max(maxn,Query(a[i]+a[j]));
Insert(a[j]);
}
Insert(a[i]);
}
printf("%d\n",maxn);
Init();
}
fclose(stdin);
// fclose(stdout);
return 0;
}