<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>

<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>

<ul>
	<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
	<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>

<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>

<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>

<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>

<p><strong>Output:</strong> <span class="example-io">27</span></p>

<p><strong>Explanation:</strong></p>

<p><em>Some</em> of the good integers are:</p>

<ul>
	<li>551 because it can be rearranged to form 515.</li>
	<li>525 because it is already k-palindromic.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>The two good integers are 4 and 8.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>

<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10</code></li>
	<li><code>1 &lt;= k &lt;= 9</code></li>
</ul>

class Solution:
    def countGoodIntegers(self, n: int, k: int) -> int:

        p = [0] * n
        p[0] = 1
        for i in range(1, n):
            p[i] = (p[i - 1] * 10) % k

        # @cache
        def dfs(i, remain):
            if i == (n + 1) // 2:
                possible = set()
                possible.add('')
                if remain == 0:
                    
                    return True, possible
                else:
                    return False, possible

            possible = set()
            for digit in reversed(range(10)):
                if i == 0 and digit == 0:
                    continue
                if n % 2 and i == n // 2:
                    new_remain = (remain + digit * p[i]) % k
                    result, s = dfs(i + 1, new_remain)
                    if result:
                        possible.add(str(digit))

                else:
                    new_remain = (remain + digit * p[i] + digit * p[n - i - 1]) % k
                    result, s = dfs(i + 1, new_remain)
                    if result:
                        for mid in s:
                            possible.add(str(digit) + mid + str(digit))
            
            return len(possible) > 0, possible
        
        _, result = dfs(0, 0)
        # dfs.cache_clear()
        # print(len(result))
        # print(result)

        @cache
        def count_valid_numbers(count):
            count = list(count)
            # 总的数字个数
            total_digits = n
            valid_numbers = 0

            # 选择第一个数字（非零）
            valid_starts = [i for i in range(1, 10) if count[i] > 0]
            
            # 计算 10!
            total_permutations = math.factorial(total_digits - 1)
            
            for d in valid_starts:

                count[d] -= 1

                repeated_permutations = 1
                for c in count:
                    if c > 1:
                        repeated_permutations *= math.factorial(c)
                
                # 总的合法排列数量
                valid_numbers += (total_permutations // repeated_permutations)
                # print(d, (total_permutations // repeated_permutations))

                count[d] += 1


            return valid_numbers

        seen = set()
        total = 0
        for s in result:
            count = [0] * 10
            for c in s:
                count[int(c)] += 1

            # print(s, count_valid_numbers(tuple(count)))
            # print('-' * 10)
            # print(s)
            if tuple(count) in seen:
                continue
            total += count_valid_numbers(tuple(count))
            seen.add(tuple(count))        
        return total

            

30478