You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solutions/gold/cf-100886G.mdx
+14-6
Original file line number
Diff line number
Diff line change
@@ -6,42 +6,50 @@ author: Daniel Zhu
6
6
---
7
7
8
8
<Spoilertitle="Hint">
9
+
9
10
If $l = 1$, what will the best solution always be?
11
+
10
12
</Spoiler>
13
+
11
14
## Explanation
15
+
12
16
### $l = 1$
17
+
13
18
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
+
14
20
### $l \neq 1$
21
+
15
22
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!
16
23
17
24
## Implementation
18
25
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).
22
27
23
28
<LanguageSection>
24
-
25
29
<CPPSection>
26
30
27
31
```cpp
28
32
#include<bits/stdc++.h>
29
33
using ll = longlong;
30
34
usingnamespacestd;
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) */
32
37
ll prod(string s) {
33
38
if (!s.length()) return-1;
34
39
llres=1;
35
40
for (charc : s) { res *= c - '0'; }
36
41
return res;
37
42
}
43
+
38
44
int main() {
39
45
string l, r;
40
46
cin >> l >> r;
47
+
41
48
// pad beginning of l with zeros
42
49
// l = 12, r = 132 -> l = 012, r = 132
43
50
while (l.length() < r.length()) { l.insert(l.begin(), '0'); }
44
51
string ans = "";
52
+
45
53
// i -> length of LCP (longest common prefix) of str and r
0 commit comments