#include <iostream> #include <set> #define ASZ(array_) ((int)(sizeof(array_) / sizeof((array_)[0]))) #define GETV(value_) (std::cin >> (value_)) #define FORIR(container_, vname_) for (auto vname_ = (container_).rbegin(); vname_ != (container_).rend(); ++vname_) #define DECL_GETT(type_, name_) \ type_ name_; \ GETV(name_) #define DECL_GET(name_) DECL_GETT(int, name_) #define PUTN(value_) std::cout << (value_) #define PUT(value_) PUTN(value_) << "\n" namespace alg { namespace bit { template <typename T> int count(T t) { int r = 0; while (t) { r += (t & 1); t >>= 1; } return r; } } } int main(void) { DECL_GET(n); std::set<int> sets[20]; std::set<int> result; int v = 0; while (n > 0) { int bits_count = alg::bit::count(++v); n -= bits_count; while (bits_count <= ASZ(sets)) { sets[bits_count++ - 1].insert(v); } result.insert(v); } n = -n; while (n > 0) { int i = n; v = *sets[--i].rbegin(); result.erase(v); n -= alg::bit::count(v); do { v = sets[i].erase(v); } while (i--); } PUT(result.size()); char const *delim = ""; FORIR(result, e) { PUTN(delim) << *e; delim = " "; } PUT(""); }
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 | #include <iostream> #include <set> #define ASZ(array_) ((int)(sizeof(array_) / sizeof((array_)[0]))) #define GETV(value_) (std::cin >> (value_)) #define FORIR(container_, vname_) for (auto vname_ = (container_).rbegin(); vname_ != (container_).rend(); ++vname_) #define DECL_GETT(type_, name_) \ type_ name_; \ GETV(name_) #define DECL_GET(name_) DECL_GETT(int, name_) #define PUTN(value_) std::cout << (value_) #define PUT(value_) PUTN(value_) << "\n" namespace alg { namespace bit { template <typename T> int count(T t) { int r = 0; while (t) { r += (t & 1); t >>= 1; } return r; } } } int main(void) { DECL_GET(n); std::set<int> sets[20]; std::set<int> result; int v = 0; while (n > 0) { int bits_count = alg::bit::count(++v); n -= bits_count; while (bits_count <= ASZ(sets)) { sets[bits_count++ - 1].insert(v); } result.insert(v); } n = -n; while (n > 0) { int i = n; v = *sets[--i].rbegin(); result.erase(v); n -= alg::bit::count(v); do { v = sets[i].erase(v); } while (i--); } PUT(result.size()); char const *delim = ""; FORIR(result, e) { PUTN(delim) << *e; delim = " "; } PUT(""); } |