#include <iostream> #include <set> int bits(int n) { int sum = 0; while (n>0) {sum += n&1; n>>=1; } return sum; } int main() { std::ios_base::sync_with_stdio(0); int n; std::cin >> n; std::set<int, std::greater<int>> S; int i=1; while (n > 0) { S.insert(i); n -= bits(i); ++i; } while (n<0) { --i; if (n+bits(i) <= 0) { S.erase(i); n += bits(i); } } std::cout << S.size() << std::endl; for (int x : S) std::cout << " " << x; std::cout << std::endl; return 0; }
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 | #include <iostream> #include <set> int bits(int n) { int sum = 0; while (n>0) {sum += n&1; n>>=1; } return sum; } int main() { std::ios_base::sync_with_stdio(0); int n; std::cin >> n; std::set<int, std::greater<int>> S; int i=1; while (n > 0) { S.insert(i); n -= bits(i); ++i; } while (n<0) { --i; if (n+bits(i) <= 0) { S.erase(i); n += bits(i); } } std::cout << S.size() << std::endl; for (int x : S) std::cout << " " << x; std::cout << std::endl; return 0; } |