#include <bits/stdc++.h>
using namespace std;
int n;
int it,diff;
int *arr;
int *psum;
int res;
vector <int> v;
void preprocess()
{
cin >> n;
arr = new int [n+1];
psum = new int [n+1]; psum[0] = 0;
for(int i=1; i<=n; i++) arr[i] = __builtin_popcount(i);
for(int i=1; i<=n; i++) psum[i] = psum[i-1] + arr[i];
auto x = lower_bound(psum, psum+n, n);
it = x-psum;
diff = *x-n;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
preprocess();
for(int i=it; i>0; i--)
{
if(arr[i] <= diff) diff -= arr[i];
else
{
res++;
v.push_back(i);
}
}
cout << res << '\n';
for(int i=0; i<v.size(); i++) cout << v[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 | #include <bits/stdc++.h> using namespace std; int n; int it,diff; int *arr; int *psum; int res; vector <int> v; void preprocess() { cin >> n; arr = new int [n+1]; psum = new int [n+1]; psum[0] = 0; for(int i=1; i<=n; i++) arr[i] = __builtin_popcount(i); for(int i=1; i<=n; i++) psum[i] = psum[i-1] + arr[i]; auto x = lower_bound(psum, psum+n, n); it = x-psum; diff = *x-n; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); preprocess(); for(int i=it; i>0; i--) { if(arr[i] <= diff) diff -= arr[i]; else { res++; v.push_back(i); } } cout << res << '\n'; for(int i=0; i<v.size(); i++) cout << v[i] << " "; } |
English