#include <iostream> #include <vector> int bit_sum(int i) { int r = 0; while (i != 0) { r += i % 2; i /= 2; } return r; } int main() { std::ios_base::sync_with_stdio(0); int n; std::cin >> n; std::vector<int> prefix_bit_sum = {0}; int num = 1; while (true) { prefix_bit_sum.push_back(prefix_bit_sum.back() + bit_sum(num)); if (prefix_bit_sum.back() > n) break; ++num; } std::vector<int> res; while (n > 0) { if (prefix_bit_sum[num - 1] < n) { n -= bit_sum(num); res.push_back(num); } --num; } std::cout << res.size() << "\n"; for (int x : res) { std::cout << x << " "; } 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 | #include <iostream> #include <vector> int bit_sum(int i) { int r = 0; while (i != 0) { r += i % 2; i /= 2; } return r; } int main() { std::ios_base::sync_with_stdio(0); int n; std::cin >> n; std::vector<int> prefix_bit_sum = {0}; int num = 1; while (true) { prefix_bit_sum.push_back(prefix_bit_sum.back() + bit_sum(num)); if (prefix_bit_sum.back() > n) break; ++num; } std::vector<int> res; while (n > 0) { if (prefix_bit_sum[num - 1] < n) { n -= bit_sum(num); res.push_back(num); } --num; } std::cout << res.size() << "\n"; for (int x : res) { std::cout << x << " "; } return 0; } |