Reprint Please specify source: 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, the beauty of the numbers he wrote out was Maximum.
TheBeauty of the written out numbersb1,b2,...,bkIs such maximum non-negative integerv, that numberb1 andb2 and... and b K is divisible by Number 2 v without a remainder. If such number v doesn ' t exist (that's, for any non-negative integer < Span class= "Tex-span" > v , Number b 1 and b 2 and ... and < Span class= "Tex-span" > b k is divisible by 2 v without a remainder), the beauty of the written out Numbers equals-1.
Tell the bear which numbers he should write off so, the beauty of the written out numbers is maximum. If There is multiple ways to write out of 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 are 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, ..., a n (1≤ a1 < a2 < ... < an ≤ 9).
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 is allowed to print numbers b1, b2, ..., bK in any or Der, but all of them must is distinct. If There is multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still is multiple ways, you is 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
Test instructions
Give the number of n so that you can select as many numbers as possible so that it is divisible by as much as 2 of the power.
Analysis:
Since the given number is within the range of 10 9, it is possible to enumerate the power of the 2, from high to low. Because the number of requests to take as large as possible, so each time the first one is 1 of the number is taken out, and then the sum of these numbers, if it can be divisible by the power of the 2, then satisfied, otherwise, continue to take a small power.
1 //#####################2 //Author:fraud3 //Blog:http://www.cnblogs.com/fraud/4 //#####################5#include <iostream>6#include <sstream>7#include <ios>8#include <iomanip>9#include <functional>Ten#include <algorithm> One#include <vector> A#include <string> -#include <list> -#include <queue> the#include <deque> -#include <stack> -#include <Set> -#include <map> +#include <cstdio> -#include <cstdlib> +#include <cmath> A#include <cstring> at#include <climits> -#include <cctype> - using namespacestd; - #defineXinf Int_max - #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) in #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) to #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) + #defineDEP (x,r,l) for (int x=r; x>=l; x--) - #defineCLR (a,x) memset (a,x,sizeof (A)) the #defineIT iterator *typedefLong Longll; $typedef pair<int,int>PII;Panax Notoginsengtypedef vector<pii>VII; -typedef vector<int>VI; the inta[100010]; + intb[100010]; A intMain () the { +Ios::sync_with_stdio (false); - intN; $Cin>>N; $ for(intI=1; i<=n;i++) cin>>A[i]; -Sort (A +1, A +1+n); - inti; the inttot=0; - intt= -;Wuyi while(t>=0){ the inttmp=1<<T; -tot=0; Wu for(intI=1; i<=n;i++){ - if((tmp&a[i)) b[tot++]=A[i]; About } $ if(!tot) { -t--; - Continue; - } A inttemp=b[0]; + for(intI=1; i<tot;i++){ thetemp=temp&B[i]; - } $ if(temp%tmp==0){ thecout<<tot<<Endl; the for(intI=0; i<tot;i++){ the if(i) cout<<" "; thecout<<B[i]; - } incout<<Endl; the Break; the } Aboutt--; the } the return 0; the}View Code
Codeforces 336C Vasily the Bear and Sequence (greedy)