#include <bits/stdc++.h> using namespace std; const int MAX_N = 5e5+7; int n, tab[MAX_N], s, i; vector<int> vec; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for (i = 1; s < n; i++) { tab[i] = __builtin_popcount(i); s+=tab[i]; } for (int j = i; j > 0; j--) { if (s - tab[j] >= n) s-=tab[j]; else vec.push_back(j); } cout<<vec.size()<<"\n"; for (int i = 0; i < vec.size(); i++) { cout<<vec[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 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 5e5+7; int n, tab[MAX_N], s, i; vector<int> vec; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for (i = 1; s < n; i++) { tab[i] = __builtin_popcount(i); s+=tab[i]; } for (int j = i; j > 0; j--) { if (s - tab[j] >= n) s-=tab[j]; else vec.push_back(j); } cout<<vec.size()<<"\n"; for (int i = 0; i < vec.size(); i++) { cout<<vec[i]<<" "; } } |