#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
typedef unsigned long long ull;
#define pb push_back
#define mp make_pair
vector<int> liczby;
int na_bin(int n) {
int res = 0;
while(n > 0) {
if(n % 2 == 1) res++;
n /= 2;
}
return res;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
int sum = 0, i = 1;
while(sum < n) {
sum += na_bin(i);
++i;
if(sum == n) break;
}
//cout << i - 1 << ' ';
liczby.pb(i - 1);
i -= 2;
while(i >= 1) {
if(sum != n && sum - na_bin(i) >= n) {
sum -= na_bin(i);
}
else liczby.pb(i);
--i;
}
cout << liczby.size() << endl;
for(int i = 0; i < liczby.size(); ++i) cout << liczby[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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef string str; typedef unsigned long long ull; #define pb push_back #define mp make_pair vector<int> liczby; int na_bin(int n) { int res = 0; while(n > 0) { if(n % 2 == 1) res++; n /= 2; } return res; } int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; int sum = 0, i = 1; while(sum < n) { sum += na_bin(i); ++i; if(sum == n) break; } //cout << i - 1 << ' '; liczby.pb(i - 1); i -= 2; while(i >= 1) { if(sum != n && sum - na_bin(i) >= n) { sum -= na_bin(i); } else liczby.pb(i); --i; } cout << liczby.size() << endl; for(int i = 0; i < liczby.size(); ++i) cout << liczby[i] << ' '; } |
English