#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int total = 0;
int i = 1;
for (;!(!(i & (i + 1)) && total >= n); i++) {
int popcount = __builtin_popcount(i);
total += popcount;
}
vector<int> res;
--i;
for (; i > 0; i--) {
int popcount = __builtin_popcount(i);
if (total - popcount < n) res.push_back(i);
else total -= popcount;
}
int length = res.size();
cout << length << '\n';
for (i = 0; i < length; i++) {
cout << res[i] << ' ';
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
// #ifdef LOCAL
// int t; cin >> t; while (t--)
// #endif
solve();
cout.flush();
}
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 | #include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int total = 0; int i = 1; for (;!(!(i & (i + 1)) && total >= n); i++) { int popcount = __builtin_popcount(i); total += popcount; } vector<int> res; --i; for (; i > 0; i--) { int popcount = __builtin_popcount(i); if (total - popcount < n) res.push_back(i); else total -= popcount; } int length = res.size(); cout << length << '\n'; for (i = 0; i < length; i++) { cout << res[i] << ' '; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // #ifdef LOCAL // int t; cin >> t; while (t--) // #endif solve(); cout.flush(); } |
English