#include <bits/stdc++.h> using namespace std; //#define cerr if(false)cerr const int mxn = 1e6 + 3; int pref[mxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); for(int i = 1; i < mxn; i++) { pref[i] = pref[i-1] + __builtin_popcount(i); } int n; cin >> n; int lb = 0, hb = mxn-1, ans = 0; while(lb <= hb) { int mb = (lb + hb) / 2; if(pref[mb] <= n) { lb = mb + 1; ans = mb; } else { hb = mb - 1; } } //cout << ans << ' ' << pref[ans] <<'\n'; bool ok = false; if(pref[ans] < n) { ans++; ok = true; } cout << ans - ok << '\n'; for(int i = ans; i > 0; i--) { if(pref[ans] - __builtin_popcount(i) == n) { pref[ans] = n; continue; } cout << i << ' '; } 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 | #include <bits/stdc++.h> using namespace std; //#define cerr if(false)cerr const int mxn = 1e6 + 3; int pref[mxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); for(int i = 1; i < mxn; i++) { pref[i] = pref[i-1] + __builtin_popcount(i); } int n; cin >> n; int lb = 0, hb = mxn-1, ans = 0; while(lb <= hb) { int mb = (lb + hb) / 2; if(pref[mb] <= n) { lb = mb + 1; ans = mb; } else { hb = mb - 1; } } //cout << ans << ' ' << pref[ans] <<'\n'; bool ok = false; if(pref[ans] < n) { ans++; ok = true; } cout << ans - ok << '\n'; for(int i = ans; i > 0; i--) { if(pref[ans] - __builtin_popcount(i) == n) { pref[ans] = n; continue; } cout << i << ' '; } return 0; } |