codeforces195c

August 15, 2013
CodeForces

link:http://codeforces.com/problemset/problem/336/C 从大到小枚举,如果对应的二进制位不等于0,就加进来,最后的sum如果%2^k==0那么就是合法的。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
int a[111111];
int main(void)
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int n;
    while (~scanf("%d", &n))
    {
        for (int i=0;i<n;++i) scanf("%d",a+i);
        for (int i=29;i>=0;--i)
        {
            int sad=1<<i, sum=-1, cnt=0;
            for (int j=0;j<n;++j)
            {
                if((sad&a[j])!=0)
                {
                    if(sum==-1) sum=a[j];
                    else sum = sum & a[j];
                    cnt++;
                }
            }
            int tmp=0;
            if (sum%sad==0)
            {
                printf("%d\n",cnt);
                for (int j=0;j<n;++j)
                {
                    if((sad&a[j])!=0)
                    {
                        tmp++;
                        if(tmp!=cnt)
                            printf("%d ",a[j]);
                        else printf("%d\n",a[j]);
                    }
                }
                break;
            }
        }
    }

    return 0;
}

很神奇的做法。 开始被这道题目吓到了。o(╯□╰)o

comments powered by Disqus