#include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; int f(int i) { return __builtin_popcount(i); } void solve() { int n; cin >> n; int i = 0; int sum = f(i); while (sum < n) { i++; sum += f(i); } vector<int> res; for (; i > 0; i--) { if (sum - f(i) >= n) { sum -= f(i); } else { res.push_back(i); } } cout << res.size() << "\n"; for (auto x : res) cout << x << " "; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); solve(); }
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 | #include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; int f(int i) { return __builtin_popcount(i); } void solve() { int n; cin >> n; int i = 0; int sum = f(i); while (sum < n) { i++; sum += f(i); } vector<int> res; for (; i > 0; i--) { if (sum - f(i) >= n) { sum -= f(i); } else { res.push_back(i); } } cout << res.size() << "\n"; for (auto x : res) cout << x << " "; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); solve(); } |