#include <bits/stdc++.h> using namespace std; int n, sum, x, it; vector <int> w, res; int main() { cin >> n; while(sum < n) { x = __builtin_popcount(it++); w.push_back(it - 1); sum += x; } for(int i = w.size() - 1; i > 0; --i) { if(__builtin_popcount(i) == sum - n) { for(int j = i - 1; j > 0; --j) res.push_back(j); break; } res.push_back(i); } sort(res.begin(), res.end()); reverse(res.begin(), res.end()); cout << res.size() << "\n"; for(auto u : res) cout << u << " "; }
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 | #include <bits/stdc++.h> using namespace std; int n, sum, x, it; vector <int> w, res; int main() { cin >> n; while(sum < n) { x = __builtin_popcount(it++); w.push_back(it - 1); sum += x; } for(int i = w.size() - 1; i > 0; --i) { if(__builtin_popcount(i) == sum - n) { for(int j = i - 1; j > 0; --j) res.push_back(j); break; } res.push_back(i); } sort(res.begin(), res.end()); reverse(res.begin(), res.end()); cout << res.size() << "\n"; for(auto u : res) cout << u << " "; } |