Problem Description
Given two positive integers n and k, count the number of n‑digit integers (with no leading zeros) that are “good.” An integer is defined as good if its digits can be rearranged into a k‑palindromic number – a palindrome that is divisible by k. For example, when k = 2, the integer 2020 is good because it can be rearranged into the palindrome 2002, which is divisible by 2; however, 1010 is not good because no rearrangement produces a valid k‑palindrome. The task is to compute the count of such good integers with exactly n digits.
Key Insights
- A rearrangable palindrome can be analyzed by looking at the frequency counts of its digits (for an even‑length number, every digit must appear an even number of times; for odd‑length, exactly one digit appears an odd number of times).
- The no‑leading‑zero rule must hold both in the original integer and in any rearrangement that forms a valid palindrome.
- The palindrome property allows us to only “choose” the first half (and possibly the center digit) and mirror it to construct the full number.
- Instead of generating every permutation, a combinatorial dynamic programming (DP) approach can count the valid arrangements, while simultaneously accumulating the modulo value of the constructed palindrome.
- Since k is small (≤ 9) and n is bounded (n ≤ 10), even methods that conceptually “try” many possibilities remain feasible.
Space and Time Complexity
Time Complexity: The approach involves enumerating multiset distributions and using DP over half‑length positions; the worst‑case is exponential in n but n is at most 10, so it runs in constant time in practice. Space Complexity: O(n) for the DP state and recursion depth, bounded by a small constant given n ≤ 10.
Solution
We can solve the problem using the following strategy:
- Note that any palindrome of n digits can be fully determined by its first half (for even n) or by its first half combined with a single center digit (for odd n). Let halfLen be n/2 (integer division) and let mid exist for odd n.
- For each valid “half” arrangement, compute its contribution to the final palindrome number in modulo k. For position i (from 0 to halfLen–1), digit d appears in both the i‑th position from the left and the i‑th from the right. Therefore, its contribution is d * (10^(n-1-i) + 10^i) modulo k.
- If n is odd, the center digit contributes d * 10^(halfLen) modulo k.
- Use DP/backtracking to count the number of ways to fill the half positions from a multiset of digits, carefully enforcing that the very first selected digit is not 0.
- For each multiset distribution of digits that can be rearranged into a palindrome (i.e. frequency constraints are met), combine the count of valid half permutations with the valid choices (if n is odd) for the center digit. When combining, update the modulo value and check if the overall constructed palindrome is divisible by k.
- Use memoization to cache DP states. The states include the current position in half, the current accumulated modulo, and the remaining frequency counts of the digits.
- Sum the counts of complete palindromes for which the final total modulo is 0.
- Finally, sum over the counts across all valid distributions to return the final answer.
The key trick is breaking the problem into a half–build of the palindrome using digit multisets, while managing the “no leading zero” requirement and carrying the modulo k calculation. Because n is small, enumeration and combinatorial DP suffice even though the logic is somewhat intricate.