#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, k, roznica, moc, j, i = 1, suma = 0; cin >> n; vector<vector<int>> ciagi; while(suma<n) { moc = 0; j = i; while(j>=1) { if(j%2==1) ++moc; j/=2; } suma += moc; ciagi.push_back({i, moc}); ++i; } roznica = suma-n; if(roznica!=0) { for(int x=ciagi.size()-1; x>-1; --x) { if(roznica==ciagi[x][1]) { ciagi.erase(ciagi.begin() + x); break; } } } k = ciagi.size(); reverse(ciagi.begin(), ciagi.end()); cout << k << '\n'; for(int m=0; m<k; ++m) { cout << ciagi[m][0] << ' '; } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, k, roznica, moc, j, i = 1, suma = 0; cin >> n; vector<vector<int>> ciagi; while(suma<n) { moc = 0; j = i; while(j>=1) { if(j%2==1) ++moc; j/=2; } suma += moc; ciagi.push_back({i, moc}); ++i; } roznica = suma-n; if(roznica!=0) { for(int x=ciagi.size()-1; x>-1; --x) { if(roznica==ciagi[x][1]) { ciagi.erase(ciagi.begin() + x); break; } } } k = ciagi.size(); reverse(ciagi.begin(), ciagi.end()); cout << k << '\n'; for(int m=0; m<k; ++m) { cout << ciagi[m][0] << ' '; } return 0; } |