#include<cstdio> int T[1000001]; int Q[1000001]; int res[1000001]; int main() { int n; scanf("%d", &n); int i = 0; int c = 0; int sum = 0; while (n > sum) { c++; int popcnt = __builtin_popcount(c); sum += popcnt; T[i] = sum; Q[i] = popcnt; // printf("i: %d (c: %d), T[i] = %d, Q[i] = %d\n", i, c, T[i], Q[i]); i++; } int r = i; int res_i = 0; while (n != 0) { if (r > 0 and T[r-1] >= n) { // printf("r: %d, T[%d]: %d >= %d -> decreasing r\n", r, r-1, T[r-1], n); --r; continue; } // printf("taking r: %d, Q[r]: %d, n: %d, after: %d\n", r, Q[r], n, n - Q[r]); res[res_i] = r+1; ++res_i; n -= Q[r]; } printf("%d\n", res_i); for (int j = 0; j < res_i; j++) { printf("%d ", res[j]); } printf("\n"); }
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 53 54 55 56 57 | #include<cstdio> int T[1000001]; int Q[1000001]; int res[1000001]; int main() { int n; scanf("%d", &n); int i = 0; int c = 0; int sum = 0; while (n > sum) { c++; int popcnt = __builtin_popcount(c); sum += popcnt; T[i] = sum; Q[i] = popcnt; // printf("i: %d (c: %d), T[i] = %d, Q[i] = %d\n", i, c, T[i], Q[i]); i++; } int r = i; int res_i = 0; while (n != 0) { if (r > 0 and T[r-1] >= n) { // printf("r: %d, T[%d]: %d >= %d -> decreasing r\n", r, r-1, T[r-1], n); --r; continue; } // printf("taking r: %d, Q[r]: %d, n: %d, after: %d\n", r, Q[r], n, n - Q[r]); res[res_i] = r+1; ++res_i; n -= Q[r]; } printf("%d\n", res_i); for (int j = 0; j < res_i; j++) { printf("%d ", res[j]); } printf("\n"); } |