codeforces 336C Vasily the Bear and Sequence(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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 b1 and b2 and ... 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 b1 and 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".
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).
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.
5
1 2 3 4 5
2
4 5
3
1 2 4
1
4
题意:
给出n数,让你从中挑出尽可能多的数使得其能够被尽可能大的2的次幂整除。
分析:
由于给出的数的范围是在10的9次方之内的,所以可以枚举2的次幂数,由高到低枚举。因为要求取的数尽可能大,所以每次都先把这一位上是1的数都取出来,然后这些数的相与之后,若能够被2的这个次幂整除,则满足,否则,继续取小的次幂。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }