#include <algorithm> #include <iostream> #include <vector> using namespace std; using PII = pair<int, int>; int bit_count(int number) { int cnt = 0; while (number) { cnt += number & 1; number >>= 1; } return cnt; } int main() { int n; cin >> n; int cnt = 0; vector<PII> pos; for (int i = 1; cnt < n; ++i) { int bc = bit_count(i); pos.push_back(make_pair(i, bc)); cnt += bc; } vector<int> res; for (int i = pos.size() - 1; i >= 0; --i) { if (pos[i].second <= cnt - n) { cnt -= pos[i].second; } else { res.push_back(pos[i].first); } } cout << res.size() << endl; for (int i = 0; i < res.size(); ++i) { cout << (i == 0 ? "" : " ") << res[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 | #include <algorithm> #include <iostream> #include <vector> using namespace std; using PII = pair<int, int>; int bit_count(int number) { int cnt = 0; while (number) { cnt += number & 1; number >>= 1; } return cnt; } int main() { int n; cin >> n; int cnt = 0; vector<PII> pos; for (int i = 1; cnt < n; ++i) { int bc = bit_count(i); pos.push_back(make_pair(i, bc)); cnt += bc; } vector<int> res; for (int i = pos.size() - 1; i >= 0; --i) { if (pos[i].second <= cnt - n) { cnt -= pos[i].second; } else { res.push_back(pos[i].first); } } cout << res.size() << endl; for (int i = 0; i < res.size(); ++i) { cout << (i == 0 ? "" : " ") << res[i]; } return 0; } |