hdu 3972 1 M possible 从3*n+2中找出两个特殊的数 (其余3*n每三个相同)

Problem Description
There are 3*N+2 nonnegative integers. Except two special integers, the rest 3*N integers can form N triple groups: { Vi, Vj, Vk| Vi=Vj=Vk}, each integer are at most in one triple group.

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)
 


 

Input
The first line of the input contains an integer T(T<=3), indicating the number of test cases.

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).
 


 

Output
For each test case, print a line containing two integers a, b (a<=b) separate by a blank. a, b is the desire two special integers.
 


 

Sample Input
  
  
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
 


 

Sample Output
  
  
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值