#include <bits/stdc++.h> using namespace std; int n; vector<int> res; vector<int> v; int main() { scanf("%d", &n); int bitCnt = 0; int k = 1; while (bitCnt < n) { bitCnt += __builtin_popcount(k); v.push_back(k); ++k; } for (int i = v.size() - 1; i >= 0; --i) { if (bitCnt - __builtin_popcount(v[i]) >= n) { bitCnt -= __builtin_popcount(v[i]); continue; } else { res.push_back(v[i]); } } printf("%d\n", res.size()); for (auto x : res) { printf("%d ", x); } }
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 | #include <bits/stdc++.h> using namespace std; int n; vector<int> res; vector<int> v; int main() { scanf("%d", &n); int bitCnt = 0; int k = 1; while (bitCnt < n) { bitCnt += __builtin_popcount(k); v.push_back(k); ++k; } for (int i = v.size() - 1; i >= 0; --i) { if (bitCnt - __builtin_popcount(v[i]) >= n) { bitCnt -= __builtin_popcount(v[i]); continue; } else { res.push_back(v[i]); } } printf("%d\n", res.size()); for (auto x : res) { printf("%d ", x); } } |