#include "bits/stdc++.h" using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; int k = 0; int pom = 1; int Suma = N; vector<int> Ciag; vector<vector<int>> Index(20); while(N > pom){ k++; pom = pom * 2; } k--; int l = 0; int i = 1; while(Suma >= 0){ Index[__builtin_popcount(i)].push_back(i); Ciag.push_back(i); if(Suma - __builtin_popcount(i) < 0){ int x = __builtin_popcount(i) - Suma; l = Index[x][Index[x].size() - 1]; } Suma = Suma - __builtin_popcount(i); i++; } cout << Ciag.size() - 1 << endl; for(int i = Ciag.size() - 1; i >= 0; i--){ if(Ciag[i] != l){ cout << Ciag[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 29 30 31 32 33 34 35 36 37 38 | #include "bits/stdc++.h" using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; int k = 0; int pom = 1; int Suma = N; vector<int> Ciag; vector<vector<int>> Index(20); while(N > pom){ k++; pom = pom * 2; } k--; int l = 0; int i = 1; while(Suma >= 0){ Index[__builtin_popcount(i)].push_back(i); Ciag.push_back(i); if(Suma - __builtin_popcount(i) < 0){ int x = __builtin_popcount(i) - Suma; l = Index[x][Index[x].size() - 1]; } Suma = Suma - __builtin_popcount(i); i++; } cout << Ciag.size() - 1 << endl; for(int i = Ciag.size() - 1; i >= 0; i--){ if(Ciag[i] != l){ cout << Ciag[i] <<" "; } } } |