#include <bits/stdc++.h> #define endl '\n' #define pii pair<int, int> #define st first #define nd second using namespace std; int count_ones(int x){ int counter = 0; while(x != 0){ x = x&(x-1); counter++; } return counter; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pii> v; int i=0; int ones = 0; while(true){ i++; int tmp = count_ones(i); ones += tmp; v.push_back(make_pair(i, tmp)); if(ones >= n){ break; } } vector<int> res; for(int i=v.size()-1; i>=0; i--){ if(v[i].nd > (ones)-n){ res.push_back(v[i].st); } else { ones -= v[i].nd; } } cout << res.size() << endl; for(int i=0; i<res.size(); i++){ cout << res[i] << " "; } cout << endl; 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 45 46 47 48 49 50 51 52 53 54 55 56 | #include <bits/stdc++.h> #define endl '\n' #define pii pair<int, int> #define st first #define nd second using namespace std; int count_ones(int x){ int counter = 0; while(x != 0){ x = x&(x-1); counter++; } return counter; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pii> v; int i=0; int ones = 0; while(true){ i++; int tmp = count_ones(i); ones += tmp; v.push_back(make_pair(i, tmp)); if(ones >= n){ break; } } vector<int> res; for(int i=v.size()-1; i>=0; i--){ if(v[i].nd > (ones)-n){ res.push_back(v[i].st); } else { ones -= v[i].nd; } } cout << res.size() << endl; for(int i=0; i<res.size(); i++){ cout << res[i] << " "; } cout << endl; return 0; } |