#include <bits/stdc++.h> constexpr int LIMIT = 120'225; int BIT_SUM[LIMIT]; int PREFIX_SUM[LIMIT]; std::vector<int> OUTPUT; void make_bit() { int sum = 0; for (int i = 1; i < LIMIT; ++i) { int temp = i; while (temp) { if (temp & 1) ++BIT_SUM[i]; temp >>= 1; } sum += BIT_SUM[i]; PREFIX_SUM[i] = sum; } } int main() { make_bit(); int n; std::cin >> n; int x = 0; while (PREFIX_SUM[x] < n) ++x; while (n > 0) { if (n > PREFIX_SUM[x - 1]) { n -= BIT_SUM[x]; OUTPUT.push_back(x); } --x; } std::cout << OUTPUT.size() << '\n'; for (auto& i : OUTPUT) std::cout << i << ' '; }
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 | #include <bits/stdc++.h> constexpr int LIMIT = 120'225; int BIT_SUM[LIMIT]; int PREFIX_SUM[LIMIT]; std::vector<int> OUTPUT; void make_bit() { int sum = 0; for (int i = 1; i < LIMIT; ++i) { int temp = i; while (temp) { if (temp & 1) ++BIT_SUM[i]; temp >>= 1; } sum += BIT_SUM[i]; PREFIX_SUM[i] = sum; } } int main() { make_bit(); int n; std::cin >> n; int x = 0; while (PREFIX_SUM[x] < n) ++x; while (n > 0) { if (n > PREFIX_SUM[x - 1]) { n -= BIT_SUM[x]; OUTPUT.push_back(x); } --x; } std::cout << OUTPUT.size() << '\n'; for (auto& i : OUTPUT) std::cout << i << ' '; } |