#include<bits/stdc++.h>
using namespace std;
unsigned int countSetBits(unsigned int n)
{
unsigned int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
int sum(int n){
int s=0;
for(int i=1;i<=n;i++){
s+=countSetBits(i);
}
return s;
}
int wyn(int n){
int p,k,sr;
p=1;k=n;
sr=(p+k)>>1;
while(p<k){
if(sum(sr)>=n){
k=sr;
}
else{
p=sr+1;
}
sr=(p+k)>>1;
}
return p;
}
bool banned[1000000];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
int n,i,w,s=0,ile=0;
cin>>n;
w=wyn(n);
for(i=1;i<=w;i++){
s+=countSetBits(i);
}
i=w-1;
//cout<<"s:"<<s<<" n:"<<n<<"\n";
while(s-n>0){
if(countSetBits(i)<=s-n){
banned[i]=1;
ile++;
s-=countSetBits(i);
}
i--;
}
cout<<w-ile<<"\n";
for(i=w;i>0;i--){
if(!banned[i])
cout<<i<<" ";
}
}
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include<bits/stdc++.h> using namespace std; unsigned int countSetBits(unsigned int n) { unsigned int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } int sum(int n){ int s=0; for(int i=1;i<=n;i++){ s+=countSetBits(i); } return s; } int wyn(int n){ int p,k,sr; p=1;k=n; sr=(p+k)>>1; while(p<k){ if(sum(sr)>=n){ k=sr; } else{ p=sr+1; } sr=(p+k)>>1; } return p; } bool banned[1000000]; int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); int n,i,w,s=0,ile=0; cin>>n; w=wyn(n); for(i=1;i<=w;i++){ s+=countSetBits(i); } i=w-1; //cout<<"s:"<<s<<" n:"<<n<<"\n"; while(s-n>0){ if(countSetBits(i)<=s-n){ banned[i]=1; ile++; s-=countSetBits(i); } i--; } cout<<w-ile<<"\n"; for(i=w;i>0;i--){ if(!banned[i]) cout<<i<<" "; } } |
English