#include <iostream> using namespace std; // https://stackoverflow.com/questions/109023/count-the-number-of-set-bits-in-a-32-bit-integer int sumabitow(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0F0F0F0F; return (i * 0x01010101) >> 24; } int main() { int n, suma = 0, maks = 0, wykluczone, suma1 = 0; cin >> n; while (suma < n) suma += sumabitow(++maks); if (suma == n) { cout << maks << '\n'; for (int i = maks; i > 0; i--) { cout << i << ' '; } return 0; } wykluczone = maks; while (suma1 != n) { suma1 = suma - sumabitow(--wykluczone); } cout << maks - 1 << '\n'; for (int i = maks; i > 0; i--) { if (i != wykluczone) cout << 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 | #include <iostream> using namespace std; // https://stackoverflow.com/questions/109023/count-the-number-of-set-bits-in-a-32-bit-integer int sumabitow(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0F0F0F0F; return (i * 0x01010101) >> 24; } int main() { int n, suma = 0, maks = 0, wykluczone, suma1 = 0; cin >> n; while (suma < n) suma += sumabitow(++maks); if (suma == n) { cout << maks << '\n'; for (int i = maks; i > 0; i--) { cout << i << ' '; } return 0; } wykluczone = maks; while (suma1 != n) { suma1 = suma - sumabitow(--wykluczone); } cout << maks - 1 << '\n'; for (int i = maks; i > 0; i--) { if (i != wykluczone) cout << i << ' '; } return 0; } |