#include <cstdio> const int max_size = 121000; int bit_count[max_size]; int preprocess[max_size]; int output[max_size]; int n, m; int main() { preprocess[1] = 0; bit_count[1] = 0; for (int i = 0; i < max_size; ++i) { int cnt = 0, val = i; while (val > 0) { cnt += val & 1; val >>= 1; } bit_count[i] = cnt; preprocess[i] = preprocess[i - 1] + cnt; // printf("[%d] %d\n", i, preprocess[i]); } int start = 1; scanf("%d", &n); while (preprocess[start] < n) ++start; m = 0; while (n > 0) { while (start > 0 && preprocess[start - 1] >= n) --start; n -= bit_count[start]; output[m++] = start; } printf("%d\n", m); for (int i = 0; i < m; ++i) printf("%d ", output[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 | #include <cstdio> const int max_size = 121000; int bit_count[max_size]; int preprocess[max_size]; int output[max_size]; int n, m; int main() { preprocess[1] = 0; bit_count[1] = 0; for (int i = 0; i < max_size; ++i) { int cnt = 0, val = i; while (val > 0) { cnt += val & 1; val >>= 1; } bit_count[i] = cnt; preprocess[i] = preprocess[i - 1] + cnt; // printf("[%d] %d\n", i, preprocess[i]); } int start = 1; scanf("%d", &n); while (preprocess[start] < n) ++start; m = 0; while (n > 0) { while (start > 0 && preprocess[start - 1] >= n) --start; n -= bit_count[start]; output[m++] = start; } printf("%d\n", m); for (int i = 0; i < m; ++i) printf("%d ", output[i]); printf("\n"); return 0; } |