1112. Distinct-Digit Multiples

A vault mint stamps serial numbers on its coins. A serial number is a positive integer written in ordinary decimal notation with no leading zeros, and the mint calls it tidy when no digit occurs twice in it: 1024 is tidy, while 1223 and 77 are not.

Given the largest serial number n the mint will ever print and a divisor k, count how many integers x with 1 <= x <= n are tidy and divisible by k. Because n can reach 10^15, you cannot test the numbers one by one; note that no tidy number has more than ten digits.

Example 1

Input:
n = 50, k = 5
Output:
10
Explanation:

The multiples of 5 up to 50 are 5, 10, 15, 20, 25, 30, 35, 40, 45 and 50; all of them are tidy, so the answer is 10.

Example 2

Input:
n = 100, k = 11
Output:
0
Explanation:

The multiples of 11 up to 100 are 11, 22, ..., 99, and every one of them repeats a digit, so none is tidy.

Example 3

Input:
n = 1000, k = 7
Output:
106
Explanation:

Counting the multiples of 7 up to 1000 whose digits are all different gives 106.

Constraints

1 ≤ n ≤ 1015

1 ≤ k ≤ 100

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(2^10 * 10 * k)
Space
O(2^10 * k)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…