#include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int64_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::vector<int> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::unordered_set<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } int count_bits(unsigned int x) { int count = 0; while(x) { count += x & 1; x >>= 1; } return count; } int main() { prelude(); int n = 0; std::cin >> n; int s = 0; std::vector<unsigned int> nums; unsigned int i = 1; while(s < n) { s += count_bits(i); nums.push_back(i); ++i; } int idx = nums.size()-2; int cut = s-n; while(cut > 0) { if(count_bits(nums[idx]) <= cut) { cut -= count_bits(nums[idx]); nums.erase(nums.begin() + idx); } idx--; } std::reverse(nums.begin(), nums.end()); std::cout << nums.size() << "\n"; for(int i = 0; i < nums.size()-1; ++i) { std::cout << nums[i] << " "; } std::cout << nums[nums.size()-1] << "\n"; 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <algorithm> #include <cstdint> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int64_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::vector<int> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::unordered_set<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } int count_bits(unsigned int x) { int count = 0; while(x) { count += x & 1; x >>= 1; } return count; } int main() { prelude(); int n = 0; std::cin >> n; int s = 0; std::vector<unsigned int> nums; unsigned int i = 1; while(s < n) { s += count_bits(i); nums.push_back(i); ++i; } int idx = nums.size()-2; int cut = s-n; while(cut > 0) { if(count_bits(nums[idx]) <= cut) { cut -= count_bits(nums[idx]); nums.erase(nums.begin() + idx); } idx--; } std::reverse(nums.begin(), nums.end()); std::cout << nums.size() << "\n"; for(int i = 0; i < nums.size()-1; ++i) { std::cout << nums[i] << " "; } std::cout << nums[nums.size()-1] << "\n"; return 0; } |