Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef int64_t i64;
- int sz;
- i64 a, b;
- i64 ans, maxAns;
- string lo, hi;
- pair<i64, string> dp[19][2][2][2];
- bool calc[19][2][2][2];
- pair<i64, string> solve(int idx, bool st, bool canLo, bool canHi, i64 val) {
- if (idx == sz) return {1LL, ""};
- if (calc[idx][st][canLo][canHi])
- return dp[idx][st][canLo][canHi];
- pair<i64, string> ans = {-1, ""};
- int l = canLo ? 0 : lo[idx] - '0';
- int r = canHi ? 9 : hi[idx] - '0';
- for (int i = l; i <= r; ++i) {
- bool hasStarted = st | (i != 0);
- auto res = solve(idx + 1, hasStarted, canLo | (l < i), canHi | (i < r), val * (hasStarted ? i : 1));
- ans = max(ans, {(hasStarted ? i : 1) * res.first, to_string(i) + res.second});
- }
- calc[idx][st][canLo][canHi] = true;
- return dp[idx][st][canLo][canHi] = ans;
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- cin >> a >> b;
- lo = to_string(a);
- hi = to_string(b);
- sz = max(lo.size(), hi.size());
- while (lo.size() < sz) lo = '0' + lo;
- while (hi.size() < sz) hi = '0' + hi;
- string ans = solve(0, 0, 0, 0, 1).second;
- int cnt = 0;
- while (ans[cnt] == '0') ++cnt;
- cout << ans.substr(cnt) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment