// Example program #include <iostream> #include <vector> #include <unordered_map> using namespace std; void testcase(int t) { int n; cin >> n; unordered_map<int,int> M; int total_bits = 0, left_bits = n, i; for (i = 1; total_bits < n; i++) { int b = __builtin_popcount(i); M[i] = b; total_bits += b; } vector <int> Ans; for (; i >= 1; i--) { int b = M[i]; if (total_bits - b >= left_bits) { total_bits -= b; continue; } else { Ans.push_back(i); total_bits -= b; left_bits -= b; } } cout << Ans.size() <<endl; for (int a : Ans) { cout << a << " "; } cout <<endl; } int main() { ios_base::sync_with_stdio(false); int T = 1; // cin >> T; for (int t = 1; t <= T; t++) { testcase(t); } 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 | // Example program #include <iostream> #include <vector> #include <unordered_map> using namespace std; void testcase(int t) { int n; cin >> n; unordered_map<int,int> M; int total_bits = 0, left_bits = n, i; for (i = 1; total_bits < n; i++) { int b = __builtin_popcount(i); M[i] = b; total_bits += b; } vector <int> Ans; for (; i >= 1; i--) { int b = M[i]; if (total_bits - b >= left_bits) { total_bits -= b; continue; } else { Ans.push_back(i); total_bits -= b; left_bits -= b; } } cout << Ans.size() <<endl; for (int a : Ans) { cout << a << " "; } cout <<endl; } int main() { ios_base::sync_with_stdio(false); int T = 1; // cin >> T; for (int t = 1; t <= T; t++) { testcase(t); } return 0; } |