#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; int policz(int x){ int ans = 0; for(int i = 0; i <= 30; i++){ if(x&(1<<i)) ans++; } return ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; int ans = 0; int counter = 0; while (ans < x){ counter++; ans += policz(counter); } if(ans == x){ cout << counter << '\n'; for(int i = counter; i >= 1; i--) cout << i << ' '; } else{ cout << counter-1 << '\n'; int do_odjecia = ans-x; bool odjete = false; for(int i = counter; i >= 1; i--){ if(policz(i) == do_odjecia && !odjete) odjete = true; else cout << i << ' '; } } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17
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 <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; int policz(int x){ int ans = 0; for(int i = 0; i <= 30; i++){ if(x&(1<<i)) ans++; } return ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; int ans = 0; int counter = 0; while (ans < x){ counter++; ans += policz(counter); } if(ans == x){ cout << counter << '\n'; for(int i = counter; i >= 1; i--) cout << i << ' '; } else{ cout << counter-1 << '\n'; int do_odjecia = ans-x; bool odjete = false; for(int i = counter; i >= 1; i--){ if(policz(i) == do_odjecia && !odjete) odjete = true; else cout << i << ' '; } } return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |