#include <bits/stdc++.h> const int maxN = 1e6; int firstExceedsI(int t[], int toExceed) { int left = 1, right = toExceed+1; int result; while (left != right) { int mid = (left + right) / 2; if (t[mid] >= toExceed) { result = mid; right = mid; } else { left = mid + 1; } } return result; } int t[1000111]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= maxN; i++) { t[i] = __builtin_popcount(i); } for (int i = 1; i <= n; i++) { t[i] += t[i-1]; } std::vector<int> result; int remaining = n; while (remaining > 0) { int i = firstExceedsI(t, remaining); result.push_back(i); remaining -= (t[i] - t[i-1]); } printf("%lu\n", result.size()); for (auto it : result) printf("%d ", it); return 0; }
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 51 52 53 | #include <bits/stdc++.h> const int maxN = 1e6; int firstExceedsI(int t[], int toExceed) { int left = 1, right = toExceed+1; int result; while (left != right) { int mid = (left + right) / 2; if (t[mid] >= toExceed) { result = mid; right = mid; } else { left = mid + 1; } } return result; } int t[1000111]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= maxN; i++) { t[i] = __builtin_popcount(i); } for (int i = 1; i <= n; i++) { t[i] += t[i-1]; } std::vector<int> result; int remaining = n; while (remaining > 0) { int i = firstExceedsI(t, remaining); result.push_back(i); remaining -= (t[i] - t[i-1]); } printf("%lu\n", result.size()); for (auto it : result) printf("%d ", it); return 0; } |