#include <iostream> #include <vector> using namespace std; typedef unsigned u32; constexpr u32 bit_power(u32 x) { u32 res = 0; for(uint i = 1; i <= x; i <<= 1) if(x & i) ++res; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); u32 n, sum = 0; cin >> n; vector<pair<u32, u32>> vec; vec.reserve(n); for(uint i = 1; sum < n; ++i) { auto tmp = bit_power(i); sum += tmp; vec.push_back({tmp, i}); } u32 s = vec.size(); for(auto i = vec.rbegin(); sum > n && i < vec.rend(); ++i) { if(i->first <= sum - n) { sum -= i->first; i->second = 0; s--; } } cout << s << '\n'; for(auto i = vec.crbegin(); i < vec.crend(); ++i) if(i->second) cout << i->second << ' '; }
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 | #include <iostream> #include <vector> using namespace std; typedef unsigned u32; constexpr u32 bit_power(u32 x) { u32 res = 0; for(uint i = 1; i <= x; i <<= 1) if(x & i) ++res; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); u32 n, sum = 0; cin >> n; vector<pair<u32, u32>> vec; vec.reserve(n); for(uint i = 1; sum < n; ++i) { auto tmp = bit_power(i); sum += tmp; vec.push_back({tmp, i}); } u32 s = vec.size(); for(auto i = vec.rbegin(); sum > n && i < vec.rend(); ++i) { if(i->first <= sum - n) { sum -= i->first; i->second = 0; s--; } } cout << s << '\n'; for(auto i = vec.crbegin(); i < vec.crend(); ++i) if(i->second) cout << i->second << ' '; } |