#include <bits/stdc++.h> using namespace std; int possible[130000]; int binary(int x, int low, int high) { int mid; while (low < high) { mid = (low + high)/2; if (x > possible[mid]) low = mid + 1; else high = mid; } return high; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); possible[0] = 0; int num = 0, i, power = 0, n; vector<int> numbers; cin >> n; while (power < n) { num++; i = num; while (i > 0) { if (i%2) power++; i /= 2; } possible[num] = power; } while (n>0) { num = binary(n, 1, num); numbers.push_back(num); n -= possible[num] - possible[num-1]; } cout << numbers.size() << '\n'; for (int i : numbers) cout << i << " "; }
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 | #include <bits/stdc++.h> using namespace std; int possible[130000]; int binary(int x, int low, int high) { int mid; while (low < high) { mid = (low + high)/2; if (x > possible[mid]) low = mid + 1; else high = mid; } return high; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); possible[0] = 0; int num = 0, i, power = 0, n; vector<int> numbers; cin >> n; while (power < n) { num++; i = num; while (i > 0) { if (i%2) power++; i /= 2; } possible[num] = power; } while (n>0) { num = binary(n, 1, num); numbers.push_back(num); n -= possible[num] - possible[num-1]; } cout << numbers.size() << '\n'; for (int i : numbers) cout << i << " "; } |