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[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
using namespace std;
int a[];
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n;
while (~scanf("%d", &n))
{
for (int i=;i<n;++i) scanf("%d",a+i);
for (int i=;i>=;--i)
{
int sad=<<i, sum=-, cnt=;
for (int j=;j<n;++j)
{
if((sad&a[j])!=)
{
if(sum==-) sum=a[j];
else sum = sum & a[j];
cnt++;
}
}
int tmp=;
if (sum%sad==)
{
printf("%d\n",cnt);
for (int j=;j<n;++j)
{
if((sad&a[j])!=)
{
tmp++;
if(tmp!=cnt)
printf("%d ",a[j]);
else printf("%d\n",a[j]);
}
}
break;
}
}
} return ;
}
很神奇的做法。
开始被这道题目吓到了。o(╯□╰)o