Your task is to find the two special integers.
Now, suneast is so boring. He wants to compete with you. As there is no computer available for him, he has to find the integers use his eyes! Unbelievable! So just for fairness, your program can access ONLY 1MB memory!
Tips: Impossible itself says 1 M possible ------ Tourist’s quote (a topcoder target member)
Each case begin with a line containing an integers N(2<=N<=300000, N=2(mod3) ).
The second lines contains N nonnegative integers Vi (1<=Vi<=2147483647).
2 2 1 2 23 1 3 1 4 5 2 1 1 3 1 4 5 2 1 1 3 1 4 5 2 1 2 1
1 2 1 2
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
int len;
int a[32];
int val()
{
int cnt=0;
for(int i=len-1;i>=0;i--) cnt=cnt*3+a[i];
return cnt;
}
};
Node toThree(int n)//将n转换成3进制
{
Node t;
t.len=0;memset(t.a,0,sizeof(t.a));//逆序
while(n) t.a[t.len++]=n%3,n/=3;//多余高位0
return t;
}
void Xor3(Node& t,Node& b)//t^=b; 3进制异或
{
t.len=max(t.len,b.len);
for(int i=0;i<t.len;i++)
{
t.a[i]=(t.a[i]+b.a[i])%3;
}
for(;t.len>0&&t.a[t.len-1]==0;t.len--);
}
Node sum[3][32];
int main()
{
int ci;scanf("%d",&ci);
int n;
while(ci--)
{
memset(sum,0,sizeof(sum));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int tn;scanf("%d",&tn);//t>0
Node t=toThree(tn);
for(int j=0;j<32;j++)
{
Xor3(sum[t.a[j]][j],t);
}
}
int tp,flag=0;
int p[3];
for(int i=0;i<32;i++)
{
tp=0;
for(int j=0;j<3;j++)
{
if(sum[j][i].len) p[tp++]=j;
}
if(tp==2)
{
flag=1;
int rmax=sum[p[0]][i].val();
int rmin=sum[p[1]][i].val();
if(rmax<rmin) swap(rmax,rmin);
printf("%d %d\n",rmin,rmax);
break;
}
}
if(!flag)
{
for(int j=0;j<3;j++)
{
if(sum[j][0].len)
{
Xor3(sum[j][0],sum[j][0]);
int r=sum[j][0].val();
printf("%d %d\n",r,r);
break;
}
}
}
}
return 0;
}