#include<bits/stdc++.h> using namespace std; int litBits(int k) { //IT WORKSSSS int ans = 0; while (k) { ans += k & 1; k >>=1; } return ans; } vector<int> res(int n) { vector<int> ans; int count = 0; for (int i = 1; i <= n; i++) { ans.push_back(i); count += litBits(i); } if(count > n) { for (int i = ans.size() - 1; i >= 0; i--) { if(count - litBits(ans[i]) >= n) { count -= litBits(ans[i]); ans.erase(ans.begin() + i); } } } return ans; } int main() { int n; cin >> n; vector<int> ans = res(n); cout<<ans.size()<<'\n'; for (int i = ans.size() - 1; i >= 0; i--) { cout<<ans[i]<<" "; } }
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 | #include<bits/stdc++.h> using namespace std; int litBits(int k) { //IT WORKSSSS int ans = 0; while (k) { ans += k & 1; k >>=1; } return ans; } vector<int> res(int n) { vector<int> ans; int count = 0; for (int i = 1; i <= n; i++) { ans.push_back(i); count += litBits(i); } if(count > n) { for (int i = ans.size() - 1; i >= 0; i--) { if(count - litBits(ans[i]) >= n) { count -= litBits(ans[i]); ans.erase(ans.begin() + i); } } } return ans; } int main() { int n; cin >> n; vector<int> ans = res(n); cout<<ans.size()<<'\n'; for (int i = ans.size() - 1; i >= 0; i--) { cout<<ans[i]<<" "; } } |