#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int last = 0; int bit_cnt = 0; while (bit_cnt < n) { ++last; bit_cnt += __builtin_popcount(last); } vi result; result.reserve(last); for (int i = last; i > 0; --i) { int cnt = __builtin_popcount(i); if (bit_cnt - cnt >= n) { bit_cnt -= cnt; } else { result.push_back(i); } } cout << result.size() << '\n'; for (size_t i = 0; i < result.size(); ++i) { cout << result[i] << ' '; } cout << '\n'; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int last = 0; int bit_cnt = 0; while (bit_cnt < n) { ++last; bit_cnt += __builtin_popcount(last); } vi result; result.reserve(last); for (int i = last; i > 0; --i) { int cnt = __builtin_popcount(i); if (bit_cnt - cnt >= n) { bit_cnt -= cnt; } else { result.push_back(i); } } cout << result.size() << '\n'; for (size_t i = 0; i < result.size(); ++i) { cout << result[i] << ' '; } cout << '\n'; return 0; } |