2081. Sum of k-Mirror Numbers
Description
A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k.
- For example,
9
is a 2-mirror number. The representation of9
in base-10 and base-2 are9
and1001
respectively, which read the same both forward and backward. - On the contrary,
4
is not a 2-mirror number. The representation of4
in base-2 is100
, which does not read the same both forward and backward.
Given the base k
and the number n
, return the sum of the n
smallest k-mirror numbers.
Example 1:
Input: k = 2, n = 5 Output: 25 Explanation: The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows: base-10 base-2 1 1 3 11 5 101 7 111 9 1001 Their sum = 1 + 3 + 5 + 7 + 9 = 25.
Example 2:
Input: k = 3, n = 7 Output: 499 Explanation: The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows: base-10 base-3 1 1 2 2 4 11 8 22 121 11111 151 12121 212 21212 Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
Example 3:
Input: k = 7, n = 17 Output: 20379000 Explanation: The 17 smallest 7-mirror numbers are: 1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
Constraints:
2 <= k <= 9
1 <= n <= 30
Solutions
Solution 1: Half Enumeration + Mathematics
For a k-mirror number, we can divide it into two parts: the first half and the second half. For numbers with even length, the first and second halves are exactly the same; for numbers with odd length, the first and second halves are the same, but the middle digit can be any digit.
We can enumerate the numbers in the first half, and then construct the complete k-mirror number based on the first half. The specific steps are as follows:
- Enumerate Lengths: Start enumerating the length of the numbers from 1, until we find enough k-mirror numbers that meet the requirements.
- Calculate the Range of the First Half: For a number of length \(l\), the range of the first half is \([10^{(l-1)/2}, 10^{(l+1)/2})\).
- Construct k-Mirror Numbers: For each number \(i\) in the first half, if the length is even, use \(i\) directly as the first half; if the length is odd, divide \(i\) by 10 to get the first half. Then reverse the digits of the first half and append them to form the complete k-mirror number.
- Check k-Mirror Numbers: Convert the constructed number to base \(k\) and check whether it is a palindrome.
- Accumulate the Result: If it is a k-mirror number, add it to the result and decrease the counter \(n\). When \(n\) reaches 0, return the result.
The time complexity mainly depends on the length being enumerated and the range of the first half. Since the maximum value of \(n\) is 30, the number of enumerations is limited in practice. The space complexity is \(O(1)\), since only a constant amount of extra space is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
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 |
|
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 |
|
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 |
|
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 |
|