#include <bits/stdc++.h> using namespace std; int cnt,act,deleted; vector<int> v; int main(){ int n; scanf("%d",&n); for(int i=1; i>0; i++){ v.push_back(i); act+=__builtin_popcount(i); if(act==n){ printf("%d\n",(int)v.size()); for(int i=v.size()-1; i>=0; i--) printf("%d ",v[i]); return 0; } if(act>n){ for(int j=20; j>=0; j--){ if(i&(1<<j)){ cnt++; deleted+=(1<<j); } if(cnt==act-n){ printf("%d\n",(int)v.size()-1); for(int k=v.size()-1; k>=0; k--){ if(v[k]!=deleted) printf("%d ",v[k]); } return 0; } } } } 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 | #include <bits/stdc++.h> using namespace std; int cnt,act,deleted; vector<int> v; int main(){ int n; scanf("%d",&n); for(int i=1; i>0; i++){ v.push_back(i); act+=__builtin_popcount(i); if(act==n){ printf("%d\n",(int)v.size()); for(int i=v.size()-1; i>=0; i--) printf("%d ",v[i]); return 0; } if(act>n){ for(int j=20; j>=0; j--){ if(i&(1<<j)){ cnt++; deleted+=(1<<j); } if(cnt==act-n){ printf("%d\n",(int)v.size()-1); for(int k=v.size()-1; k>=0; k--){ if(v[k]!=deleted) printf("%d ",v[k]); } return 0; } } } } return 0; } |