#include <iostream> #include <vector> using namespace std; int last[25]; vector <int> ans; int bits(int a) { int counter=0; while (a > 0){ if (a&1) counter++; a = (a >> 1); } return counter; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, sum=0, i=1, del = 0; cin >> n; while (sum < n){ int add = bits(i); if (sum + add > n){ del = last[sum + add - n]; sum = n; } last[add] = i; ans.push_back(i); sum += add; i++; } if (del == 0) cout << ans.size() << "\n"; else cout << ans.size()-1 << "\n"; for (i = ans.size()-1; i >= 0; i--) { if (ans[i] != del) cout << ans[i] << " "; } 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 | #include <iostream> #include <vector> using namespace std; int last[25]; vector <int> ans; int bits(int a) { int counter=0; while (a > 0){ if (a&1) counter++; a = (a >> 1); } return counter; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, sum=0, i=1, del = 0; cin >> n; while (sum < n){ int add = bits(i); if (sum + add > n){ del = last[sum + add - n]; sum = n; } last[add] = i; ans.push_back(i); sum += add; i++; } if (del == 0) cout << ans.size() << "\n"; else cout << ans.size()-1 << "\n"; for (i = ans.size()-1; i >= 0; i--) { if (ans[i] != del) cout << ans[i] << " "; } return 0; } |