forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
56 lines (49 loc) · 904 Bytes
/
G.cpp
File metadata and controls
56 lines (49 loc) · 904 Bytes
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
56
#include <bits/stdc++.h>
using namespace std;
long long maxProduct = -1;
string ans;
void update(string s)
{
long long product = 1;
while (s.back() == '0')
s.pop_back();
for (char c : s)
product *= c - '0';
if (product > maxProduct)
{
maxProduct = product;
ans = s;
}
}
void solve(string l, string r)
{
int i = l.size() - 1;
while (i >= 0 && l[i] == r[i])
i--;
if (i < 0)
{
update(l);
return;
}
string m = r;
for (int j = 0; j < i; j++)
m[j] = '0';
solve(m, r);
assert(m[i] != '0');
m[i]--;
for (int j = 0; j < i; j++)
m[j] = '9';
update(m);
}
int main()
{
string l, r;
cin >> l >> r;
reverse(l.begin(), l.end());
reverse(r.begin(), r.end());
while (l.size() < r.size())
l += '0';
solve(l, r);
reverse(ans.begin(), ans.end());
cout << ans << endl;
}