#include <bits/stdc++.h> #define ll long long #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define watch(x) cout << (#x) << " : " << x << '\n' using namespace std; const int N = (int)1e6 + 128; int get(int x) { int res = 0; while (x) res += (x & 1), x >>= 1; return res; } int pref[N]; void solve() { int n; cin >> n; int ans = 1e9, skip = -1; for (int i = 0; i <= n; i++) { int val = get(i); int l = 0, r = N - 1; while (l + 1 < r) { int m = (l + r) >> 1; if (pref[m] - val >= n) r = m; else l = m; } int res = l; while (pref[res] - val < n) res++; if (pref[res] - val != n || res < i) continue; if (ans >= res) skip = i, ans = res; } vector <int> tot; for (int i = ans; i >= 1; i--) if (i != skip) tot.push_back(i); cout << (int)tot.size() << '\n'; for (auto c : tot) cout << c << ' '; } int main() { boost; for (int i = 1; i < N; i++) pref[i] = pref[i - 1] + get(i); solve(); 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 47 48 49 50 51 52 53 54 55 | #include <bits/stdc++.h> #define ll long long #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define watch(x) cout << (#x) << " : " << x << '\n' using namespace std; const int N = (int)1e6 + 128; int get(int x) { int res = 0; while (x) res += (x & 1), x >>= 1; return res; } int pref[N]; void solve() { int n; cin >> n; int ans = 1e9, skip = -1; for (int i = 0; i <= n; i++) { int val = get(i); int l = 0, r = N - 1; while (l + 1 < r) { int m = (l + r) >> 1; if (pref[m] - val >= n) r = m; else l = m; } int res = l; while (pref[res] - val < n) res++; if (pref[res] - val != n || res < i) continue; if (ans >= res) skip = i, ans = res; } vector <int> tot; for (int i = ans; i >= 1; i--) if (i != skip) tot.push_back(i); cout << (int)tot.size() << '\n'; for (auto c : tot) cout << c << ' '; } int main() { boost; for (int i = 1; i < N; i++) pref[i] = pref[i - 1] + get(i); solve(); return 0; } |