#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; const int MAX_N = 1e6; unsigned bits[MAX_N]; const int NONE = -1; int bitcount(uint32_t i) { // Java: use int, and use >>> instead of >>. Or use Integer.bitCount() // C or C++: use uint32_t i = i - ((i >> 1) & 0x55555555); // add pairs of bits i = (i & 0x33333333) + ((i >> 2) & 0x33333333); // quads i = (i + (i >> 4)) & 0x0F0F0F0F; // groups of 8 return (i * 0x01010101) >> 24; // horizontal sum of bytes } void solve(int n) { vector<int> a; int bs = 0; int cur = 1; while (bs < n) { bits[cur] = bitcount(cur); bs += bits[cur]; a.push_back(cur); cur += 1; } int i = a.size() - 1; int out_size = a.size(); for (int i = a.size()-1; i >= 0 && bs > n; --i) { if (bs - bits[a[i]] >= n) { bs -= bits[a[i]]; a[i] = NONE; out_size -= 1; } } assert(bs == n); printf("%d\n", out_size); for (auto it = a.rbegin(); it != a.rend(); ++it) { if (*it != NONE) { printf("%d ", *it); } } printf("\n"); } int main() { int n; scanf("%d", &n); solve(n); /* for (int k = 1; k < 1e6; ++k) { solve(k); } */ 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 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> using namespace std; #define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b)) using ll = long long; const int INF = 1e9; const int MAX_N = 1e6; unsigned bits[MAX_N]; const int NONE = -1; int bitcount(uint32_t i) { // Java: use int, and use >>> instead of >>. Or use Integer.bitCount() // C or C++: use uint32_t i = i - ((i >> 1) & 0x55555555); // add pairs of bits i = (i & 0x33333333) + ((i >> 2) & 0x33333333); // quads i = (i + (i >> 4)) & 0x0F0F0F0F; // groups of 8 return (i * 0x01010101) >> 24; // horizontal sum of bytes } void solve(int n) { vector<int> a; int bs = 0; int cur = 1; while (bs < n) { bits[cur] = bitcount(cur); bs += bits[cur]; a.push_back(cur); cur += 1; } int i = a.size() - 1; int out_size = a.size(); for (int i = a.size()-1; i >= 0 && bs > n; --i) { if (bs - bits[a[i]] >= n) { bs -= bits[a[i]]; a[i] = NONE; out_size -= 1; } } assert(bs == n); printf("%d\n", out_size); for (auto it = a.rbegin(); it != a.rend(); ++it) { if (*it != NONE) { printf("%d ", *it); } } printf("\n"); } int main() { int n; scanf("%d", &n); solve(n); /* for (int k = 1; k < 1e6; ++k) { solve(k); } */ return 0; } |