#include <bits/stdc++.h> using namespace std; const int N = 1e6+1; int n, cnt[N]; int bs(int k) { int l=1, r=N-1, res=-1; while (l <= r) { int m=(l+r)/2; if (cnt[m] >= k) r=m-1, res=m; else l=m+1; } assert(res != -1); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n; cnt[0]=0; for (int i=1; i<N; ++i) cnt[i]=cnt[i-1]+__builtin_popcount(i); vector<int> v; while (n) { int x=bs(n); n-=__builtin_popcount(x); v.push_back(x); } cout<<v.size()<<"\n"; for (auto u : v) cout<<u<<" "; cout<<"\n"; }
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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6+1; int n, cnt[N]; int bs(int k) { int l=1, r=N-1, res=-1; while (l <= r) { int m=(l+r)/2; if (cnt[m] >= k) r=m-1, res=m; else l=m+1; } assert(res != -1); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n; cnt[0]=0; for (int i=1; i<N; ++i) cnt[i]=cnt[i-1]+__builtin_popcount(i); vector<int> v; while (n) { int x=bs(n); n-=__builtin_popcount(x); v.push_back(x); } cout<<v.size()<<"\n"; for (auto u : v) cout<<u<<" "; cout<<"\n"; } |