codeforces 336C Vasily the Bear and Sequence(贪心)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Vasily the Bear and Sequence

Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum.

The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number band band ... and bk is divisible by number 2v without a remainder. If such number v doesn‘t exist (that is, for any non-negative integer v, number band b2and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1.

Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.

Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and".

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).

Output

In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk— the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.

Sample test(s)

input

51 2 3 4 5

output

24 5

input

31 2 4

output

14

题意:

给出n数,让你从中挑出尽可能多的数使得其能够被尽可能大的2的次幂整除。

分析:

由于给出的数的范围是在10的9次方之内的,所以可以枚举2的次幂数,由高到低枚举。因为要求取的数尽可能大,所以每次都先把这一位上是1的数都取出来,然后这些数的相与之后,若能够被2的这个次幂整除,则满足,否则,继续取小的次幂。

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 #include <iostream>
 6 #include <sstream>
 7 #include <ios>
 8 #include <iomanip>
 9 #include <functional>
10 #include <algorithm>
11 #include <vector>
12 #include <string>
13 #include <list>
14 #include <queue>
15 #include <deque>
16 #include <stack>
17 #include <set>
18 #include <map>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cmath>
22 #include <cstring>
23 #include <climits>
24 #include <cctype>
25 using namespace std;
26 #define XINF INT_MAX
27 #define INF 0x3FFFFFFF
28 #define MP(X,Y) make_pair(X,Y)
29 #define PB(X) push_back(X)
30 #define REP(X,N) for(int X=0;X<N;X++)
31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
33 #define CLR(A,X) memset(A,X,sizeof(A))
34 #define IT iterator
35 typedef long long ll;
36 typedef pair<int,int> PII;
37 typedef vector<PII> VII;
38 typedef vector<int> VI;
39 int a[100010];
40 int b[100010];
41 int main()
42 {
43     ios::sync_with_stdio(false);
44     int n;
45     cin>>n;
46     for(int i=1;i<=n;i++)cin>>a[i];
47     sort(a+1,a+1+n);
48     int i;
49     int tot=0;
50     int t=30;
51     while(t>=0){
52         int tmp=1<<t;
53         tot=0;
54         for(int i=1;i<=n;i++){
55             if((tmp&a[i]))b[tot++]=a[i];
56         }
57         if(!tot){
58             t--;
59             continue;
60         }
61         int temp=b[0];
62         for(int i=1;i<tot;i++){
63             temp=temp&b[i];
64         }
65         if(temp%tmp==0){
66             cout<<tot<<endl;
67             for(int i=0;i<tot;i++){
68                 if(i)cout<<" ";
69                 cout<<b[i];
70             }
71             cout<<endl;
72             break;
73         }
74         t--;
75     }
76     return 0;
77 }

时间: 2024-10-05 10:16:37

codeforces 336C Vasily the Bear and Sequence(贪心)的相关文章

codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vasily the Bear loves beautiful strings. String s is beautiful if it meets the following criteria: String s only consists of characters 0 and 1, at that

Codeforces Round #195 (Div. 2) A. Vasily the Bear and Triangle

水题,注意数据范围即可 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> #include <algorithm> #include <utility> using namespace std; typedef pair<int,int> Point; int main(){     int x,y;     cin >> x >>y;     Point a,b

HDU5014Number Sequence(贪心)

HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而且使得t = A0^B0 + Ai + 1 ^ Bi + 1 + ... + An ^ Bn 最大. 解题思路: 对于一个数字进行异或,要求结果最大的话,那么取这个数字的二进制互补数字是最好的情况,而且能够发现每次找到一个数字和相应的互补的数字都会是一段区间.就这样一段一段区间的去寻找每一个点相应的

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

hdu4915 Parenthese sequence 贪心O(n)解法

hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 82    Accepted Submission(s): 25 Problem Description bobo found an ancient string. The string contains only three c

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上

Codeforces --- 982C Cut &#39;em all! DFS加贪心

题目链接: https://cn.vjudge.net/problem/1576783/origin 输入输出: ExamplesinputCopy42 44 13 1outputCopy1inputCopy31 21 3outputCopy-1inputCopy107 18 48 104 76 59 33 52 102 5outputCopy4inputCopy21 2outputCopy0NoteIn the first example you can remove the edge bet

CodeForces 490E Restoring Increasing Sequence(贪心)

CodeForces 490E 题目大意:给N个正整数,然而这些正整数中间有些数字是被'?'遮挡住了,每个'?'可以还原回一个数字,希望给定的这N个整数形成一个递增的序列.可以的话,给出这N个整数的序列,不行返回N0. 解题思路:每个整数都在满足条件的情况下尽量的小,写了一个非递归版本的,发现要考虑的因素太多了,wa了太多遍.后面改成了递归版本的,但是这个比较耗时. 代码: #include <cstdio> #include <cstring> const int MAXL =

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will