#include<iostream> #include<vector> #include<cmath> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0) ; std::cout.tie(0); int n; cin >> n; int suma=0,last=1 ; for(int i=1;suma<n;i++) suma+=__builtin_popcount(i),last=i ; vector<int>odp ; odp.push_back(last) ; int diff=suma-n ; for(int i=last-1;i>=1;i--) { int x=__builtin_popcount(i) ; if(diff>=x) { diff-=x; continue ; } odp.push_back(i) ; } cout << odp.size() << endl ; for(auto i:odp) 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 | #include<iostream> #include<vector> #include<cmath> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0) ; std::cout.tie(0); int n; cin >> n; int suma=0,last=1 ; for(int i=1;suma<n;i++) suma+=__builtin_popcount(i),last=i ; vector<int>odp ; odp.push_back(last) ; int diff=suma-n ; for(int i=last-1;i>=1;i--) { int x=__builtin_popcount(i) ; if(diff>=x) { diff-=x; continue ; } odp.push_back(i) ; } cout << odp.size() << endl ; for(auto i:odp) cout << i << " " ; } |