转载请注明出处: 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
5
1 2 3 4 5
output
2
4 5
input
3
1 2 4
output
1
4

题意:

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

分析:

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

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int a[];
int b[];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
sort(a+,a++n);
int i;
int tot=;
int t=;
while(t>=){
int tmp=<<t;
tot=;
for(int i=;i<=n;i++){
if((tmp&a[i]))b[tot++]=a[i];
}
if(!tot){
t--;
continue;
}
int temp=b[];
for(int i=;i<tot;i++){
temp=temp&b[i];
}
if(temp%tmp==){
cout<<tot<<endl;
for(int i=;i<tot;i++){
if(i)cout<<" ";
cout<<b[i];
}
cout<<endl;
break;
}
t--;
}
return ;
}

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

  1. codeforces C. Vasily the Bear and Sequence 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...

  2. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

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

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vas ...

  4. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  5. codeforces A. Vasily the Bear and Triangle 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/A 好简单的一条数学题,是8月9日的.比赛中没有做出来,今天看,从pupil变成Newbie了,那个 ...

  6. codeforces 336D. Vasily the Bear and Beautiful Strings 组合数学 dp

    题意: 给出n,m,g,求好串的个数 0 <= n,m <= 10^5,n + m >= 1,0 <= g <= 1 好串的定义: 1.只由0,1组成,并且恰好有n个0, ...

  7. HDU5014Number Sequence(贪心)

    HDU5014Number Sequence(贪心) 题目链接 题目大意: 给出n,然后给出一个数字串,长度为n + 1, 范围在[0, n - 1].然后要求你找出另外一个序列B,满足上述的要求,而 ...

  8. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  9. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

随机推荐

  1. liunx下安装mysql没有初始密码的解决方法

    #/etc/init.d/mysql stop #cd /usr/local/mysql #mysqld_safe --user=mysql --skip-grant-tables --skip-ne ...

  2. 插件的理解$.extend()与$.fn.extend()

    插件的理解.$.extend()与$.fn.extend()    插件开发包括两种:1.类级别的插件开发,即$.extend()扩展jquery对象本身:2.对象级别的插件开发,即$.fn.exte ...

  3. 转载: js jquery 获取当前页面的url,获取frameset中指定的页面的url(有修改)

    转载网址:http://blog.csdn.net/bestlxm/article/details/6800077 js jquery 怎么获取当前页面的url,获取frameset中指定的页面的ur ...

  4. 六度分离--hdu1869

    六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. 如何实现SQL事务的提交,又不对外进行污染

    一.以下是本人的一点思路: 1.在事务方法中,参数运用委托Func,选用Func 的原因是多入参,单一出参2.事务传参运用泛型,选用泛型的原因是可以减少代码量,类型安全 二.说明中涉及4个类:1.Or ...

  6. Oracle asm介绍和安装linux+oracle10g+asm过程

    Oracle asm介绍和安装linux5.2+oracle10g+asm过程   1)ASM(自动存储管理)的来由:   ASM是Oracle 10g R2中为了简化Oracle数据库的管理而推出来 ...

  7. ubuntu 执行apt-get update 提示无法获得锁

    问题如下: y@y:~$ sudo apt-get updateE: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用)E: 无法对目录 /var/l ...

  8. C++字符串指针与字符数组的区别

    今天发现这样一个问题 #include <iostream> using namespace std; int main() { ]; strcpy_s(ch1,");//编译通 ...

  9. python之路-模块安装 paramiko

    paramiko介绍(全是洋文,看不懂啊,赶紧有道翻译吧,等有朝一日,我去报个华尔街): "Paramiko" is a combination of the esperanto ...

  10. 文件读写IO

    摘要:本文主要总结了以下有关文件读写的IO,系统调用与库函数. 1.初级IO函数:close,creat,lseek,open,write 文件描述符是一个整型数 1.1close 1.2int cr ...