#include<cstdio> #define N 125000 int main() { int n; int bits[N]; int sums[N]; bits[0] = 0; bits[1] = 1; sums[0] = 0; sums[1] = 1; for (int i = 2; i < N; i++) { bits[i] = (i % 2) + bits[i/2]; sums[i] = sums[i-1] + bits[i]; } scanf("%d", &n); int m = n; int result = 0; for (int i = N - 1; i >= 1; i--) { if (m == 0) { break; } if (sums[i] >= m && sums[i-1] < m) { result++; m -= bits[i]; } } printf("%d\n", result); for (int i = N - 1; i >= 1; i--) { if (n == 0) { break; } if (sums[i] >= n && sums[i-1] < n) { printf("%d ", i); n -= bits[i]; } } 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 | #include<cstdio> #define N 125000 int main() { int n; int bits[N]; int sums[N]; bits[0] = 0; bits[1] = 1; sums[0] = 0; sums[1] = 1; for (int i = 2; i < N; i++) { bits[i] = (i % 2) + bits[i/2]; sums[i] = sums[i-1] + bits[i]; } scanf("%d", &n); int m = n; int result = 0; for (int i = N - 1; i >= 1; i--) { if (m == 0) { break; } if (sums[i] >= m && sums[i-1] < m) { result++; m -= bits[i]; } } printf("%d\n", result); for (int i = N - 1; i >= 1; i--) { if (n == 0) { break; } if (sums[i] >= n && sums[i-1] < n) { printf("%d ", i); n -= bits[i]; } } return 0; } |