#include <cstdio> #include <vector> using namespace std; const int N = 1000000 + 999; int nextItem[N]; int nextN[N]; int popCount(unsigned int n) { const unsigned int ONE = 1; int res = 0; while (n > 0) { res += n & ONE; n >>= 1; } return res; } int main() { int n; scanf("%d", &n); nextItem[1] = 1; nextN[1] = 0; int x = 2; for (int fst=2; x<=n; ++fst) { int cnt = popCount(fst); for (int i=0; i<cnt; ++i) { nextItem[x] = fst; nextN[x] = x - cnt; ++x; } } vector<int> res; while (n > 0) { res.push_back(nextItem[n]); n = nextN[n]; } printf("%d\n", (int)res.size()); for (int i=0; i<res.size(); ++i) printf("%d%s", res[i], i+1<res.size() ? " " : "\n"); 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 <cstdio> #include <vector> using namespace std; const int N = 1000000 + 999; int nextItem[N]; int nextN[N]; int popCount(unsigned int n) { const unsigned int ONE = 1; int res = 0; while (n > 0) { res += n & ONE; n >>= 1; } return res; } int main() { int n; scanf("%d", &n); nextItem[1] = 1; nextN[1] = 0; int x = 2; for (int fst=2; x<=n; ++fst) { int cnt = popCount(fst); for (int i=0; i<cnt; ++i) { nextItem[x] = fst; nextN[x] = x - cnt; ++x; } } vector<int> res; while (n > 0) { res.push_back(nextItem[n]); n = nextN[n]; } printf("%d\n", (int)res.size()); for (int i=0; i<res.size(); ++i) printf("%d%s", res[i], i+1<res.size() ? " " : "\n"); return 0; } |