#include <bits/stdc++.h> using namespace std; const int N = 1e6; int prefbitsum[N]; void preprocess() { for (int i = 1; i < N; i++) { prefbitsum[i] = prefbitsum[i - 1] + __builtin_popcount(i); } } void solve(int n) { preprocess(); vector<int> answer; for (int i = N - 1; i > 0 && n > 0; i--) { if (prefbitsum[i - 1] < n) { answer.push_back(i); n -= __builtin_popcount(i); } } cout << answer.size() << endl; for (int x : answer) { cout << x << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; solve(n); }
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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6; int prefbitsum[N]; void preprocess() { for (int i = 1; i < N; i++) { prefbitsum[i] = prefbitsum[i - 1] + __builtin_popcount(i); } } void solve(int n) { preprocess(); vector<int> answer; for (int i = N - 1; i > 0 && n > 0; i--) { if (prefbitsum[i - 1] < n) { answer.push_back(i); n -= __builtin_popcount(i); } } cout << answer.size() << endl; for (int x : answer) { cout << x << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; solve(n); } |