You are given three integers ‘A’, ‘B’ and ’K’. Your task is to find the number of Magical Numbers between ‘A’ and ‘B’(inclusive). A number ‘N’ is said to be a magical number if the sum of its digits is prime and the number ‘N’ is divisible by ‘K’.
For example:
Forgiven A = 10, B = 15 and K = 2.
12 and 14 are magical numbers because the sum of digits of 12 and 14 is a prime number, also 12 and 14 both are divisible by 2.
The first line of input contains an integer ‘T’, denoting the number of test cases. Then each test case follows.
The first and the last line of each test case contains three space-separated integers denoting ‘A’, ‘B’, and ‘K’.
Output Format:
For each test case, print a single line containing one integer, denoting the number of Magical Numbers between ‘A’ and ‘B’(inclusive)
The output of each test case will be printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= A <= B <= 10^9
1 <= K <= 100
Time Limit: 1 sec.
2
10 15 2
2 14 7
2
2
In the first test case, there are two magical numbers 12 and 14, because the sum of digits of 12 and 14 is a prime number, also 12 and 14 both are divisible by 2.
Similarly, In the second test case, 7 and 14 are magical numbers.
1
15 76 5
6
In the first test case, the numbers 20, 25, 30, 50, 65, 70 are only magical numbers.
Try to form a sequence of numbers that is less than equal to ‘X’ and keep track of sum, remainder.
Let, magicalNumbers(x, y) = magical number between x and y(inclusive)
Then, magicalNumbers(A, B) = magicalNumbers(1, B) - magicalNumbers(1, A -1).
Let's solve the above two sub-problem magicalNumbers(1, X) individually,
The idea is to use digit - dp.
Consider the global array isPrime [130 ], if ‘i’ is prime then isPrime[ i ] = 1 else 0 and 4D array for memoization ‘dp[13][130][1005][3]’.
The ‘helper’ function returns the number of magical numbers which are less than or equal to the sequence of numbers in the ‘vec’ array. It takes four-parameter (‘vec’, current index, the sum of digits till current index, the remainder of a sequence of the number formed till current index, ‘K’, ‘isSmall’ variable if it is 1, means current sequence of numbers are smaller than the given sequence in ‘vec’ else equal.
O (((log (max(A, B))) ^ 2) * K), where ‘A’, ‘B’ and ‘K’ are the given integers.
We are solving a similar problem twice, so the time complexity depends on the number which has a higher number of digits which is equal to the max of log10(‘A’) and log10(‘B’).
‘idx’ is O(log (max(A, B)) Sum is also the function of the number of digits O(log (max(A, B) * 9) and the remainder can range from 0 to ‘K’ -1 so O(‘K’) and ‘isSmall’ can take value 0 or 1.
So the overall time complexity is O(((log(max(A, B))) * (log(max(A, B)))) * K * 2) = O (((log (max(A, B))) ^ 2) * K).
O (log((max (A, B) ) ^ 2) * K ) where ‘A’, ‘B’ and ‘K’ are the given integers.
We need O( log( max(A, B) ) ) * 9 space(maximum sum of digits) for ‘isPrime’ array and O( log(max(A, B) ^ 2) * K for memoization. Hence space complexity is O(log( max(A,B)) ^2 * K) +O( log(max(A, B)) = O (log( (max(A, B) )^ 2) * K ).