题目描述
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?
输入
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≤s i≤109
- 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
题目大意:给你n个数,求
01字典树模板题,就是多了个删除操作。先把所有的数存进字典树内,然后枚举i,j,在字典树删除i,j,求亦或和最大值,再将i,j存入字典树即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int trie[31 * maxn][2];
int tot;
int z[maxn];
int siz[31 * maxn];
void insert(int j)
{
int tmp = 0;
for (int i = 31; i >= 0; --i)
{
int tmpp = (j >> i) & 1;
if (!trie[tmp][tmpp])
trie[tmp][tmpp] = ++tot;
tmp = trie[tmp][tmpp];
++siz[tmp];
}
}
void dele(int j)
{
int tmp = 0;
for (int i = 31; i >= 0; --i)
{
int tmpp = (j >> i) & 1;
tmp = trie[tmp][tmpp];
--siz[tmp];
}
}
int findd(int j)
{
int tmp = 0;
int cnt = 0;
for (int i = 31; i >= 0; --i)
{
int tmpp = (j >> i) & 1;
if (trie[tmp][tmpp ^ 1] && siz[ trie[tmp][tmpp ^ 1] ])
{
cnt += 1 << i;
tmp = trie[tmp][tmpp ^ 1];
}
else
tmp = trie[tmp][tmpp];
}
return cnt;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(trie, 0, sizeof(trie));
memset(siz, 0, sizeof(siz));
int n;
tot = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &z[i]);
int ans = 0;
for (int i = 1; i <= n; ++i)
insert(z[i]);
for (int i = 1; i < n; ++i)
{
dele(z[i]);
for (int j = i + 1; j <= n; ++j)
{
dele(z[j]);
ans = max(ans, findd(z[i] + z[j]));
insert(z[j]);
}
insert(z[i]);
}
printf("%d\n", ans);
}
return 0;
}