hdu5536(01字典树)
Chip Factory
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? InputThe 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
OutputFor 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.暴力竟然能过(mmp)暴力出奇迹。。
2.正解:01字典树,附上神仙的博客~
https://www.cnblogs.com/letlifestop/p/10262761.html
下面是01字典树
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int sto[N][4],tot,a[1005];
long long flag[N],com[N];
void init()
{
for(int i=0;i<=tot;i++)
{
flag[i]=0;
com[i]=0;
for(int j=0;j<3;j++)
{
sto[i][j]=0;
}
}
tot=0;
}
void add(long long a)
{
int u=0;
for(int i=32;i>=0;i--)
{
int tmp=(a>>i)&1;
if(sto[u][tmp]==0)
sto[u][tmp]=++tot;
flag[sto[u][tmp]]++;
u=sto[u][tmp];
}
com[u]=a;
}
void Erase(long long a)
{
int u=0;
for(int i=32;i>=0;i--)
{
int tmp=(a>>i)&1;
flag[sto[u][tmp]]--;
u=sto[u][tmp];
}
}
long long query(long long a)
{
int u=0;
for(int i=32;i>=0;i--)
{
int tmp=(a>>i)&1;
if(sto[u][tmp^1]>0)
{
if(flag[sto[u][tmp^1]]>0)
u=sto[u][tmp^1];
else
u=sto[u][tmp];
}
else
{
if(flag[sto[u][tmp]]>0)
u=sto[u][tmp];
else
u=sto[u][tmp];
}
}
return com[u]^a;///返回异或值
}
int main()
{
int t,n,m,i;
tot=0;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
add(a[i]);
}
long long maxx=0;
for(int i=1;i<=n;i++)
{
Erase(a[i]);
for(int j=i+1;j<=n;j++)
{
Erase(a[j]);
maxx=max(query(a[i]+a[j]),maxx);
add(a[j]);
}
add(a[i]);
}
cout<<maxx<<'\n';
}
return 0;
}