#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; int cnt_bits[N]; int pref[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cnt_bits[i] = cnt_bits[i ^ (i & (-i))] + 1; pref[i] = pref[i - 1] + cnt_bits[i]; } int curr = n; vector<int> res; for (int i = n; i > 0; i--) { if (pref[i - 1] < curr) { res.push_back(i); curr -= cnt_bits[i]; } } cout << (int) res.size() << "\n"; for (int v : res) { cout << v << " "; } 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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; int cnt_bits[N]; int pref[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cnt_bits[i] = cnt_bits[i ^ (i & (-i))] + 1; pref[i] = pref[i - 1] + cnt_bits[i]; } int curr = n; vector<int> res; for (int i = n; i > 0; i--) { if (pref[i - 1] < curr) { res.push_back(i); curr -= cnt_bits[i]; } } cout << (int) res.size() << "\n"; for (int v : res) { cout << v << " "; } cout << "\n"; } |