#include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; vector<int> bitsum; int bitnum(int n){ int ans = 0; while(n){ ans += n%2; n /= 2; } return ans; } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; bitsum.resize(n+1); int rem = n; for(int i = 1; i <= n; i++){ bitsum[i] = bitsum[i-1] + bitnum(i); } vector<int> ans; for(int i = n; i > 0; i--){ if (rem > bitsum[i-1]){ ans.push_back(i); rem -= bitnum(i); } } cout << ans.size() << endl; for(int i = 0; i < ans.size(); i++){ cout << ans[i] << " "; } cout << "\n"; }
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 | #include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; vector<int> bitsum; int bitnum(int n){ int ans = 0; while(n){ ans += n%2; n /= 2; } return ans; } int main(){ cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; bitsum.resize(n+1); int rem = n; for(int i = 1; i <= n; i++){ bitsum[i] = bitsum[i-1] + bitnum(i); } vector<int> ans; for(int i = n; i > 0; i--){ if (rem > bitsum[i-1]){ ans.push_back(i); rem -= bitnum(i); } } cout << ans.size() << endl; for(int i = 0; i < ans.size(); i++){ cout << ans[i] << " "; } cout << "\n"; } |