#include <iostream> #include <vector> using namespace std; int count_onebits(int a) { int s = 0; while (a > 0) { s += (a % 2); a /= 2; } return s; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> firsts(n+1); firsts[0] = 0; firsts[1] = 1; int curr_first = 1; int curr_first_onebits = 1; for (int i = 2; i <= n; ++i) { int j = i - curr_first_onebits; if (firsts[j] == curr_first) { curr_first++; curr_first_onebits = count_onebits(curr_first); } firsts[i] = curr_first; } vector<int> seq; int m = n; while (m > 0) { int el = firsts[m]; seq.push_back(el); m -= count_onebits(el); } cout << seq.size() << "\n"; for (auto it = seq.begin(); it != seq.end(); ++it) cout << *it << " "; 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 | #include <iostream> #include <vector> using namespace std; int count_onebits(int a) { int s = 0; while (a > 0) { s += (a % 2); a /= 2; } return s; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> firsts(n+1); firsts[0] = 0; firsts[1] = 1; int curr_first = 1; int curr_first_onebits = 1; for (int i = 2; i <= n; ++i) { int j = i - curr_first_onebits; if (firsts[j] == curr_first) { curr_first++; curr_first_onebits = count_onebits(curr_first); } firsts[i] = curr_first; } vector<int> seq; int m = n; while (m > 0) { int el = firsts[m]; seq.push_back(el); m -= count_onebits(el); } cout << seq.size() << "\n"; for (auto it = seq.begin(); it != seq.end(); ++it) cout << *it << " "; cout << endl; return 0; } |