#include<bits/stdc++.h> using namespace std; int a, b, n, sum, x; int l[130000][2]; int bin(int m){ int k=0; while(m>0){ if(m%2==1){ k++; } m/=2; } return k; } int main(){ios_base::sync_with_stdio(0); cout.tie(0); cin>>n; while(sum<n){ a++; l[a-1][0]=1; b=bin(a); l[a-1][1]=b; sum+=b; x++; } for(int i=a-2; i>0; i--){ if(sum-l[i][1]>=n){ l[i][0]=0; sum=sum-l[i][1]; x--; } } cout<<x<<endl; for(int i=120500; i>=0; i--){ if(l[i][0]==1){ cout<<i+1<<" "; } } 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 | #include<bits/stdc++.h> using namespace std; int a, b, n, sum, x; int l[130000][2]; int bin(int m){ int k=0; while(m>0){ if(m%2==1){ k++; } m/=2; } return k; } int main(){ios_base::sync_with_stdio(0); cout.tie(0); cin>>n; while(sum<n){ a++; l[a-1][0]=1; b=bin(a); l[a-1][1]=b; sum+=b; x++; } for(int i=a-2; i>0; i--){ if(sum-l[i][1]>=n){ l[i][0]=0; sum=sum-l[i][1]; x--; } } cout<<x<<endl; for(int i=120500; i>=0; i--){ if(l[i][0]==1){ cout<<i+1<<" "; } } return 0; } |