<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>

from typing import List
from math import factorial
from collections import defaultdict

class Solution:
    def countGoodIntegers(self, n: int, k: int) -> int:
        # Precompute factorials up to 10! since n <= 10
        factorials = [1] * (n + 1)
        for i in range(1, n +1):
            factorials[i] = factorials[i-1] * i
        
        # Function to generate all n-digit palindromic numbers
        def generate_palindromes(n: int) -> List[int]:
            palindromes = []
            half = (n + 1) // 2
            start = 10**(half -1)
            end = 10**half
            for first_half in range(start, end):
                first_half_str = str(first_half)
                if n %2 ==0:
                    palindrome_str = first_half_str + first_half_str[::-1]
                else:
                    palindrome_str = first_half_str + first_half_str[:-1][::-1]
                palindrome = int(palindrome_str)
                palindromes.append(palindrome)
            return palindromes
        
        # Function to compute digit count signature
        def get_digit_signature(number: int) -> tuple:
            counts = [0]*10
            for ch in str(number):
                counts[int(ch)] +=1
            return tuple(counts)
        
        # Generate all n-digit palindromic numbers
        palindromes = generate_palindromes(n)
        
        # Filter palindromic numbers divisible by k
        valid_palindromes = [p for p in palindromes if p %k ==0]
        
        # Collect unique digit count signatures
        unique_signatures = set()
        for p in valid_palindromes:
            signature = get_digit_signature(p)
            unique_signatures.add(signature)
        
        # Function to calculate number of valid anagrams given digit counts
        def count_valid_anagrams(counts: tuple) -> int:
            total = factorials[n]
            denominator = 1
            for cnt in counts:
                denominator *= factorials[cnt]
            total_anagrams = total // denominator
            # Calculate anagrams with leading zero if zero is present
            if counts[0] >0:
                # Fix first digit as zero, then arrange the remaining digits
                # Reduce count of zero by one
                counts_with_zero_fixed = list(counts)
                counts_with_zero_fixed[0] -=1
                # If after fixing zero, first digit is zero, which is invalid, skip
                # But since we've fixed zero as the first digit, it's invalid
                # So, number of invalid anagrams is:
                denominator_invalid =1
                for cnt in counts_with_zero_fixed:
                    denominator_invalid *= factorials[cnt]
                invalid_anagrams = factorials[n-1] // denominator_invalid
                total_valid = total_anagrams - invalid_anagrams
            else:
                total_valid = total_anagrams
            return total_valid
        
        # Sum the valid anagrams for all unique signatures
        total_good_integers = 0
        for signature in unique_signatures:
            total_good_integers += count_valid_anagrams(signature)
        
        return total_good_integers

22490