An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for k = 2, 2020 can be rearranged to form the k-palindromic integer 2002, whereas 1010 cannot be rearranged to form a k-palindromic integer.
Return the count of good integers containing n digits.
Note that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.
Example 1:
Input:n = 3, k = 5
Output:27
Explanation:
Some of the good integers are:
551 because it can be rearranged to form 515.
525 because it is already k-palindromic.
Example 2:
Input:n = 1, k = 4
Output:2
Explanation:
The two good integers are 4 and 8.
Example 3:
Input:n = 5, k = 6
Output:2468
Constraints:
1 <= n <= 10
1 <= k <= 9
Solutions
Solution 1: Enumeration + Combinatorics
Thinking
A good integer is an \(n\)-digit number whose digits match some palindrome divisible by \(k\). \(n\le 10\) forbids listing all \(n\)-digit numbers; a palindrome is fixed by its first half, about \(10^{\lceil n/2\rceil}\) of them.
Build each palindrome from the first half; if it is divisible by \(k\), add the permutations of that multiset (nonzero leading digit), using the sorted digit string as a seen key. Factorials compute \(\frac{(n-x_0)(n-1)!}{\prod x_i!}\).
We can consider enumerating all palindromic numbers of length \(n\) and checking whether they are \(k\)-palindromic numbers. Due to the properties of palindromic numbers, we only need to enumerate the first half of the digits and then reverse and append them to form the full number.
The length of the first half of the digits is \(\lfloor \frac{n - 1}{2} \rfloor\), so the range of the first half is \([10^{\lfloor \frac{n - 1}{2} \rfloor}, 10^{\lfloor \frac{n - 1}{2} \rfloor + 1})\). We can reverse the first half and append it to form a palindromic number of length \(n\). Note that if \(n\) is odd, the middle digit needs special handling.
Next, we check whether the palindromic number is \(k\)-palindromic. If it is, we count all unique permutations of the number. To avoid duplicates, we can use a set \(\textit{vis}\) to store the smallest permutation of each palindromic number that has already been processed. If the smallest permutation of the current palindromic number is already in the set, we skip it. Otherwise, we calculate the number of unique permutations of the palindromic number and add it to the result.
We can use an array \(\textit{cnt}\) to count the occurrences of each digit and use combinatorics to calculate the number of permutations. Specifically, if digit \(0\) appears \(x_0\) times, digit \(1\) appears \(x_1\) times, ..., and digit \(9\) appears \(x_9\) times, the number of permutations of the palindromic number is:
Here, \((n - x_0)\) represents the number of choices for the highest digit (excluding \(0\)), \((n - 1)!\) represents the permutations of the remaining digits, and we divide by the factorial of the occurrences of each digit to avoid duplicates.
Finally, we sum up all the permutation counts to get the final result.
Time complexity is \(O(10^m \times n \times \log n)\), and space complexity is \(O(10^m \times n)\), where \(m = \lfloor \frac{n - 1}{2} \rfloor\).