#include <iostream> using namespace std; int main() { int n; cin >> n; int popcount = 0; int next = 1; while (popcount < n) { popcount += __builtin_popcount(next); next += 1; } int ban = -1; if (popcount > n) { for (int i = next - 1; i > 0; --i) { if (ban == -1 && __builtin_popcount(i) == popcount - n) { ban = i; } } } cout << (next - 1 - (popcount != n)) << "\n"; for (int i=next - 1; i > 0; --i) { if (ban == i) 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 26 | #include <iostream> using namespace std; int main() { int n; cin >> n; int popcount = 0; int next = 1; while (popcount < n) { popcount += __builtin_popcount(next); next += 1; } int ban = -1; if (popcount > n) { for (int i = next - 1; i > 0; --i) { if (ban == -1 && __builtin_popcount(i) == popcount - n) { ban = i; } } } cout << (next - 1 - (popcount != n)) << "\n"; for (int i=next - 1; i > 0; --i) { if (ban == i) continue; cout << i << ' '; } } |