#include <bits/stdc++.h> using namespace std; int pref[1000003]; vector<int>V; int main() { int n,sum=0,ind; cin>>n; pref[0]=-1; for(int i=1;sum<=n;i++) { sum+=__builtin_popcount(i); pref[i]=sum; ind=i; } for(int i=ind;i>=1;i--) { if(n==0)break; if(pref[i]>=n&&pref[i-1]<n) { n-=__builtin_popcount(i); V.push_back(i); } } cout<<V.size()<<"\n"; for(int i=0;i<V.size();i++) { cout<<V[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 | #include <bits/stdc++.h> using namespace std; int pref[1000003]; vector<int>V; int main() { int n,sum=0,ind; cin>>n; pref[0]=-1; for(int i=1;sum<=n;i++) { sum+=__builtin_popcount(i); pref[i]=sum; ind=i; } for(int i=ind;i>=1;i--) { if(n==0)break; if(pref[i]>=n&&pref[i-1]<n) { n-=__builtin_popcount(i); V.push_back(i); } } cout<<V.size()<<"\n"; for(int i=0;i<V.size();i++) { cout<<V[i]<<" "; } return 0; } |