#include <math.h> #include <iostream> using namespace std; int lowerpower(int s) { return pow(2, floor(log2(s))); } int popq(int q) { int a = 0; while (q > 0) { if (q % 2 == 1) { a++; } q /= 2; } return a; } int total = 0; int find(int sear) { int current = 0; while (total < sear) { current++; total += popq(current); } return current; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int w; cin >> w; int q = find(w); int x = total - w; int l = lowerpower(q); int kys = 0; for (int i = 0; i < x; i++) { kys += l; l /= 2; } if (total == w) { cout << q << endl; } else { cout << q - 1 << endl; } for (int i = q; i >kys; i--) { cout << i << " "; } for (int i = kys-1; i >= 1; i--) { cout << i << " "; } }
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 63 64 65 66 67 68 | #include <math.h> #include <iostream> using namespace std; int lowerpower(int s) { return pow(2, floor(log2(s))); } int popq(int q) { int a = 0; while (q > 0) { if (q % 2 == 1) { a++; } q /= 2; } return a; } int total = 0; int find(int sear) { int current = 0; while (total < sear) { current++; total += popq(current); } return current; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int w; cin >> w; int q = find(w); int x = total - w; int l = lowerpower(q); int kys = 0; for (int i = 0; i < x; i++) { kys += l; l /= 2; } if (total == w) { cout << q << endl; } else { cout << q - 1 << endl; } for (int i = q; i >kys; i--) { cout << i << " "; } for (int i = kys-1; i >= 1; i--) { cout << i << " "; } } |