#include <iostream> #include <vector> using namespace std; int bin_search(vector<int>& tab, int b,int e,int x) { if(b==e) { return b; } int mid = (b+e)/2; if(tab[mid] == x) { return mid; } else if(tab[mid] > x) { return bin_search(tab,b,mid,x); } else { return bin_search(tab,mid+1,e,x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int>bit; vector<int>bit_sum; bit.push_back(0); bit_sum.push_back(0); int it = 1; while (bit_sum.back() < n) { bit.push_back(bit[it/2] + it%2); bit_sum.push_back(bit_sum.back()+bit.back()); it++; } vector<int> result; int end = bit_sum.size(); while(n > 0) { int b_re = bin_search(bit_sum,0,end,n); n -= bit[b_re]; result.push_back(b_re); end = b_re-1; } cout << result.size() << '\n'; for (int i = 0; i < result.size(); i++) { cout << result[i] << ' '; } cout << endl; 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <vector> using namespace std; int bin_search(vector<int>& tab, int b,int e,int x) { if(b==e) { return b; } int mid = (b+e)/2; if(tab[mid] == x) { return mid; } else if(tab[mid] > x) { return bin_search(tab,b,mid,x); } else { return bin_search(tab,mid+1,e,x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int>bit; vector<int>bit_sum; bit.push_back(0); bit_sum.push_back(0); int it = 1; while (bit_sum.back() < n) { bit.push_back(bit[it/2] + it%2); bit_sum.push_back(bit_sum.back()+bit.back()); it++; } vector<int> result; int end = bit_sum.size(); while(n > 0) { int b_re = bin_search(bit_sum,0,end,n); n -= bit[b_re]; result.push_back(b_re); end = b_re-1; } cout << result.size() << '\n'; for (int i = 0; i < result.size(); i++) { cout << result[i] << ' '; } cout << endl; return 0; } |