#include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); ll n; cin >> n; ll a = 1; vector<ll> result; while (n > 0) { ll ones = __builtin_popcount(a); if (n - ones >= 0) { n -= ones; result.pb(a); } else { ll over = ones - n; ll i = result.size() - 1; result.pb(a); while (over > 0) { ll pom = __builtin_popcount(result[i]); if (over - pom >= 0) { over -= pom; result.erase(result.begin() + i); } i--; } n = 0; } a++; } reverse(result.begin(), result.end()); cout << result.size() << endl; for (int i = 0; i < result.size(); ++i) { cout << result[i] << ' '; } }
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 | #include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); ll n; cin >> n; ll a = 1; vector<ll> result; while (n > 0) { ll ones = __builtin_popcount(a); if (n - ones >= 0) { n -= ones; result.pb(a); } else { ll over = ones - n; ll i = result.size() - 1; result.pb(a); while (over > 0) { ll pom = __builtin_popcount(result[i]); if (over - pom >= 0) { over -= pom; result.erase(result.begin() + i); } i--; } n = 0; } a++; } reverse(result.begin(), result.end()); cout << result.size() << endl; for (int i = 0; i < result.size(); ++i) { cout << result[i] << ' '; } } |