/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <algorithm> #include <vector> using namespace std; int liczba_jedynek(int n) { int cnt = 0; while (n) { cnt += n & 1; n >>= 1; } return cnt; } int binsearch(int x, vector<int>& v) { int l = 0, m; int r = v.size() - 1; while(l < r) { m = (l + r)/2; if(v[m] >= x) r = m; else l = m + 1; } return l; } int main() { int n; cin >> n; int size = n + 5; // v[i]: maksymalna liczba bitow jesli zaczniemy od i vector<int> v(size); v[0] = 0; for(int i = 1; i < size; i++) { v[i] = v[i-1] + liczba_jedynek(i); } vector<int> res; int remaining = n; while(remaining > 0) { int ind = binsearch(remaining, v); res.push_back(ind); remaining -= liczba_jedynek(ind); } cout << res.size() << endl; for(auto i : res) cout << 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 | /****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <iostream> #include <algorithm> #include <vector> using namespace std; int liczba_jedynek(int n) { int cnt = 0; while (n) { cnt += n & 1; n >>= 1; } return cnt; } int binsearch(int x, vector<int>& v) { int l = 0, m; int r = v.size() - 1; while(l < r) { m = (l + r)/2; if(v[m] >= x) r = m; else l = m + 1; } return l; } int main() { int n; cin >> n; int size = n + 5; // v[i]: maksymalna liczba bitow jesli zaczniemy od i vector<int> v(size); v[0] = 0; for(int i = 1; i < size; i++) { v[i] = v[i-1] + liczba_jedynek(i); } vector<int> res; int remaining = n; while(remaining > 0) { int ind = binsearch(remaining, v); res.push_back(ind); remaining -= liczba_jedynek(ind); } cout << res.size() << endl; for(auto i : res) cout << i << " "; cout << endl; return 0; } |