#include <bits/stdc++.h> #define popc(x) __builtin_popcount(x) using namespace std; vector<int>solutions; vector<int> ans; int main() { int n; cin>>n; int i=0,s=0,low=0; while(s<=n) { s+=popc(++i); while(low<s) { solutions.push_back(i); low++; } } while(n>0) { ans.push_back(solutions[n-1]); n-=popc(solutions[n-1]); } cout<<ans.size()<<"\n"; for(int i=0; i<ans.size(); 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 | #include <bits/stdc++.h> #define popc(x) __builtin_popcount(x) using namespace std; vector<int>solutions; vector<int> ans; int main() { int n; cin>>n; int i=0,s=0,low=0; while(s<=n) { s+=popc(++i); while(low<s) { solutions.push_back(i); low++; } } while(n>0) { ans.push_back(solutions[n-1]); n-=popc(solutions[n-1]); } cout<<ans.size()<<"\n"; for(int i=0; i<ans.size(); i++) cout<<ans[i]<<" "; } |