#include <iostream> #include <bits/stdc++.h> using namespace std; int Suma(int n) { int i = 0; while(n) { if(n%2)i++; n/=2; } return i; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> seq; int bitsCount = 0; int k = 1; while(bitsCount < n) { seq.push_back(k); bitsCount+= Suma(k); k++; } int bitsToRemove = bitsCount - n; k = seq.size() - 1; while(bitsToRemove > 0) { if (bitsToRemove - Suma(seq[k]) >= 0) { bitsToRemove -= Suma(seq[k]); seq.erase(seq.begin() + k); } k--; } cout << seq.size() << endl; for(int i = seq.size()-1 ; i >= 0 ; --i) { cout << seq[i] << " "; } 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 57 | #include <iostream> #include <bits/stdc++.h> using namespace std; int Suma(int n) { int i = 0; while(n) { if(n%2)i++; n/=2; } return i; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> seq; int bitsCount = 0; int k = 1; while(bitsCount < n) { seq.push_back(k); bitsCount+= Suma(k); k++; } int bitsToRemove = bitsCount - n; k = seq.size() - 1; while(bitsToRemove > 0) { if (bitsToRemove - Suma(seq[k]) >= 0) { bitsToRemove -= Suma(seq[k]); seq.erase(seq.begin() + k); } k--; } cout << seq.size() << endl; for(int i = seq.size()-1 ; i >= 0 ; --i) { cout << seq[i] << " "; } return 0; } |