转载请注明出处: 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 ;
}

最新文章

  1. android嵌套unity3d
  2. Intro.js 网站演示
  3. iOS开发——高级篇——内存分析,Instruments
  4. POJ 3180-The Cow Prom (图论-有向图强联通tarjan算法)
  5. CentOS 7.2 搭建 Ghost 博客
  6. 使用OPC的方式去连接PLC进行AB SLC-5_04数据的采集
  7. Cassandra1.2文档学习(19)—— CQL索引
  8. Struts,Spring,Hibernate的作用
  9. input text 字体的影响
  10. SQL从入门到基础 - 06 限制结果集范围
  11. 调用CachedRowSetImpl类时,为什么会出现这样的错误
  12. gulp实用配置(2)——中小项目
  13. 模型的继承 -- Django从入门到精通系列教程
  14. CLOUD清理临时表空间
  15. DDD关键知识点整理汇总
  16. Windows下文件夹扩展名
  17. mysql各数据类型的存储范围
  18. day 09 初识函数
  19. 《转》Python学习(19)-python函数(二)-关于lambda
  20. PTA第三次上机

热门文章

  1. coconHashMap实现原理分析
  2. No2_5.类的高级特性_Java学习笔记_抽象类和成员内部类
  3. CSS的权重(转)
  4. sudo 无法解析主机的解决办法
  5. LoadRunner参数化功能详解
  6. coredata中谓词的使用
  7. [TYVJ] P1023 奶牛的锻炼
  8. C语言---类型转换
  9. 程序减肥,strip,eu-strip 及其符号表
  10. Axure 原型设计工具画业务流程图