#include <bits/stdc++.h> using namespace std; int n, suma, r, x, k; int dp[1000001]; vector <int> w; int main() { cin >> n; dp[1]=1; k=0; x=2; suma=1; while (suma<n) { if (x%(1<<k)==0) { dp[x]=1; k++; } else { dp[x]=dp[(1<<k)]+dp[x%(1<<k)]; } suma+=dp[x]; x++; } r=suma-n; for (int i=x-1;i>=1;i--) { if (r==0) w.push_back(i); else { if(dp[i]>r) w.push_back(i); else r-=dp[i]; } } cout << w.size() << '\n'; for (int i=0;i<w.size();i++) cout <<w[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 | #include <bits/stdc++.h> using namespace std; int n, suma, r, x, k; int dp[1000001]; vector <int> w; int main() { cin >> n; dp[1]=1; k=0; x=2; suma=1; while (suma<n) { if (x%(1<<k)==0) { dp[x]=1; k++; } else { dp[x]=dp[(1<<k)]+dp[x%(1<<k)]; } suma+=dp[x]; x++; } r=suma-n; for (int i=x-1;i>=1;i--) { if (r==0) w.push_back(i); else { if(dp[i]>r) w.push_back(i); else r-=dp[i]; } } cout << w.size() << '\n'; for (int i=0;i<w.size();i++) cout <<w[i]<<" "; } |