#include <iostream> #include <unordered_set> #include <queue> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> #include <assert.h> #include <tuple> using namespace std; using ull = unsigned long long; using ll = long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif #define MAX (1000000000) // #define MAX (20) int tab[1000010]; std::vector<int> how_many_bits_in_1_to_x; std::vector<int> results; int main() { int n; cin >> n; // how many bits in 1..N ? { how_many_bits_in_1_to_x.push_back(0); how_many_bits_in_1_to_x.push_back(1); int i = 2; int last = 1; while (true) { last = last + __builtin_popcount(i); how_many_bits_in_1_to_x.push_back(last); // printf("%d: %d\n", i, last); if (last > 1000000) { break; } i++; } } // x(1) -> c(1) // x(2) -> x(1) + c(2) // x(3) -> x(2) + c(3) while (n > 0) { // find largest number which HAS to be in solution auto it = std::lower_bound(how_many_bits_in_1_to_x.begin(), how_many_bits_in_1_to_x.end(), n); auto item = static_cast<int>(it - how_many_bits_in_1_to_x.begin()); // printf("result: [%d] %d\n", item, *it); results.push_back(item); n -= __builtin_popcount(item); assert(n >= 0); } printf("%d\n", results.size()); for(auto x: results) { printf("%d ", x); } printf("\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 | #include <iostream> #include <unordered_set> #include <queue> #include <cassert> #include <algorithm> #include <vector> #include <set> #include <map> #include <numeric> #include <assert.h> #include <tuple> using namespace std; using ull = unsigned long long; using ll = long long; #ifdef GGDEBUG #define dbg printf #else #define dbg //dbg #endif #define MAX (1000000000) // #define MAX (20) int tab[1000010]; std::vector<int> how_many_bits_in_1_to_x; std::vector<int> results; int main() { int n; cin >> n; // how many bits in 1..N ? { how_many_bits_in_1_to_x.push_back(0); how_many_bits_in_1_to_x.push_back(1); int i = 2; int last = 1; while (true) { last = last + __builtin_popcount(i); how_many_bits_in_1_to_x.push_back(last); // printf("%d: %d\n", i, last); if (last > 1000000) { break; } i++; } } // x(1) -> c(1) // x(2) -> x(1) + c(2) // x(3) -> x(2) + c(3) while (n > 0) { // find largest number which HAS to be in solution auto it = std::lower_bound(how_many_bits_in_1_to_x.begin(), how_many_bits_in_1_to_x.end(), n); auto item = static_cast<int>(it - how_many_bits_in_1_to_x.begin()); // printf("result: [%d] %d\n", item, *it); results.push_back(item); n -= __builtin_popcount(item); assert(n >= 0); } printf("%d\n", results.size()); for(auto x: results) { printf("%d ", x); } printf("\n"); return 0; } |