#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long void solve(){ ll n,suma=0,size=1; cin >> n; vector<ll> lista = {}; vector<bool> flaga = {}; for(ll i = 1; suma < n; i++){ suma += __builtin_popcount(i); lista.push_back(i); flaga.push_back(true); } for(ll i = lista.size()-1; i > 0; i--){ if(suma > n && suma - __builtin_popcount(lista[i]) >= n && i != lista.size()-1){ suma -= __builtin_popcount(lista[i]); flaga[i] = false; } else{ size++; } } cout << size << endl; for(int i = lista.size()-1; i >= 0; i--){ if(flaga[i]){ cout << lista[i] << " "; } } cout << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("input.txt", "r", stdin); solve(); }
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 | #include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long void solve(){ ll n,suma=0,size=1; cin >> n; vector<ll> lista = {}; vector<bool> flaga = {}; for(ll i = 1; suma < n; i++){ suma += __builtin_popcount(i); lista.push_back(i); flaga.push_back(true); } for(ll i = lista.size()-1; i > 0; i--){ if(suma > n && suma - __builtin_popcount(lista[i]) >= n && i != lista.size()-1){ suma -= __builtin_popcount(lista[i]); flaga[i] = false; } else{ size++; } } cout << size << endl; for(int i = lista.size()-1; i >= 0; i--){ if(flaga[i]){ cout << lista[i] << " "; } } cout << endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("input.txt", "r", stdin); solve(); } |