#include <iostream> #include <vector> using namespace std; int main() { vector<int> bit_num; bit_num.resize(1<<19+1); bit_num[0] = 0; for (int left = 1; left < 1<<19; left<<=1) { for (int i = left; i < left<<1; ++i) { bit_num[i] = bit_num[i - left] + 1; } } int bit_sum; cin >> bit_sum; int largest = 0; while (bit_sum > 0) { bit_sum-=bit_num[++largest]; } vector<int> exclusions; int current = largest; bit_sum = -bit_sum; while (bit_sum > 0) { if (bit_num[current] <= bit_sum) { exclusions.push_back(current); bit_sum -= bit_num[current]; } --current; } cout << largest - exclusions.size() << endl; int excl_ind = 0; for (int i = largest; i >= 1; --i) { if (excl_ind == exclusions.size() || i != exclusions[excl_ind]) { cout << i << " "; } else { ++excl_ind; } } 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 | #include <iostream> #include <vector> using namespace std; int main() { vector<int> bit_num; bit_num.resize(1<<19+1); bit_num[0] = 0; for (int left = 1; left < 1<<19; left<<=1) { for (int i = left; i < left<<1; ++i) { bit_num[i] = bit_num[i - left] + 1; } } int bit_sum; cin >> bit_sum; int largest = 0; while (bit_sum > 0) { bit_sum-=bit_num[++largest]; } vector<int> exclusions; int current = largest; bit_sum = -bit_sum; while (bit_sum > 0) { if (bit_num[current] <= bit_sum) { exclusions.push_back(current); bit_sum -= bit_num[current]; } --current; } cout << largest - exclusions.size() << endl; int excl_ind = 0; for (int i = largest; i >= 1; --i) { if (excl_ind == exclusions.size() || i != exclusions[excl_ind]) { cout << i << " "; } else { ++excl_ind; } } cout << endl; return 0; } |