#include <vector> #include <iostream> int countBits(int x) { int count = 0; while (x) { count += x & 1; x >>= 1; } return count; } int main() { int n; std::cin >> n; int currentSum = 0; for (int i = 1; i <= 1'000'000; ++i) { currentSum += countBits(i); } std::vector<int> out; for (int i = 1'000'000; i > 0; --i) { if ((currentSum - countBits(i)) >= n) { currentSum -= countBits(i); continue; } out.push_back(i); currentSum -= countBits(i); n -= countBits(i); } std::cout << out.size() << std::endl; for (auto i : out) std::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 27 28 29 30 31 32 33 34 | #include <vector> #include <iostream> int countBits(int x) { int count = 0; while (x) { count += x & 1; x >>= 1; } return count; } int main() { int n; std::cin >> n; int currentSum = 0; for (int i = 1; i <= 1'000'000; ++i) { currentSum += countBits(i); } std::vector<int> out; for (int i = 1'000'000; i > 0; --i) { if ((currentSum - countBits(i)) >= n) { currentSum -= countBits(i); continue; } out.push_back(i); currentSum -= countBits(i); n -= countBits(i); } std::cout << out.size() << std::endl; for (auto i : out) std::cout << i << " "; } |