codeforces195c

link:http://codeforces.com/problemset/problem/336/C

从大到小枚举,如果对应的二进制位不等于0,就加进来,最后的sum如果%2^k==0那么就是合法的。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <cctype>
 7 #include <algorithm>
 8 #include <queue>
 9 #include <deque>
10 #include <queue>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <vector>
15 #include <utility>
16 #include <functional>
17 #include <fstream>
18 #include <iomanip>
19 #include <sstream>
20 #include <numeric>
21 #include <cassert>
22 #include <ctime>
23 #include <iterator>
24 const int INF = 0x3f3f3f3f;
25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
26 using namespace std;
27 int a[111111];
28 int main(void)
29 {
30     #ifndef ONLINE_JUDGE
31     freopen("in.txt", "r", stdin);
32     #endif // ONLINE_JUDGE
33     int n;
34     while (~scanf("%d", &n))
35     {
36         for (int i=0;i<n;++i) scanf("%d",a+i);
37         for (int i=29;i>=0;--i)
38         {
39             int sad=1<<i, sum=-1, cnt=0;
40             for (int j=0;j<n;++j)
41             {
42                 if((sad&a[j])!=0)
43                 {
44                     if(sum==-1) sum=a[j];
45                     else sum = sum & a[j];
46                     cnt++;
47                 }
48             }
49             int tmp=0;
50             if (sum%sad==0)
51             {
52                 printf("%d\n",cnt);
53                 for (int j=0;j<n;++j)
54                 {
55                     if((sad&a[j])!=0)
56                     {
57                         tmp++;
58                         if(tmp!=cnt)
59                             printf("%d ",a[j]);
60                         else printf("%d\n",a[j]);
61                     }
62                 }
63                 break;
64             }
65         }
66     }
67 
68     return 0;
69 }

很神奇的做法。

开始被这道题目吓到了。o(╯□╰)o

posted on 2013-08-15 18:41  aries__liu  阅读(197)  评论(0编辑  收藏  举报