Skip to content

Commit 8c20b14

Browse files
Update cf-100886G.mdx
1 parent 1750492 commit 8c20b14

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

solutions/gold/cf-100886G.mdx

+14-6
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,50 @@ author: Daniel Zhu
66
---
77

88
<Spoiler title="Hint">
9+
910
If $l = 1$, what will the best solution always be?
11+
1012
</Spoiler>
13+
1114
## Explanation
15+
1216
### $l = 1$
17+
1318
Let's start with an example: $l = 1$ and $r = 875$. Observe that we really need to consider three numbers as viable answers: $875$, $799$, and $869$. Try to think about why, and how this generalizes!
19+
1420
### $l \neq 1$
21+
1522
We can apply a very similar approach when $l \neq 1$. In fact, it's almost identical, with one minor difference. Consider the case where $l = 855$ and $r = 875$, just as before. The only difference is we can no longer consider $799$ as a viable answer, as it is less than $l$. Again, try to think about how to generalize this!
1623

1724
## Implementation
1825

19-
**Time Complexity:** $\mathcal{O}(D^2)$
20-
21-
_Note:_ $D$ is the max number of digits (19).
26+
**Time Complexity:** $\mathcal{O}(D^2)$, where $D$ is the maximum number of digits (19).
2227

2328
<LanguageSection>
24-
2529
<CPPSection>
2630

2731
```cpp
2832
#include <bits/stdc++.h>
2933
using ll = long long;
3034
using namespace std;
31-
/** @return product of the given string's digits (-1 if empty) */
35+
36+
/** @return the product of the given string's digits (-1 if empty) */
3237
ll prod(string s) {
3338
if (!s.length()) return -1;
3439
ll res = 1;
3540
for (char c : s) { res *= c - '0'; }
3641
return res;
3742
}
43+
3844
int main() {
3945
string l, r;
4046
cin >> l >> r;
47+
4148
// pad beginning of l with zeros
4249
// l = 12, r = 132 -> l = 012, r = 132
4350
while (l.length() < r.length()) { l.insert(l.begin(), '0'); }
4451
string ans = "";
52+
4553
// i -> length of LCP (longest common prefix) of str and r
4654
bool eq = true; // whether l[0, i) = r[0, i)
4755
for (int i = 0; i <= r.length(); i++) {
@@ -58,12 +66,12 @@ int main() {
5866
cur += string(r.length() - cur.length(), '9');
5967
if (prod(cur) > prod(ans)) { ans = cur; }
6068
}
69+
6170
// remove any leading zeros from ans
6271
while (ans[0] == '0') { ans.erase(ans.begin()); }
6372
cout << ans << endl;
6473
}
6574
```
6675
6776
</CPPSection>
68-
6977
</LanguageSection>

0 commit comments

Comments
 (0)