#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
void solve() {
int n;
cin >> n;
vector<pii> data(n+1);
data[0] = {0, 0};
for (int i = 1, x = 0;;x += __builtin_popcount(i), i++) {
for(int j = x + 1;j <= n && j <= x + __builtin_popcount(i);++j) {
data[j] = {data[j - __builtin_popcount(i)].first + 1, i};
}
if (data[n].first != 0)
break;
}
cout << data[n].first << '\n';
for (int x = data[n].second;x > 0;n -= __builtin_popcount(x), x = data[n].second)
cout << x << " ";
cout << "\n";
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
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 | #include<bits/stdc++.h> using namespace std; typedef pair<int,int> pii; void solve() { int n; cin >> n; vector<pii> data(n+1); data[0] = {0, 0}; for (int i = 1, x = 0;;x += __builtin_popcount(i), i++) { for(int j = x + 1;j <= n && j <= x + __builtin_popcount(i);++j) { data[j] = {data[j - __builtin_popcount(i)].first + 1, i}; } if (data[n].first != 0) break; } cout << data[n].first << '\n'; for (int x = data[n].second;x > 0;n -= __builtin_popcount(x), x = data[n].second) cout << x << " "; cout << "\n"; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); solve(); } |
English