#include <cstdio> #include <iostream> #include <vector> using namespace std; inline int popcnt(int x) { int ret = 0; while(x) { if (x & 1) ret++; x>>=1; } return ret; } #define N 130000 int sum[N]; vector<int> ans; void solve(int i, int n) { if (n == 0) return; ans.push_back(i); n = n - popcnt(i); while (i > 0) { if (sum[i-1] < n) break; i--; } solve(i, n); } int main () { int n, i; scanf("%d", &n); for (i = 1; i <= n; i++) { sum[i] = sum[i-1] + popcnt(i); if (sum[i] >= n) break; } solve(i, n); printf("%ld\n", ans.size()); for (i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[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 47 48 49 50 51 | #include <cstdio> #include <iostream> #include <vector> using namespace std; inline int popcnt(int x) { int ret = 0; while(x) { if (x & 1) ret++; x>>=1; } return ret; } #define N 130000 int sum[N]; vector<int> ans; void solve(int i, int n) { if (n == 0) return; ans.push_back(i); n = n - popcnt(i); while (i > 0) { if (sum[i-1] < n) break; i--; } solve(i, n); } int main () { int n, i; scanf("%d", &n); for (i = 1; i <= n; i++) { sum[i] = sum[i-1] + popcnt(i); if (sum[i] >= n) break; } solve(i, n); printf("%ld\n", ans.size()); for (i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[i]); } |