#include <iostream> #include <string> using namespace std; int bits(long n) { int b{}; while (n > 0) { if (n & 1) { b++; } n >>= 1; } return b; } int main() { long n; long k{}; string line{}; cin >> n; while (n > 0) { k++; n -= bits(k); } line.append(to_string(k)); for (long i=k-1; i>0; i--) { if (n < 0 && -n >= bits(i)) { n += bits(i); k--; } else { line.append(" " + to_string(i)); } } cout << k << endl; cout << line << endl; 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 | #include <iostream> #include <string> using namespace std; int bits(long n) { int b{}; while (n > 0) { if (n & 1) { b++; } n >>= 1; } return b; } int main() { long n; long k{}; string line{}; cin >> n; while (n > 0) { k++; n -= bits(k); } line.append(to_string(k)); for (long i=k-1; i>0; i--) { if (n < 0 && -n >= bits(i)) { n += bits(i); k--; } else { line.append(" " + to_string(i)); } } cout << k << endl; cout << line << endl; return 0; } |