#include <bits/stdc++.h> #define FOR(i,n) for(int i = 0; i<n; ++i) #define FOREACH(v, a) for(auto & v : (a)) #define X first #define Y second #define PR std::pair #define MPR std::make_pair #define ReversePQ(type) priority_queue<type,vector<type>, std::greater<type>> #define PQ(type) priority_queue<type> typedef long long ll; typedef std::vector<int> VI; using namespace std; #pragma region Ready functions int fastPower(int a, int b){ int res=1, pop_a = a; while(b){ if(b&1) res *= pop_a; pop_a *= pop_a; b>>=1; } return res; } #pragma endregion void solve(){ VI res; int n; cin>>n; int i = 1; int sum = 0; while(sum < n){ res.push_back(i); sum += __popcount(i); i++; } int cnt = 0; i--; while(sum > n){ if(sum - __popcount(res[i-1]) >= n){ sum -= __popcount(res[i-1]); res[i-1] = -1; cnt++; } i--; } cout<<(res.size() - cnt)<<endl; for(int j = res.size() - 1; j >= 0; j--){ if(res[j] == -1) continue; cout<<res[j]<<" "; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <bits/stdc++.h> #define FOR(i,n) for(int i = 0; i<n; ++i) #define FOREACH(v, a) for(auto & v : (a)) #define X first #define Y second #define PR std::pair #define MPR std::make_pair #define ReversePQ(type) priority_queue<type,vector<type>, std::greater<type>> #define PQ(type) priority_queue<type> typedef long long ll; typedef std::vector<int> VI; using namespace std; #pragma region Ready functions int fastPower(int a, int b){ int res=1, pop_a = a; while(b){ if(b&1) res *= pop_a; pop_a *= pop_a; b>>=1; } return res; } #pragma endregion void solve(){ VI res; int n; cin>>n; int i = 1; int sum = 0; while(sum < n){ res.push_back(i); sum += __popcount(i); i++; } int cnt = 0; i--; while(sum > n){ if(sum - __popcount(res[i-1]) >= n){ sum -= __popcount(res[i-1]); res[i-1] = -1; cnt++; } i--; } cout<<(res.size() - cnt)<<endl; for(int j = res.size() - 1; j >= 0; j--){ if(res[j] == -1) continue; cout<<res[j]<<" "; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); } |