#include <iostream> #include <vector> using namespace std; typedef long long ll; int const N = 1e6 + 9; ll ile[N]; int n; vector<int> ans; void solve() { cin >> n; ll sum = 0; ile[1] = 1; ile[2] = 1; for (int i = 1; i < N; i++) { //szukamy maksymalnego 2^k int k = 1; while (k <= i) k <<= 1; k >>= 1; if (k == i) ile[i] = 1; else ile[i] = ile[k] + ile[i - k]; sum += ile[i]; ll mozemy = (ll)i * ((ll)i + 1); mozemy /= 2; if (sum >= n && (sum - n) <= mozemy) { for (int j = i; j >= 1; j--) { if ((sum - n) >= ile[j]) sum -= ile[j]; else ans.push_back(j); } break; } } cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.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 48 49 50 51 52 53 54 55 | #include <iostream> #include <vector> using namespace std; typedef long long ll; int const N = 1e6 + 9; ll ile[N]; int n; vector<int> ans; void solve() { cin >> n; ll sum = 0; ile[1] = 1; ile[2] = 1; for (int i = 1; i < N; i++) { //szukamy maksymalnego 2^k int k = 1; while (k <= i) k <<= 1; k >>= 1; if (k == i) ile[i] = 1; else ile[i] = ile[k] + ile[i - k]; sum += ile[i]; ll mozemy = (ll)i * ((ll)i + 1); mozemy /= 2; if (sum >= n && (sum - n) <= mozemy) { for (int j = i; j >= 1; j--) { if ((sum - n) >= ile[j]) sum -= ile[j]; else ans.push_back(j); } break; } } cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); } |