#include<bits/stdc++.h> using namespace std; int cnt(int x) { int res = 0; while(x > 0) { res += x&1; x = x >> 1; } return res; } int main(){ ios_base::sync_with_stdio(0); int n; cin >> n; int sum =0; vector<int> candidats; for(int k=1;sum < n;k++) { //cout << k <<endl; sum += cnt(k); candidats.push_back(k); } vector<int> res; for(int i=candidats.size()-1;sum >= n && i >=0;i--) { //cout << i << endl; //cout << sum << " " << candidats[i] << " " << cnt(candidats[i]) <<endl; if(sum - cnt(candidats[i]) >= n) { sum -=cnt(candidats[i]); } else res.push_back(candidats[i]); } cout << res.size() <<endl; for(int t:res) cout << t << " "; }
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 | #include<bits/stdc++.h> using namespace std; int cnt(int x) { int res = 0; while(x > 0) { res += x&1; x = x >> 1; } return res; } int main(){ ios_base::sync_with_stdio(0); int n; cin >> n; int sum =0; vector<int> candidats; for(int k=1;sum < n;k++) { //cout << k <<endl; sum += cnt(k); candidats.push_back(k); } vector<int> res; for(int i=candidats.size()-1;sum >= n && i >=0;i--) { //cout << i << endl; //cout << sum << " " << candidats[i] << " " << cnt(candidats[i]) <<endl; if(sum - cnt(candidats[i]) >= n) { sum -=cnt(candidats[i]); } else res.push_back(candidats[i]); } cout << res.size() <<endl; for(int t:res) cout << t << " "; } |