fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define all(v) ((v).begin()), ((v).end())
  5.  
  6. void BRO(void) {
  7. ios_base::sync_with_stdio(false);
  8. cin.tie(NULL);
  9. cout.tie(NULL);
  10. }
  11. template <typename T> void stop(T x){return void(cout << x << endl);}
  12. void HIRO(void) {
  13. #ifndef ONLINE_JUDGE
  14. freopen("D:\\Personal\\No\\CP\\input.txt", "r", stdin);
  15. // freopen("output.txt", "w", stdout);
  16.  
  17. #endif
  18. }
  19. const long long mod = 1e9 + 7;
  20. const int MOD = 998244353;
  21. /*
  22.   When I wrote this, only Allah and I understood what I was doing
  23.   Now, only Allah knows
  24.   */
  25.  
  26. int dp[20][2][2][2];
  27. void get_this_shit_done() {
  28. string a , b;
  29. cin >> a >> b;
  30. if(a.size() < b.size()){
  31. std::reverse(a.begin(), a.end());
  32. while(a.size() < b.size()) a.push_back('0');
  33. std::reverse(a.begin(), a.end());
  34. }
  35. // dp[idx][low][high][started]
  36. memset(dp, -1, sizeof dp);
  37. function<int(int, bool, bool, bool)> go = [&](int idx, bool low, bool high, bool started)-> int {
  38. if(idx == a.size()){
  39. return 1;
  40. }
  41. int &ret = dp[idx][low][high][started];
  42. if(~ret)
  43. return ret;
  44. ret = 0;
  45. int mn = low ? a[idx] - '0' : 0;
  46. int mx = high ? b[idx] - '0' : 9;
  47. if(not started){
  48. for(int i = mn ; i <= mx ; i++){
  49. if(i != 0)
  50. ret = max(ret, go(idx + 1, low & (i == mn), high & ( i == mx ), true) * i);
  51. else
  52. ret = go(idx + 1, low & (i == mn), high & (i == mx), false);
  53. }
  54. }else{
  55. for (int i = mn; i <= mx ; ++i) {
  56. ret = max(ret, go(idx + 1, low & (i == mn), high & ( i == mx ), started) * i);
  57. }
  58. }
  59. return ret;
  60. };
  61. int res = go(0, true, true, false);
  62. vector<int>v;
  63. function<void(int, bool, bool, bool)> build = [&](int idx, bool low, bool high, bool started) -> void {
  64. if(idx == b.size()) return;
  65. int ret = go(idx, low, high, started);
  66. int mn = low ? a[idx] - '0' : 0;
  67. int mx = high ? b[idx] - '0' : 9;
  68. if(not started){
  69. for(int i = mn ; i <= mx ; i++){
  70. if(i != 0){
  71. if(ret == go(idx + 1, low & (i == mn), high & (i == mx), true) * i){
  72. cout << i;
  73. build(idx + 1, low & (i == mn), high & (i == mx), true);
  74.  
  75. }
  76. } else {
  77. if(ret == go(idx + 1, low & (i == mn), high & (i == mx), false)) {
  78. build(idx + 1, low & (i == mn), high & (i == mx), false);
  79.  
  80. }
  81. }
  82. }
  83. } else {
  84. for (int i = mn; i <= mx ; ++i) {
  85. if(ret == go(idx + 1, low & (i == mn), high & (i == mx), started) * i) {
  86. cout << i;
  87. build(idx + 1, low & (i == mn), high & (i == mx), started);
  88.  
  89. }
  90. }
  91. }
  92. };
  93.  
  94. build(0, true, true, false);
  95. }
  96.  
  97. signed main() {
  98. BRO();
  99. HIRO();
  100. //freopen("input.txt", "r", stdin);
  101. // freopen("output.txt", "w", stdout);
  102. int t = 1 ;
  103. //cin >> t;
  104. while(t--){
  105. get_this_shit_done();
  106. }
  107. }
  108.  
Success #stdin #stdout 0s 5268KB
stdin
Standard input is empty
stdout
Standard output is empty