#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; int sum = 0; int last = 0; queue<int> out; while (sum < n) { last++; sum += __builtin_popcount(last); } //cout << last << " "; out.push(last); n -= __builtin_popcount(last); sum -= __builtin_popcount(last); last--; while (n > 0) { while (sum >= n) { sum -= __builtin_popcount(last); last--; } //cout << last + 1 << " "; out.push(last + 1); n -= __builtin_popcount(last + 1); } cout << out.size() << '\n'; while(!out.empty()) { cout << out.front() << " "; out.pop(); } cout << '\n'; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; int sum = 0; int last = 0; queue<int> out; while (sum < n) { last++; sum += __builtin_popcount(last); } //cout << last << " "; out.push(last); n -= __builtin_popcount(last); sum -= __builtin_popcount(last); last--; while (n > 0) { while (sum >= n) { sum -= __builtin_popcount(last); last--; } //cout << last + 1 << " "; out.push(last + 1); n -= __builtin_popcount(last + 1); } cout << out.size() << '\n'; while(!out.empty()) { cout << out.front() << " "; out.pop(); } cout << '\n'; return 0; } |