#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; cin >> N; int lim = N; vector<int> res; vector<int> rere; for (int i = 1; i <= N && lim > 0; ++i) { int pop_cnt = __builtin_popcount(i); res.push_back(i); lim -= pop_cnt; } lim = abs(lim); for (int i = res.size() - 1; i >= 0; --i) { int pop_cnt = __builtin_popcount(res[i]); if (lim >= pop_cnt) { lim -= pop_cnt; } else rere.push_back(res[i]); } cout << rere.size() << "\n"; for (auto p : rere) cout << p << " "; }
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 <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; cin >> N; int lim = N; vector<int> res; vector<int> rere; for (int i = 1; i <= N && lim > 0; ++i) { int pop_cnt = __builtin_popcount(i); res.push_back(i); lim -= pop_cnt; } lim = abs(lim); for (int i = res.size() - 1; i >= 0; --i) { int pop_cnt = __builtin_popcount(res[i]); if (lim >= pop_cnt) { lim -= pop_cnt; } else rere.push_back(res[i]); } cout << rere.size() << "\n"; for (auto p : rere) cout << p << " "; } |