#include <bits/stdc++.h> using namespace std; typedef long long ll; int BitCount(unsigned int u) { unsigned int uCount; uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); return ((uCount + (uCount >> 3)) & 030707070707) % 63; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> ans; ll sum = 0; for (ll i = 1; i < 1000000; i++) { sum += BitCount(i); ans.push_back(i); if (sum == n) { reverse(ans.begin(), ans.end()); break; }else if (sum > n) { ll diff = sum - n; reverse(ans.begin(), ans.end()); for(ll j = 0; j < ans.size(); j++) { if (BitCount(ans[j]) == diff) { ans.erase(ans.begin() + j); break; } } break; } } cout << ans.size() << endl; for (ll i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int BitCount(unsigned int u) { unsigned int uCount; uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111); return ((uCount + (uCount >> 3)) & 030707070707) % 63; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; cin >> n; vector<ll> ans; ll sum = 0; for (ll i = 1; i < 1000000; i++) { sum += BitCount(i); ans.push_back(i); if (sum == n) { reverse(ans.begin(), ans.end()); break; }else if (sum > n) { ll diff = sum - n; reverse(ans.begin(), ans.end()); for(ll j = 0; j < ans.size(); j++) { if (BitCount(ans[j]) == diff) { ans.erase(ans.begin() + j); break; } } break; } } cout << ans.size() << endl; for (ll i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } |