
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first and only line of each test case contains the integer 'N'.
For each test case, print an integer denoting the total count of special numbers between 1 to N.
Print the output of each test case in a new line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10^4
1 <= N <= 10^9
Time Limit: 1 sec
The idea is to iterate through all the numbers that are smaller than or equal to N one by one and find the total count of special numbers. To check whether a particular number is a special number, we can check the number digit by digit to determine whether all the digits lie between 1 to 5. If yes, then the number is a special number. Otherwise, the number is not a special number.
For a d - digit number, each digit can have 5 possible values, i.e., 1, 2, 3, 4, and 5. The choice for any digit is independent of all the other digits. Therefore, by the rule of Product, we can infer that the total number of d - digit special numbers is 5^d.
Let N = N1 N2 … N(d-1) N(d) be a d - digit number.
We can easily count and add all the 1 digit, 2 digit … , d - 1 digit special numbers using the above formula. The sum obtained will be sum(5^i) for i = 1 to d - 1, which we can calculate naively or use the fact that the above sequence forms a Geometric Progression to obtain the result as (5*(5^(d-1)-1))/4. Let the obtained result be res.
Now it remains only to count the d - digit special numbers that are smaller than N, for which we will be using Digit - DP.
Our final answer will be the sum of res and specialNumbersFound variable.