#include <iostream> #include<vector> #include <tuple> #include <algorithm> #include <math.h> using namespace std; vector<int> Ciag; int JakaMocBitowa(int x) { int ans = 0; int power = pow(2, 29); while (power != 0) { if (x >= power) { ans++; x -= power; } power /= 2; } return ans; } int n; int main() { cin >> n; int a, suma = 0, Wynik = 0; for (int i = 1; i <= n; ++i) { a = JakaMocBitowa(i); Ciag.push_back(i); Wynik++; suma += a; if (suma == n) { //jej break; } else if (suma > n) { for (int j = i - 2; j >= 0; --j) { if (suma - JakaMocBitowa(j + 1) == n) { Ciag[j] = -1; Wynik--; break; } } break; } } cout << Wynik << endl; for (int i = Ciag.size() - 1; i >= 0; --i) { if (Ciag[i] != -1) { 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> #include<vector> #include <tuple> #include <algorithm> #include <math.h> using namespace std; vector<int> Ciag; int JakaMocBitowa(int x) { int ans = 0; int power = pow(2, 29); while (power != 0) { if (x >= power) { ans++; x -= power; } power /= 2; } return ans; } int n; int main() { cin >> n; int a, suma = 0, Wynik = 0; for (int i = 1; i <= n; ++i) { a = JakaMocBitowa(i); Ciag.push_back(i); Wynik++; suma += a; if (suma == n) { //jej break; } else if (suma > n) { for (int j = i - 2; j >= 0; --j) { if (suma - JakaMocBitowa(j + 1) == n) { Ciag[j] = -1; Wynik--; break; } } break; } } cout << Wynik << endl; for (int i = Ciag.size() - 1; i >= 0; --i) { if (Ciag[i] != -1) { cout << Ciag[i] << " "; } } } |