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: solution/2900-2999/2999.Count the Number of Powerful Integers/README_EN.md
+74-3
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,33 @@ It can be shown that there are only 2 powerful integers in this range.
72
72
73
73
<!-- solution:start -->
74
74
75
-
### Solution 1
75
+
### Solution 1: Digit DP
76
+
77
+
This problem is essentially about finding the count of numbers in the given range $[l, .., r]$ that satisfy the conditions. The count depends on the number of digits and the value of each digit. We can solve this problem using the Digit DP approach, where the size of the number has minimal impact on the complexity.
78
+
79
+
For the range $[l, .., r]$, we typically transform it into two subproblems: $[1, .., r]$ and $[1, .., l - 1]$, i.e.,
80
+
81
+
$$
82
+
ans = \sum_{i=1}^{r} ans_i - \sum_{i=1}^{l-1} ans_i
83
+
$$
84
+
85
+
For this problem, we calculate the count of numbers in $[1, \textit{finish}]$ that satisfy the conditions, and subtract the count of numbers in $[1, \textit{start} - 1]$ that satisfy the conditions to get the final answer.
86
+
87
+
We use memoization to implement Digit DP. Starting from the topmost digit, we recursively calculate the number of valid numbers, accumulate the results layer by layer, and finally return the answer from the starting point.
88
+
89
+
The basic steps are as follows:
90
+
91
+
1. Convert $\textit{start}$ and $\textit{finish}$ to strings for easier manipulation in Digit DP.
92
+
2. Design a function $\textit{dfs}(\textit{pos}, \textit{lim})$, which represents the count of valid numbers starting from the $\textit{pos}$-th digit, with the current restriction condition $\textit{lim}$.
93
+
3. If the maximum number of digits is less than the length of $\textit{s}$, return 0.
94
+
4. If the remaining number of digits equals the length of $\textit{s}$, check if the current number satisfies the condition and return 1 or 0.
95
+
5. Otherwise, calculate the upper limit of the current digit as $\textit{up} = \min(\textit{lim} ? \textit{t}[\textit{pos}] : 9, \textit{limit})$. Then iterate through the digits $i$ from 0 to $\textit{up}$, recursively call $\textit{dfs}(\textit{pos} + 1, \textit{lim} \&\& i == \textit{t}[\textit{pos}])$, and accumulate the results.
96
+
6. If $\textit{lim}$ is false, store the current result in the cache to avoid redundant calculations.
97
+
7. Finally, return the result.
98
+
99
+
The answer is the count of valid numbers in $[1, \textit{finish}]$ minus the count of valid numbers in $[1, \textit{start} - 1]$.
100
+
101
+
Time complexity is $O(\log M \times D)$, and space complexity is $O(\log M)$, where $M$ is the upper limit of the number, and $D = 10$.
76
102
77
103
<!-- tabs:start -->
78
104
@@ -82,7 +108,7 @@ It can be shown that there are only 2 powerful integers in this range.
0 commit comments