#include<iostream> #include<vector> using namespace std; int bits(int n) { int res = 0; while (n > 0) { if (n & 1 == 1) ++res; n = n >> 1; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k = 0; cin >> n; vector<int> v, r; for (int i = 1;; i++) { k += bits(i); v.push_back(i); if (k >= n) break; } for (int i = v.size() - 1; i >= 0; i--) { int b = bits(v[i]); if (k - b >= n) { k -= b; } else { r.push_back(v[i]); } } cout << r.size() << endl; for (int i = 0; i < r.size(); i++) { cout << r[i] << ' '; } cout << endl; 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 | #include<iostream> #include<vector> using namespace std; int bits(int n) { int res = 0; while (n > 0) { if (n & 1 == 1) ++res; n = n >> 1; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k = 0; cin >> n; vector<int> v, r; for (int i = 1;; i++) { k += bits(i); v.push_back(i); if (k >= n) break; } for (int i = v.size() - 1; i >= 0; i--) { int b = bits(v[i]); if (k - b >= n) { k -= b; } else { r.push_back(v[i]); } } cout << r.size() << endl; for (int i = 0; i < r.size(); i++) { cout << r[i] << ' '; } cout << endl; return 0; } |