#include <bits/stdc++.h>
using namespace std;
int howbit(int a){
int res = 0;
while(a != 0){
if(a%2){
res ++;
}
a = a>>1;
}
return res;
}
int n, tab[1000000], k;
vector<int> V1;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for(int i = 1; i<n+1; i++){
tab[i] = tab[i-1] + howbit(i);
if(tab[i] >= n){
k = i;
break;
}
}
while(n!= 0){
n -= howbit(k);
V1.push_back(k);
while(tab[k] >= n){
k--;
}
k++;
}
cout << V1.size();
cout << endl;
for(int i = 0; i<V1.size(); i++){
cout << V1[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 39 40 41 42 43 44 45 46 47 | #include <bits/stdc++.h> using namespace std; int howbit(int a){ int res = 0; while(a != 0){ if(a%2){ res ++; } a = a>>1; } return res; } int n, tab[1000000], k; vector<int> V1; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; for(int i = 1; i<n+1; i++){ tab[i] = tab[i-1] + howbit(i); if(tab[i] >= n){ k = i; break; } } while(n!= 0){ n -= howbit(k); V1.push_back(k); while(tab[k] >= n){ k--; } k++; } cout << V1.size(); cout << endl; for(int i = 0; i<V1.size(); i++){ cout << V1[i] << " "; } return 0; } |
English