//============================================================================ // Name : 2c-muz.cpp // Author : // Version : // Description : https://sio2.mimuw.edu.pl/c/pa-2022-1/p/muz/ //============================================================================ #include <bits/stdc++.h> using namespace std; int countSetBits(int n) { int count = 0; while (n) { n &= (n - 1); count++; } return count; } int main() { int n; cin >> n; int bitSum = 0; int i = 0; while (bitSum < n) { i++; bitSum += countSetBits(i); } const int max = i; deque<int> to_remove; int iCount; while (bitSum > n) { i--; iCount = countSetBits(i); if (bitSum - iCount >= n) { bitSum -= iCount; to_remove.push_back(i); } } cout << max - to_remove.size() << endl; for (int j = max; j > 0; j--) { if (to_remove.size() > 0 && to_remove[0] == j) { to_remove.pop_front(); } else { cout << j << " "; } } cout << 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 44 45 46 47 48 49 50 51 | //============================================================================ // Name : 2c-muz.cpp // Author : // Version : // Description : https://sio2.mimuw.edu.pl/c/pa-2022-1/p/muz/ //============================================================================ #include <bits/stdc++.h> using namespace std; int countSetBits(int n) { int count = 0; while (n) { n &= (n - 1); count++; } return count; } int main() { int n; cin >> n; int bitSum = 0; int i = 0; while (bitSum < n) { i++; bitSum += countSetBits(i); } const int max = i; deque<int> to_remove; int iCount; while (bitSum > n) { i--; iCount = countSetBits(i); if (bitSum - iCount >= n) { bitSum -= iCount; to_remove.push_back(i); } } cout << max - to_remove.size() << endl; for (int j = max; j > 0; j--) { if (to_remove.size() > 0 && to_remove[0] == j) { to_remove.pop_front(); } else { cout << j << " "; } } cout << endl; return 0; } |