#include <bits/stdc++.h> #define ll long long #define pb push_back #define DUZ 2000000 using namespace std; int moc[DUZ+5]; void solve(){ vector<int> a; int N; cin>>N; for(int i=1; i<DUZ; i++){ moc[i]=__builtin_popcount(i); //cout<<i<<" "<<moc[i]<<"\n"; } int j=0; int k=1; while(j<N){ j+=moc[k]; k++; } k--; for(int i=k; i>=1; i--){ if(j-N>=moc[i]){ j-=moc[i]; }else{ //cout<<i<<" "; a.pb(i); } } cout<<a.size()<<"\n"; for(int i=0; i<a.size(); i++) cout<<a[i]<<" "; } int main(){ solve(); 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 39 40 41 42 43 44 45 | #include <bits/stdc++.h> #define ll long long #define pb push_back #define DUZ 2000000 using namespace std; int moc[DUZ+5]; void solve(){ vector<int> a; int N; cin>>N; for(int i=1; i<DUZ; i++){ moc[i]=__builtin_popcount(i); //cout<<i<<" "<<moc[i]<<"\n"; } int j=0; int k=1; while(j<N){ j+=moc[k]; k++; } k--; for(int i=k; i>=1; i--){ if(j-N>=moc[i]){ j-=moc[i]; }else{ //cout<<i<<" "; a.pb(i); } } cout<<a.size()<<"\n"; for(int i=0; i<a.size(); i++) cout<<a[i]<<" "; } int main(){ solve(); return 0; } |