#include <bits/stdc++.h>
using namespace std;
int popcnt(int x){
return __builtin_popcount(x);
}
int main(){
ios_base::sync_with_stdio(0);
vector<int> res={0};
int n,cnt=0;
cin >> n;
while(n>=0){
res.push_back(res[res.size()-1]+1);
n -= popcnt(res[res.size()-1]);
++cnt;
}
if(n<0){
for(int i=res.size()-1; i>=0; --i){
if(popcnt(res[i]) == -n){
res[i] = -1;
--cnt;
break;
}
}
}
reverse(res.begin(),res.end());
cout << cnt << '\n';
for(auto &x : res)
if(x>0) cout << x << ' ';
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 | #include <bits/stdc++.h> using namespace std; int popcnt(int x){ return __builtin_popcount(x); } int main(){ ios_base::sync_with_stdio(0); vector<int> res={0}; int n,cnt=0; cin >> n; while(n>=0){ res.push_back(res[res.size()-1]+1); n -= popcnt(res[res.size()-1]); ++cnt; } if(n<0){ for(int i=res.size()-1; i>=0; --i){ if(popcnt(res[i]) == -n){ res[i] = -1; --cnt; break; } } } reverse(res.begin(),res.end()); cout << cnt << '\n'; for(auto &x : res) if(x>0) cout << x << ' '; return 0; } |
English