#include <iostream> using namespace std; int numberOfBits(unsigned int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0F0F0F0F; return (i * 0x01010101) >> 24; } int main() { unsigned int n; cin >> n; unsigned int max_of_count[33] = {0}; unsigned int total_count = 0; int num = 1; while (total_count < n) { unsigned int bits = numberOfBits(num); total_count += bits; if (num > max_of_count[bits]) { max_of_count[bits] = num; } ++num; } --num; if (total_count == n) { cout << num << endl; for (int i = num; i > 0; --i) cout << i << " "; cout << endl; return 0; } cout << (num - 1) << endl; for (int i=num;i>0;--i) { if (i != max_of_count[total_count - n]) cout << i << " "; } cout << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <iostream> using namespace std; int numberOfBits(unsigned int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0F0F0F0F; return (i * 0x01010101) >> 24; } int main() { unsigned int n; cin >> n; unsigned int max_of_count[33] = {0}; unsigned int total_count = 0; int num = 1; while (total_count < n) { unsigned int bits = numberOfBits(num); total_count += bits; if (num > max_of_count[bits]) { max_of_count[bits] = num; } ++num; } --num; if (total_count == n) { cout << num << endl; for (int i = num; i > 0; --i) cout << i << " "; cout << endl; return 0; } cout << (num - 1) << endl; for (int i=num;i>0;--i) { if (i != max_of_count[total_count - n]) cout << i << " "; } cout << endl; return 0; } |