#include <cstdio> int tab[1000001]; int cnt(int n) { int c = 0; while (n > 0) { c += (n % 2); n /= 2; } return c; } int main() { int n; scanf("%d", &n); tab[0] = 0; int j = -1; int acc = 0; for (int i = 1; i <= n; i++) { //tab[i] = tab[i - 1] + cnt(i); tab[i] = cnt(i); //printf("%d\n", tab[i]); acc += tab[i]; if (acc >= n) { j = i; break; } } int ret = 0; int d = acc - n; //printf("%d\n", d); int first = 1; if (d) { printf("%d\n", j - 1); } else { printf("%d\n", j); } for (int i = j; i >= 1; i--) { if (first && tab[i] == d) { first = 0; } else { printf("%d ", 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 52 53 54 55 56 57 58 59 60 61 62 | #include <cstdio> int tab[1000001]; int cnt(int n) { int c = 0; while (n > 0) { c += (n % 2); n /= 2; } return c; } int main() { int n; scanf("%d", &n); tab[0] = 0; int j = -1; int acc = 0; for (int i = 1; i <= n; i++) { //tab[i] = tab[i - 1] + cnt(i); tab[i] = cnt(i); //printf("%d\n", tab[i]); acc += tab[i]; if (acc >= n) { j = i; break; } } int ret = 0; int d = acc - n; //printf("%d\n", d); int first = 1; if (d) { printf("%d\n", j - 1); } else { printf("%d\n", j); } for (int i = j; i >= 1; i--) { if (first && tab[i] == d) { first = 0; } else { printf("%d ", i); } } return 0; } |