Count how many length- integers can be rearranged into a palindrome that is divisible by .
Problem
You are given two integers and .
Count how many positive integers with exactly digits are good.
An -digit integer is called good if its digits can be rearranged to form a palindrome that is divisible by .
Two integers are considered different if their decimal representations are different, even if they contain the same digits in a different order.
Notes
- The integer must have exactly digits, so it cannot start with
0. - A palindrome reads the same forward and backward.
- You only need to count integers that can be rearranged into at least one valid palindrome divisible by .
Input Format
- Two integers
nandk.
Output Format
- Return the number of good integers with exactly
ndigits.
Constraints
- Digits are decimal (
0to9).
Hints
- For a palindrome, the multiset of digits is highly constrained: at most one digit may have an odd frequency.
- Instead of checking every integer directly, think in terms of digit counts and generate only palindromic candidates.
- Once a valid multiset of digits is found, count how many -digit numbers can be formed from it without leading zeros.
Input Format
Two integers n and k.
Output Format
The count of good integers with exactly n digits.
Constraints
- The integer must have exactly digits.
- Leading zeros are not allowed in the integer representation.
Example 1
Input
n = 3, k = 2
Output
9
Explanation
The good 3-digit integers are exactly those whose digits can be rearranged into an even palindrome divisible by 2, such as 202, 212, 222, 232, 242, 252, 262, 272, and 282. All of them have digit multiset {2, x, 2} with a valid palindrome arrangement divisible by 2, giving a total of 9.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.