#include <bits/stdc++.h> using namespace std; vector<int> answer; void Solve(int bits_num); int main() { cin.tie(0); ios::sync_with_stdio(0); int bits_num; cin >> bits_num; Solve(bits_num); cout << answer.size() << endl; for (int i = 0; i < answer.size(); i++) { cout << answer[i] << " "; } cout << endl; return 0; } void Solve(int bits_num) { int iter = 1; while (bits_num > 0) { int bits = __builtin_popcount(iter); bits_num -= bits; answer.push_back(iter); iter++; } reverse(answer.begin(), answer.end()); if (bits_num == 0) { return; } bits_num = -bits_num; for (int i = 1; i < answer.size(); i++) { if (__builtin_popcount(answer[i]) == bits_num) { answer.erase(answer.begin() + i); return; } } }
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 <bits/stdc++.h> using namespace std; vector<int> answer; void Solve(int bits_num); int main() { cin.tie(0); ios::sync_with_stdio(0); int bits_num; cin >> bits_num; Solve(bits_num); cout << answer.size() << endl; for (int i = 0; i < answer.size(); i++) { cout << answer[i] << " "; } cout << endl; return 0; } void Solve(int bits_num) { int iter = 1; while (bits_num > 0) { int bits = __builtin_popcount(iter); bits_num -= bits; answer.push_back(iter); iter++; } reverse(answer.begin(), answer.end()); if (bits_num == 0) { return; } bits_num = -bits_num; for (int i = 1; i < answer.size(); i++) { if (__builtin_popcount(answer[i]) == bits_num) { answer.erase(answer.begin() + i); return; } } } |