#include <bits/stdc++.h> using namespace std; int dp[1000000 + 8], pop[1000000 + 8]; int ile(int a) { int ans = 0; while(a) { ans += a%2; a /= 2; } return ans; } int main() { int n; cin >> n; dp[0] = 0; dp[1] = 1; pop[1] = 1; for(int i = 1; i < n; i++) { int last = 0; int curr = dp[i] + 1; while(true) { int x1 = ile(curr); if(ile(last) > x1 || i + x1 > n) break; if(dp[i + x1] > curr || dp[i + x1] == 0) { dp[i + x1] = curr; pop[i + x1] = x1; } last = curr; curr++; } } vector<int> v; int j = n; while(j) { v.push_back(dp[j]); j -= pop[j]; } cout << v.size() << '\n'; for(auto u : v) cout << u << ' '; 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 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> using namespace std; int dp[1000000 + 8], pop[1000000 + 8]; int ile(int a) { int ans = 0; while(a) { ans += a%2; a /= 2; } return ans; } int main() { int n; cin >> n; dp[0] = 0; dp[1] = 1; pop[1] = 1; for(int i = 1; i < n; i++) { int last = 0; int curr = dp[i] + 1; while(true) { int x1 = ile(curr); if(ile(last) > x1 || i + x1 > n) break; if(dp[i + x1] > curr || dp[i + x1] == 0) { dp[i + x1] = curr; pop[i + x1] = x1; } last = curr; curr++; } } vector<int> v; int j = n; while(j) { v.push_back(dp[j]); j -= pop[j]; } cout << v.size() << '\n'; for(auto u : v) cout << u << ' '; cout << '\n'; } |