#include <bits/stdc++.h> using namespace std; int zapalone_bity(int x) { int pot = 0; while ((1 << pot) < x) pot++; if ((1 << pot) == x) return 1; int ilosc = 0; for (int i = 0; i < pot; i++) if (x & (1 << i)) ilosc++; return ilosc; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; stack<int>numbers; int ilosc = 0; for (int i = 1; i <= n; i++) { ilosc += zapalone_bity(i); numbers.push(i); if (ilosc >= n) break; } if (ilosc > n) { stack<int>temp; while (ilosc - zapalone_bity(numbers.top()) != n) { temp.push(numbers.top()); numbers.pop(); } numbers.pop(); while (temp.size()) { numbers.push(temp.top()); temp.pop(); } } cout << numbers.size() << '\n'; while (numbers.size()) { cout << numbers.top() << ' '; numbers.pop(); } cout << '\n'; 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 | #include <bits/stdc++.h> using namespace std; int zapalone_bity(int x) { int pot = 0; while ((1 << pot) < x) pot++; if ((1 << pot) == x) return 1; int ilosc = 0; for (int i = 0; i < pot; i++) if (x & (1 << i)) ilosc++; return ilosc; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; stack<int>numbers; int ilosc = 0; for (int i = 1; i <= n; i++) { ilosc += zapalone_bity(i); numbers.push(i); if (ilosc >= n) break; } if (ilosc > n) { stack<int>temp; while (ilosc - zapalone_bity(numbers.top()) != n) { temp.push(numbers.top()); numbers.pop(); } numbers.pop(); while (temp.size()) { numbers.push(temp.top()); temp.pop(); } } cout << numbers.size() << '\n'; while (numbers.size()) { cout << numbers.top() << ' '; numbers.pop(); } cout << '\n'; return 0; } |