#include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int sum = 0; int it = 1; while(sum < n) { sum += __builtin_popcount(it++); } it--; if (sum == n) cout << it << "\n"; else cout << it - 1 << "\n"; for(int i = it; i > 0; i--) { if (__builtin_popcount(i) == sum - n) { sum = n; continue; } cout << 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 | #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int sum = 0; int it = 1; while(sum < n) { sum += __builtin_popcount(it++); } it--; if (sum == n) cout << it << "\n"; else cout << it - 1 << "\n"; for(int i = it; i > 0; i--) { if (__builtin_popcount(i) == sum - n) { sum = n; continue; } cout << i << " "; } } |