fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. ll a, b;
  10. cin >> a >> b;
  11.  
  12. string s1 = to_string(a);
  13. string s2 = to_string(b);
  14. ll tmp = 1, ans = b;
  15.  
  16. for(auto x: s2){
  17. tmp *= (x - '0');
  18. }
  19.  
  20. for(int i = 0; i < (int)s2.size(); i++){
  21. if(s2[i] == '0')continue;
  22. s2[i]--;
  23. string tmps = "";
  24. ll g = 1;
  25. for(int j = 0; j < (int)s2.size(); j++){
  26. if(j > i){
  27. g *= (9);
  28. tmps += '9';
  29. }
  30. else {
  31. g *= (s2[j] - '0');
  32. tmps += s2[j];
  33. }
  34. }
  35.  
  36. if(g > tmp && (s2.size() > s1.size() || tmps >= s1)){
  37. tmp = g;
  38. ans = stoll(tmps);
  39. }
  40.  
  41. s2[i]++;
  42.  
  43. }
  44.  
  45. if(s1.size() < s2.size()){
  46. ll g = 1;
  47. string h = "";
  48. for(int i = 0; i < (int)s2.size() - 1; i++){
  49. g *= 9;
  50. h += '9';
  51. }
  52. if(g > tmp){
  53. ans = stoll(h);
  54. }
  55. }
  56.  
  57. cout << ans << "\n";
  58.  
  59.  
  60.  
  61. }
  62.  
  63. int main(){
  64. ios_base::sync_with_stdio(false);
  65. cin.tie(nullptr);
  66.  
  67. int t = 1;
  68. cin >> t;
  69.  
  70. for(int i = 1; i <= t; i++){
  71. solve();
  72. }
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5268KB
stdin
2
1 10
51 62
stdout
9
59