Ctrl K

Shuffle into a Divisible Palindrome

Hard
You are given numbers digits and divisor. Call a number suitable when its digits can be rearranged into a number that: - is a palindrome, meaning it reads the same left to right and right to left; - is divisible by divisor. Neither the original number nor the rearranged one may start with a zero. For example, with divisor = 2 the number 2020 is suitable (it becomes 2002), while 1010 is not: 0110 starts with a zero and 1001 is odd. How many suitable numbers have exactly digits digits?
Example 1
digits=2, divisor=3 → 3

The two-digit palindromes are 11, 22, …, 99; of these 33, 66 and 99 are divisible by 3. Their digits cannot be rearranged any other way, so the answer is 3.

Example 2
digits=1, divisor=1 → 9

Every one-digit number from 1 to 9 is already a palindrome, and all are divisible by 1.

Example 3
digits=3, divisor=2 → 108

For instance, 112 counts: it becomes 212, which is even. But 110 does not: the only palindrome from its digits is 101, which is odd, and 011 starts with a zero.

Constraints
  • 1 ≤ digits ≤ 10
  • 1 ≤ divisor ≤ 9
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHeaviest Picks of Distinct Letters HardPair GCDs by Sorted Position HardThe Middle as the Sole Favorite HardThe Middle as the Sole Favorite (Large Input) HardThe K-th Odd-Even Zigzag Hard
loading Python…
Input
digits=2, divisor=3
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 59 tests on the server, hidden ones included.