#include <bits/stdc++.h> using namespace std; bool res[2000000]; int main() { int sum = 0, length = 0, n, i = 1; cin >> n; while (sum < n) { sum += __builtin_popcount(i); res[i] = true; i++; length++; } int excess = sum - n; i--; int tmp; for (int j = i; j > 0 && excess > 0; --j) { tmp = __builtin_popcount(j); if (excess >= tmp) { excess -= tmp; length--; res[j] = false; } } cout << length << "\n"; for (int j = i; j > 0; --j) { if (res[j]) cout << j << " "; } }
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 | #include <bits/stdc++.h> using namespace std; bool res[2000000]; int main() { int sum = 0, length = 0, n, i = 1; cin >> n; while (sum < n) { sum += __builtin_popcount(i); res[i] = true; i++; length++; } int excess = sum - n; i--; int tmp; for (int j = i; j > 0 && excess > 0; --j) { tmp = __builtin_popcount(j); if (excess >= tmp) { excess -= tmp; length--; res[j] = false; } } cout << length << "\n"; for (int j = i; j > 0; --j) { if (res[j]) cout << j << " "; } } |