#include<iostream> #include<vector> using namespace std; int main(){ int n; cin >> n; vector<int>v; vector<bool>bo; int z=1,kn=n,l=0; while(true){ kn-=__builtin_popcount(z); if(kn==0){v.push_back(z);bo.push_back(true);break;} if(kn>0){v.push_back(z);bo.push_back(true);z++;} else{ int rn=kn*-1; for(int j=v.size()-1;j>=0;j--){ if(rn>__builtin_popcount(v[j])){bo[j]=false;l++;rn-=__builtin_popcount(v[j]);} else if(rn==__builtin_popcount(v[j])){bo[j]=false;l++;rn=0;break;} } v.push_back(z); bo.push_back(true); break; } } cout<<v.size()-l<<endl; for(int i=v.size()-1;i>=0;i--){ if(bo[i]){cout<<v[i]<<" ";} } cout<<endl; }
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<iostream> #include<vector> using namespace std; int main(){ int n; cin >> n; vector<int>v; vector<bool>bo; int z=1,kn=n,l=0; while(true){ kn-=__builtin_popcount(z); if(kn==0){v.push_back(z);bo.push_back(true);break;} if(kn>0){v.push_back(z);bo.push_back(true);z++;} else{ int rn=kn*-1; for(int j=v.size()-1;j>=0;j--){ if(rn>__builtin_popcount(v[j])){bo[j]=false;l++;rn-=__builtin_popcount(v[j]);} else if(rn==__builtin_popcount(v[j])){bo[j]=false;l++;rn=0;break;} } v.push_back(z); bo.push_back(true); break; } } cout<<v.size()-l<<endl; for(int i=v.size()-1;i>=0;i--){ if(bo[i]){cout<<v[i]<<" ";} } cout<<endl; } |