#include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n; vector <int> res; int main() { qio; cin >> n; int i = 1; int sum = 0; while (sum < n) { sum += __builtin_popcount(i); i++; } if (sum == n) { cout << i - 1 << endl; for (int j = i - 1; j >= 1; j--) { cout << j << " "; } cout << endl; } else { for (int j = i - 1; j >= 1; j--) { if (sum - __builtin_popcount(j) >= n) sum -= __builtin_popcount(j); else res.push_back(j); } cout << res.size() << endl; for (int j = 0; j < res.size(); j++) cout << res[j] << " "; cout << endl; } }
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 | #include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n; vector <int> res; int main() { qio; cin >> n; int i = 1; int sum = 0; while (sum < n) { sum += __builtin_popcount(i); i++; } if (sum == n) { cout << i - 1 << endl; for (int j = i - 1; j >= 1; j--) { cout << j << " "; } cout << endl; } else { for (int j = i - 1; j >= 1; j--) { if (sum - __builtin_popcount(j) >= n) sum -= __builtin_popcount(j); else res.push_back(j); } cout << res.size() << endl; for (int j = 0; j < res.size(); j++) cout << res[j] << " "; cout << endl; } } |