hdu5536Chip Factory

算机学院大学生程序设计竞赛(新生为主) 

Chip Factory

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 640    Accepted Submission(s): 320


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.

1T1000
3n1000
0si109
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
 

Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
 
#include
const int maxn=51000;
const int ch_size=2;
using namespace std;
int n,a[maxn];
unsigned int ans;
struct Trie{
    int a[maxn][ch_size],tot,sum[maxn];
    void init(){sum[0]=tot=0;memset(a[0],0,sizeof(a[0]));}
    void clear(int x){memset(a[x],0,sizeof(a[x]));sum[x]=0;}
    void insert(unsigned int v){
        int x=0;sum[0]++;
        for(int i=31;i>=0;i--){
            bool son=((v>>i)&1);
            if(a[x][son]){
                x=a[x][son];
            }else{
                clear(++tot);
                x=a[x][son]=tot;

            }
            sum[x]++;
        }
    }
    void update(unsigned int v,unsigned int f1,unsigned int f2){
        int x=0;unsigned int tmp=0;
        bool k1=1,k2=1;
       // printf("%u %u %u\n",v,f1,f2);
        for(int i=31;i>=0;i--){
            bool son=((v>>i)&1);
           // printf("%d %d %d %d %d %d %d %u\n",i,son,(((f1>>i)&1)^son),(((f2>>i)&1)^son),sum[a[x][son^1]],k1,k2,1u<>i)&1)^son)-k2*(((f2>>i)&1)^son)>0){
                x=a[x][son^1];
                if((((f1>>i)&1)^son)==0)k1=0;

                if((((f2>>i)&1)^son)==0)k2=0;
                tmp|=(1u<>i)&1)!=son)k1=0;
                if(((f2>>i)&1)!=son)k2=0;
            }
        }
        ans=max(ans,tmp);
      //  printf("tmp = %d\n",tmp);
    }
}T;
void work(){
    T.init();
    scanf("%d",&n);
    ans=0;
    for(int i=1;i<=n;i++){
        scanf("%u",&a[i]);
        T.insert(a[i]);
    }
    for(int i=1;i<=n;i++)
    for(int j=i+1;j<=n;j++){
        unsigned int t=a[i]+a[j];
        T.update(t,a[i],a[j]);
    }
    printf("%u\n",ans);
}
int main(){
    int t;scanf("%d",&t);
    while(t--)work();
    return 0;
}


你可能感兴趣的:(hdu,我写的,trie)