#include <bits/stdc++.h> using namespace std; int popcnt(int x) { int w = 0; while(x > 0) { w += x % 2; x /= 2; } return w; } int main() { int n; cin >> n; int s = 0; int l = 0; while(s <= n) { l ++; s += popcnt(l); } cout << l - 1 << '\n'; bool f = 0; while(l > 0) { if(s - popcnt(l) != n) cout << l << " "; else if(f) cout << l << " "; else f = 1; l--; } }
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; int popcnt(int x) { int w = 0; while(x > 0) { w += x % 2; x /= 2; } return w; } int main() { int n; cin >> n; int s = 0; int l = 0; while(s <= n) { l ++; s += popcnt(l); } cout << l - 1 << '\n'; bool f = 0; while(l > 0) { if(s - popcnt(l) != n) cout << l << " "; else if(f) cout << l << " "; else f = 1; l--; } } |