#include <bits/stdc++.h> using namespace std; void solve_test_case(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(); } vector<size_t> find_sequence(size_t n); void solve_test_case() { size_t bit_power_sum; cin >> bit_power_sum; auto seq = find_sequence(bit_power_sum); cout << seq.size() << "\n"; for (auto v : seq) cout << v << " "; cout << "\n"; } vector<size_t> find_sequence(size_t n) { vector<size_t> partial_sums; partial_sums.push_back(0); size_t sum = 0; size_t current = 0; while (sum < n) { current++; sum += __builtin_popcount(current); partial_sums.push_back(sum); } vector<size_t> result; for (size_t i = partial_sums.size() - 1; i > 0; i--) { if (partial_sums[i - 1] < n) { result.push_back(i); n -= __builtin_popcount(i); } } return result; }
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 44 45 46 47 48 49 50 | #include <bits/stdc++.h> using namespace std; void solve_test_case(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(); } vector<size_t> find_sequence(size_t n); void solve_test_case() { size_t bit_power_sum; cin >> bit_power_sum; auto seq = find_sequence(bit_power_sum); cout << seq.size() << "\n"; for (auto v : seq) cout << v << " "; cout << "\n"; } vector<size_t> find_sequence(size_t n) { vector<size_t> partial_sums; partial_sums.push_back(0); size_t sum = 0; size_t current = 0; while (sum < n) { current++; sum += __builtin_popcount(current); partial_sums.push_back(sum); } vector<size_t> result; for (size_t i = partial_sums.size() - 1; i > 0; i--) { if (partial_sums[i - 1] < n) { result.push_back(i); n -= __builtin_popcount(i); } } return result; } |