#include <bits/stdc++.h> using namespace std; const int MAXN = 1'000'000; vector <int> solve(int n) { vector <int> numBits = {0}, numBitsTotal = {0}; for (int x = 1; numBitsTotal.back() < MAXN; x++) { int b = __builtin_popcount(x); numBits.push_back(b); numBitsTotal.push_back(numBitsTotal.back() + b); } vector <int> ans; for (int x = (int) numBits.size() - 1; x >= 1; x--) { if (numBitsTotal[x - 1] < n) { ans.push_back(x); n -= numBits[x]; } } return ans; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; auto ans = solve(n); cout << ans.size() << '\n'; for (int x : ans) { 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 <bits/stdc++.h> using namespace std; const int MAXN = 1'000'000; vector <int> solve(int n) { vector <int> numBits = {0}, numBitsTotal = {0}; for (int x = 1; numBitsTotal.back() < MAXN; x++) { int b = __builtin_popcount(x); numBits.push_back(b); numBitsTotal.push_back(numBitsTotal.back() + b); } vector <int> ans; for (int x = (int) numBits.size() - 1; x >= 1; x--) { if (numBitsTotal[x - 1] < n) { ans.push_back(x); n -= numBits[x]; } } return ans; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; auto ans = solve(n); cout << ans.size() << '\n'; for (int x : ans) { cout << x << ' '; } return 0; } |