#include <cstdio> #include <algorithm> using namespace std; int bc(int n) { int r = 0; do { r += n & 1; } while (n >>= 1); return r; } int BC[1000056]; int A[1000056]; int B[1000056]; int main() { int n; scanf("%d", &n); for (int i=0; i<=n; i++) { BC[i] = bc(i); } A[0] = 0; A[1] = 1; int k = 1; for (int i=2; i<=n; i++) { k = max(k-20, 1); while (true) { int cc = BC[k]; if (A[i-cc] < k) { A[i] = k; break; } k++; } } int v = 0; int p = n; while (p > 0) { int a = A[p]; B[v++] = a; p -= BC[a]; } printf("%d\n", v); for (int i=0; i<v; i++) printf("%d ", B[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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <cstdio> #include <algorithm> using namespace std; int bc(int n) { int r = 0; do { r += n & 1; } while (n >>= 1); return r; } int BC[1000056]; int A[1000056]; int B[1000056]; int main() { int n; scanf("%d", &n); for (int i=0; i<=n; i++) { BC[i] = bc(i); } A[0] = 0; A[1] = 1; int k = 1; for (int i=2; i<=n; i++) { k = max(k-20, 1); while (true) { int cc = BC[k]; if (A[i-cc] < k) { A[i] = k; break; } k++; } } int v = 0; int p = n; while (p > 0) { int a = A[p]; B[v++] = a; p -= BC[a]; } printf("%d\n", v); for (int i=0; i<v; i++) printf("%d ", B[i]); printf("\n"); return 0; } |