#include <bits/stdc++.h>
using namespace std;
list<int> vec[21];
vector<int> ans;
void solve(){
for(int i = 1; i < 21; i++){
for(auto e : vec[i]){
ans.push_back(e);
}
}
sort(ans.begin(), ans.end(), greater<int>());
cout << ans.size() << endl;
for(auto e : ans){
cout << e << " ";
}
cout << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, x, ile = 0, i = 1, maxi = 0;
cin >> n;
while(ile < n){
x = __popcount(i);
vec[x].push_back(i);
if(x > maxi){
maxi = x;
}
ile += x;
i++;
}
i = ile - n;
if(i == 0){
solve();
return 0;
}
else{
if(vec[i].size() != 0){
vec[i].pop_back();
solve();
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <bits/stdc++.h> using namespace std; list<int> vec[21]; vector<int> ans; void solve(){ for(int i = 1; i < 21; i++){ for(auto e : vec[i]){ ans.push_back(e); } } sort(ans.begin(), ans.end(), greater<int>()); cout << ans.size() << endl; for(auto e : ans){ cout << e << " "; } cout << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, x, ile = 0, i = 1, maxi = 0; cin >> n; while(ile < n){ x = __popcount(i); vec[x].push_back(i); if(x > maxi){ maxi = x; } ile += x; i++; } i = ile - n; if(i == 0){ solve(); return 0; } else{ if(vec[i].size() != 0){ vec[i].pop_back(); solve(); return 0; } } return 0; } |
English