#include <bits/stdc++.h> using namespace std; const int N = 1000001; int n, t[N]; int bytes(int x) { int r = 0; while (x > 0) { if (x % 2) { r++; } x >>= 1; } return r; } int main() { ios_base::sync_with_stdio(0); cin >> n; int mx = 0; for (int c = 1; t[n] == 0; c++) { int b = bytes(c); mx += b; int i = min(mx, n); while (t[i] == 0 && i >= 0) { t[i] = c; i--; } } vector<int>v; int i = n; while(i > 0) { v.push_back(t[i]); i -= bytes(t[i]); } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { cout << v[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 | #include <bits/stdc++.h> using namespace std; const int N = 1000001; int n, t[N]; int bytes(int x) { int r = 0; while (x > 0) { if (x % 2) { r++; } x >>= 1; } return r; } int main() { ios_base::sync_with_stdio(0); cin >> n; int mx = 0; for (int c = 1; t[n] == 0; c++) { int b = bytes(c); mx += b; int i = min(mx, n); while (t[i] == 0 && i >= 0) { t[i] = c; i--; } } vector<int>v; int i = n; while(i > 0) { v.push_back(t[i]); i -= bytes(t[i]); } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; return 0; } |