#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int s = 0;
int i = 0;
vector<int> a;
while (s < n) {
i++;
s += __builtin_popcount(i);
a.push_back(i);
}
i = a.size()-1;
vector<int> bad(a.size()+1);
int ans = a.size();
while (s > n) {
if (s - __builtin_popcount(a[i]) >= n) {
bad[i] = true;
ans--;
s -= __builtin_popcount(a[i]);
}
i--;
}
cout << ans << '\n';
for (int i = a.size()-1; i >= 0; i--) {
if (!bad[i]) cout << a[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 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int s = 0; int i = 0; vector<int> a; while (s < n) { i++; s += __builtin_popcount(i); a.push_back(i); } i = a.size()-1; vector<int> bad(a.size()+1); int ans = a.size(); while (s > n) { if (s - __builtin_popcount(a[i]) >= n) { bad[i] = true; ans--; s -= __builtin_popcount(a[i]); } i--; } cout << ans << '\n'; for (int i = a.size()-1; i >= 0; i--) { if (!bad[i]) cout << a[i] << ' '; } } |
English