#include<bits/stdc++.h> using namespace std; const int MAXN = 1e6+7; int ileBitDo[MAXN], n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= n; ++i) { ileBitDo[i] = ileBitDo[i-1] + __builtin_popcount(i); } int q = n; vector<int> res; int l = 1, r = n; while(q) { l = 1; while(l < r) { int mid = (l+r) >> 1; if(ileBitDo[mid] < q) l = mid+1; else r = mid; } res.push_back(l); q -= __builtin_popcount(l); } cout << res.size() << '\n'; for(int s : res) cout << s << ' '; }
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 | #include<bits/stdc++.h> using namespace std; const int MAXN = 1e6+7; int ileBitDo[MAXN], n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for(int i = 1; i <= n; ++i) { ileBitDo[i] = ileBitDo[i-1] + __builtin_popcount(i); } int q = n; vector<int> res; int l = 1, r = n; while(q) { l = 1; while(l < r) { int mid = (l+r) >> 1; if(ileBitDo[mid] < q) l = mid+1; else r = mid; } res.push_back(l); q -= __builtin_popcount(l); } cout << res.size() << '\n'; for(int s : res) cout << s << ' '; } |