#include <iostream> #include <cstdio> int main() { int n; std::cin >> n; int c = 0; int j = 1; while (true) { c += __builtin_popcount(j); if (c >= n) break; j++; } if (c > n) { std::cout << j - 1 << std::endl; } else { std::cout << j << std::endl; } for (int k = j ; k >= 1 ; k--) { if (c - n == __builtin_popcount(k)) { c = n; } else { std::cout << k << " "; } } }
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> #include <cstdio> int main() { int n; std::cin >> n; int c = 0; int j = 1; while (true) { c += __builtin_popcount(j); if (c >= n) break; j++; } if (c > n) { std::cout << j - 1 << std::endl; } else { std::cout << j << std::endl; } for (int k = j ; k >= 1 ; k--) { if (c - n == __builtin_popcount(k)) { c = n; } else { std::cout << k << " "; } } } |