#include <iostream> #include <vector> using namespace std; void prep(vector<int>& ones) { ones[0] = 0; int N = ones.size() - 1; for(int i = 1; i <= N; ++i) { ones[i] = ones[i / 2] + i % 2; } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> ones(n + 1); prep(ones); vector<int> result; long long sum = 0; for(int i = 1; i < n + 1; ++i) { int num = ones[i]; sum += num; result.push_back(i); if(sum > n) break; } int toRemove = sum - n; if(toRemove > 0) { for(int i = result.size() - 1; i >= 0; --i) { if(ones[i + 1] == toRemove) { result.erase(result.begin() + i); break; } } } cout << result.size() << "\n"; for(int i = result.size() - 1; i >= 0; --i) { cout << result[i] << " "; } cout << "\n"; }
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 | #include <iostream> #include <vector> using namespace std; void prep(vector<int>& ones) { ones[0] = 0; int N = ones.size() - 1; for(int i = 1; i <= N; ++i) { ones[i] = ones[i / 2] + i % 2; } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> ones(n + 1); prep(ones); vector<int> result; long long sum = 0; for(int i = 1; i < n + 1; ++i) { int num = ones[i]; sum += num; result.push_back(i); if(sum > n) break; } int toRemove = sum - n; if(toRemove > 0) { for(int i = result.size() - 1; i >= 0; --i) { if(ones[i + 1] == toRemove) { result.erase(result.begin() + i); break; } } } cout << result.size() << "\n"; for(int i = result.size() - 1; i >= 0; --i) { cout << result[i] << " "; } cout << "\n"; } |