#include <cstdio> #include <vector> #include <algorithm> const int N = 1000005; const int X = 250005; const int LG = 20; int n, d[X][LG], a[N]; int main() { for (int i = 1; i < LG; ++i) d[X - 1][i] = N; for (int i = X - 2; i >= 0; --i) { for (int j = 0; j < LG; ++j) d[i][j] = d[i + 1][j]; d[i][__builtin_popcount(i)] = i; } for (int i = 1; i < N; ++i) a[i] = i; for (int i = 0; i < N; ++i) { for (int j = 1; j < LG && i + j < N; ++j) a[i + j] = std::min(a[i + j], d[a[i] + 1][j]); } scanf("%d", &n); std::vector<int> vec; while (n) { vec.push_back(a[n]); n -= __builtin_popcount(a[n]); } printf("%d\n", (int)vec.size()); for (int i = 0; i < (int)vec.size(); ++i) printf("%d ", vec[i]); printf("\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 | #include <cstdio> #include <vector> #include <algorithm> const int N = 1000005; const int X = 250005; const int LG = 20; int n, d[X][LG], a[N]; int main() { for (int i = 1; i < LG; ++i) d[X - 1][i] = N; for (int i = X - 2; i >= 0; --i) { for (int j = 0; j < LG; ++j) d[i][j] = d[i + 1][j]; d[i][__builtin_popcount(i)] = i; } for (int i = 1; i < N; ++i) a[i] = i; for (int i = 0; i < N; ++i) { for (int j = 1; j < LG && i + j < N; ++j) a[i + j] = std::min(a[i + j], d[a[i] + 1][j]); } scanf("%d", &n); std::vector<int> vec; while (n) { vec.push_back(a[n]); n -= __builtin_popcount(a[n]); } printf("%d\n", (int)vec.size()); for (int i = 0; i < (int)vec.size(); ++i) printf("%d ", vec[i]); printf("\n"); return 0; } |