Interesting numbers

2.8

5 votes
Easy-Medium, Easy-med
Problem

Let us call a number interesting if it is divisible by P and each of its digit is equal to some digit of the interesting set. You are given the interesting set and the number P, you need to calculate the count of interesting numbers between two given values L and R (inclusive).

Input:

First line of input contains 4 space separated integers P, L, R and Z. Here P, L and R are as defined in the problem statement, and Z is the size of the interesting set. Next line contains Z space separated integers, where the ith integer Fi represents the ith number of the interesting set.

Output:

Print the count of interesting numbers in the range L to R inclusive.

Constraints:

1LR1018

1P105

1Z10

0Fi9

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The three interesting numbers are 4,8 and 10.

Editor Image

?