#include <iostream> #include <vector> int ones(int x) { int res = 0; while (x) { res += x % 2; x >>= 1; } return res; } int ones_up_to(int x) { x += 1; int res = 0; int mul = 1; while (mul < x) { res += int(x / (mul * 2)) * mul + std::max(0, x % (mul * 2) - mul); mul *= 2; } return res; } int find(int x) { int a = 0; int b = 120210; int p, out; while (a < b) { p = (a + b) / 2; out = ones_up_to(p); if (out < x) a = p + 1; else if (x < out) b = p; else return p; } return a; } int main() { int n; int x; std::vector<int> res; std::cin >> n; while (n) { x = find(n); res.push_back(x); n -= ones(x); } std::cout << res.size() << '\n'; for (auto i = res.begin(); i < res.end(); i++) std::cout << *i << ' '; 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <iostream> #include <vector> int ones(int x) { int res = 0; while (x) { res += x % 2; x >>= 1; } return res; } int ones_up_to(int x) { x += 1; int res = 0; int mul = 1; while (mul < x) { res += int(x / (mul * 2)) * mul + std::max(0, x % (mul * 2) - mul); mul *= 2; } return res; } int find(int x) { int a = 0; int b = 120210; int p, out; while (a < b) { p = (a + b) / 2; out = ones_up_to(p); if (out < x) a = p + 1; else if (x < out) b = p; else return p; } return a; } int main() { int n; int x; std::vector<int> res; std::cin >> n; while (n) { x = find(n); res.push_back(x); n -= ones(x); } std::cout << res.size() << '\n'; for (auto i = res.begin(); i < res.end(); i++) std::cout << *i << ' '; return 0; } |