#include <bits/stdc++.h>
using namespace std;
unsigned long long w[600001],j=1;
vector<int>v[1000001];
int main()
{
int n,indx=0,ile=0;
cin>>n;
while(n){
int g,p=__builtin_popcount(j);
if(p<=n){
n-=p;
/// cout<<j<<' '<<n<<endl;
w[indx]=j;
v[p].push_back(indx);
indx++;
ile++;
j++;
}
else{
g=p-n;
int y=v[g][v[g].size()-1];
v[g].erase(v[g].end()-1);
n+=__builtin_popcount(w[y]);
w[y]=0;
ile--;
}
if(n==0)break;
}
cout<<ile<<"\n";
for(int i=indx-1;i>=0;i--){
if(w[i]>0)cout<<w[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 | #include <bits/stdc++.h> using namespace std; unsigned long long w[600001],j=1; vector<int>v[1000001]; int main() { int n,indx=0,ile=0; cin>>n; while(n){ int g,p=__builtin_popcount(j); if(p<=n){ n-=p; /// cout<<j<<' '<<n<<endl; w[indx]=j; v[p].push_back(indx); indx++; ile++; j++; } else{ g=p-n; int y=v[g][v[g].size()-1]; v[g].erase(v[g].end()-1); n+=__builtin_popcount(w[y]); w[y]=0; ile--; } if(n==0)break; } cout<<ile<<"\n"; for(int i=indx-1;i>=0;i--){ if(w[i]>0)cout<<w[i]<<' '; } return 0; } |
English