#include <bits/stdc++.h>
#include <queue>
using namespace std;
int n;
void read() {
cin>>n;
}
int t[1000000];
pair<int,int> countSetBits()
{
int i = 1;
int ile = 0;
while(true){
int s = __builtin_popcount(i);
t[i]=s;
ile += s;
if(ile>=n)break;
i++;
}
return make_pair(i,ile);
}
int w[200000];
void solve() {
read();
pair<int,int> ww;
ww = countSetBits();
int i = ww.first;
int wynik = ww.second;
int j = 0;
while(i>=0){
if(wynik-t[i]<n){
w[j++]=i;
n-=t[i];
}
wynik-=t[i];
i--;
}
cout<<j<<"\n";
for(int i = 0;i<j;i++){
cout<<w[i]<<" ";
}
//cout<<w.first<<" "<<w.second<<"\n";
}
int main() {
ios_base::sync_with_stdio(false);
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 46 47 48 49 50 51 52 53 54 | #include <bits/stdc++.h> #include <queue> using namespace std; int n; void read() { cin>>n; } int t[1000000]; pair<int,int> countSetBits() { int i = 1; int ile = 0; while(true){ int s = __builtin_popcount(i); t[i]=s; ile += s; if(ile>=n)break; i++; } return make_pair(i,ile); } int w[200000]; void solve() { read(); pair<int,int> ww; ww = countSetBits(); int i = ww.first; int wynik = ww.second; int j = 0; while(i>=0){ if(wynik-t[i]<n){ w[j++]=i; n-=t[i]; } wynik-=t[i]; i--; } cout<<j<<"\n"; for(int i = 0;i<j;i++){ cout<<w[i]<<" "; } //cout<<w.first<<" "<<w.second<<"\n"; } int main() { ios_base::sync_with_stdio(false); solve(); return 0; } |
English