// // main.cpp // Muzyka pop 2 // // Created by Mikołaj Małysz on 13/12/2022. // #include <iostream> #include <vector> using namespace std; unsigned int countSetBits(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int main(int argc, const char * argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; bool run = true; vector<int> v = {}; int current = 1; int sum = 0; while (run) { sum += countSetBits(current); v.push_back(current); current++; if (sum >= n) { break; } } int diff; if (sum > n) { diff = sum-n; for(long i = v.size() - 1; i >= 0; i--){ if (countSetBits(v[i]) == diff) { v.erase(v.begin() + i); break; } } } cout << v.size() << endl; for (auto it = v.rbegin(); it != v.rend(); it++){ // Print each element cout << *it << ' '; } cout << '\n'; 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 | // // main.cpp // Muzyka pop 2 // // Created by Mikołaj Małysz on 13/12/2022. // #include <iostream> #include <vector> using namespace std; unsigned int countSetBits(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int main(int argc, const char * argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; bool run = true; vector<int> v = {}; int current = 1; int sum = 0; while (run) { sum += countSetBits(current); v.push_back(current); current++; if (sum >= n) { break; } } int diff; if (sum > n) { diff = sum-n; for(long i = v.size() - 1; i >= 0; i--){ if (countSetBits(v[i]) == diff) { v.erase(v.begin() + i); break; } } } cout << v.size() << endl; for (auto it = v.rbegin(); it != v.rend(); it++){ // Print each element cout << *it << ' '; } cout << '\n'; return 0; } |