hdu 3972 1 M possible

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

  给你3N+2个数字,有两个数字不是以3的倍数,找出这两个数。注意内存很小,不能全部存到数组里。

  如果是只有一个数不是3的倍数就好做,统计所有数二进制每位上的个数,把每位的个数分别对3取余,最后剩下的数转换成10进制就是答案。

  但是在有两个数的情况下,如果对3取余余数是2,说明这两个数二进制这位都有,但是如果余1,还像刚才那样用一维数组保存就不能确定是哪个数二进制这一位为1。解决的方法是用二维数组,cnt[i][j]表示二进制第i位和第j位都为1的数有多少个。如果所有cnt[i][i]都为2或者0,说明那两个数相等。如果找到一个cnt[i][i]为1的,那么所有cnt[i][j]为1的说明第i位为1的这个数第j位也为1。这样就可以确定第1个数,从cnt中减去第一个数情况,得到的就是第二个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define MAXM 20010
#define MAXNODE 4*MAXN
#define eps 1e-9
using namespace std;
int T,N,cnt[35][35];
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        memset(cnt,0,sizeof(cnt));
        int t;
        while(N--){
            scanf("%d",&t);
            for(int i=0;i<32;i++)
                for(int j=0;j<32;j++) if((t&(1<<i))&&(t&(1<<j))){
                    cnt[i][j]++;
                    if(j!=i) cnt[j][i]++;
                }
        }
        int flag=0,ans1=0,ans2=0;
        for(int i=0;i<32;i++)
            for(int j=0;j<32;j++) cnt[i][j]%=3;
        for(int i=0;i<32;i++) if(cnt[i][i]==1){
            flag=1;
            for(int j=0;j<32;j++) if(cnt[i][j]>0){
                ans1|=(1<<j);
                cnt[j][j]--;
            }
            for(int j=0;j<32;j++) if(cnt[j][j]) ans2|=(1<<j);
            break;
        }
        if(!flag){  //所有cnt[i][i]都不为1
            for(int i=0;i<32;i++) if(cnt[i][i]){
                ans1|=(1<<i);
                ans2|=(1<<i);
            }
        }
        if(ans1>ans2) swap(ans1,ans2);
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值