#include <iostream> using namespace std; bool tab[1000000]; int main() { int n; cin >> n; int licz = 0, sum=0; while(sum < n) { licz++; tab[licz] = true; sum += __builtin_popcount(licz); } int m = licz; int z = licz; while(sum > n) { --m; if(__builtin_popcount(m) + n > sum) { } else { sum -= __builtin_popcount(m); tab[m] = false; z--; } } cout << z << endl; for(int i = licz; i > 0; i--) { if(tab[i] == true) { cout << 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 39 40 | #include <iostream> using namespace std; bool tab[1000000]; int main() { int n; cin >> n; int licz = 0, sum=0; while(sum < n) { licz++; tab[licz] = true; sum += __builtin_popcount(licz); } int m = licz; int z = licz; while(sum > n) { --m; if(__builtin_popcount(m) + n > sum) { } else { sum -= __builtin_popcount(m); tab[m] = false; z--; } } cout << z << endl; for(int i = licz; i > 0; i--) { if(tab[i] == true) { cout << i << " "; } } } |