#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> res; constexpr int N = 1000001; vector<int> pref(N, 0); for (int i = 1; i < N; i++) pref[i] = __builtin_popcount(i) + pref[i - 1]; for (int i = 1000000; n > 0 && i > 1; i--) { if (pref[i - 1] < n) { res.emplace_back(i); n -= __builtin_popcount(i); } } if (n == 1) res.emplace_back(1); cout << res.size() << '\n'; for (auto &x : res) cout << x << ' '; }
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 | #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> res; constexpr int N = 1000001; vector<int> pref(N, 0); for (int i = 1; i < N; i++) pref[i] = __builtin_popcount(i) + pref[i - 1]; for (int i = 1000000; n > 0 && i > 1; i--) { if (pref[i - 1] < n) { res.emplace_back(i); n -= __builtin_popcount(i); } } if (n == 1) res.emplace_back(1); cout << res.size() << '\n'; for (auto &x : res) cout << x << ' '; } |