Ralph and Arithmetic
InterviewTime limit2sMemory limit1024 MB
Count the integers from 1 to n (n up to 10^18) whose decimal digits avoid every digit in a given set of minor digits.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
Ralph is a minor character in a computer game, and he is tired of standing in the shadow of the main hero. Ralph noticed something that his game and arithmetic have in common.
Ralph believes that in arithmetic some digits appear more often than others, which makes all the other digits minor. To test his hypothesis, Ralph wrote down all the minor digits and now wants to know how many numbers from to do not contain any minor digit in their decimal notation. Help him count them.
Input
The first line contains an integer ().
The second line contains an integer , the number of digits Ralph considers minor ().
The third line contains the minor digits , separated by spaces ().
Output
Print one number: the count of integers from to whose decimal notation contains no minor digits.
Hint
In the first test, every number from 1 to 9 works except 3 and 4.
In the second test, only 1, 11, and 111 work.
In the third test, every number of length 1 to 5 made only of the digits 3 and 4 works.