#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> inv_res; int moc = 0; for (int i = 1; moc < n; ++i) { inv_res.push_back(i); moc += __builtin_popcount(i); // cout << "popc: " << __builtin_popcount(i) << '\n'; } if (moc > n) { // cout << inv_res.back() << '\n'; moc -= __builtin_popcount(inv_res.back()); inv_res.pop_back(); } for (int i = inv_res.size() - 1; moc != n; --i) { moc += __builtin_popcount(inv_res[i] + 1) - __builtin_popcount(inv_res[i]); ++inv_res[i]; } cout << inv_res.size() << '\n'; for (int i = inv_res.size() - 1; i >= 0; --i) { cout << inv_res[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 | #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> inv_res; int moc = 0; for (int i = 1; moc < n; ++i) { inv_res.push_back(i); moc += __builtin_popcount(i); // cout << "popc: " << __builtin_popcount(i) << '\n'; } if (moc > n) { // cout << inv_res.back() << '\n'; moc -= __builtin_popcount(inv_res.back()); inv_res.pop_back(); } for (int i = inv_res.size() - 1; moc != n; --i) { moc += __builtin_popcount(inv_res[i] + 1) - __builtin_popcount(inv_res[i]); ++inv_res[i]; } cout << inv_res.size() << '\n'; for (int i = inv_res.size() - 1; i >= 0; --i) { cout << inv_res[i] << ' '; } cout << '\n'; return 0; } |