#include <iostream> #include <vector> using namespace std; void solve() { int n; cin >> n; vector<int> result; int cur_sum = 0; int cur_ind = 1; while(cur_sum < n) { result.push_back(cur_ind); cur_sum += __builtin_popcount(cur_ind); cur_ind++; } cur_ind--; for(int i = result.size() - 1; i >= 0; i--) { if(cur_sum - __builtin_popcount(result[i]) >= n) { cur_sum -= __builtin_popcount(result[i]); result[i] *= -1; cur_ind--; } } cout << cur_ind << "\n"; for(int i = result.size() - 1; i >= 0; i--) { if(result[i] > 0) cout << result[i] << " "; } cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 | #include <iostream> #include <vector> using namespace std; void solve() { int n; cin >> n; vector<int> result; int cur_sum = 0; int cur_ind = 1; while(cur_sum < n) { result.push_back(cur_ind); cur_sum += __builtin_popcount(cur_ind); cur_ind++; } cur_ind--; for(int i = result.size() - 1; i >= 0; i--) { if(cur_sum - __builtin_popcount(result[i]) >= n) { cur_sum -= __builtin_popcount(result[i]); result[i] *= -1; cur_ind--; } } cout << cur_ind << "\n"; for(int i = result.size() - 1; i >= 0; i--) { if(result[i] > 0) cout << result[i] << " "; } cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |