#include<bits/stdc++.h> using namespace std; #define int long long int countBits(int n){ int wynik = 0; while(n){ wynik += n%2; n /= 2; } return wynik; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int x; cin>>x; int sum = 0, i = 0; while(sum < x){ i++; //cout<<i<<" "<<countBits(i)<<"\n"; sum += countBits(i); } bool czy = 0; if(sum == x){ cout<<i<<"\n"; } else{ cout<<i - 1<<"\n"; } for(int i2 = i; i2 > 0; i2--){ if((sum - countBits(i2)) != x || czy == 1){ cout<<i2<<" "; } else{ czy = 1; } } }
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<bits/stdc++.h> using namespace std; #define int long long int countBits(int n){ int wynik = 0; while(n){ wynik += n%2; n /= 2; } return wynik; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int x; cin>>x; int sum = 0, i = 0; while(sum < x){ i++; //cout<<i<<" "<<countBits(i)<<"\n"; sum += countBits(i); } bool czy = 0; if(sum == x){ cout<<i<<"\n"; } else{ cout<<i - 1<<"\n"; } for(int i2 = i; i2 > 0; i2--){ if((sum - countBits(i2)) != x || czy == 1){ cout<<i2<<" "; } else{ czy = 1; } } } |