Himanshu and Luck

3.5

173 votes
Binary search algorithm, Hiring, Recursion, Brute-force search, Easy-Medium, Ready, Approved, Backtracking
Problem

Himanshu is quite a lucky person, or atleast he claims to be. Recently, he has started some research work in college and guess what is he researching on? Lucky Numbers! While doing his bit of research, a fellow colleague of him asked him a pretty awkward question : "Given a range as input, find the number of lucky numbers lying in that range!". Himanshu somehow solved this problem, but now he has a bigger challenge for you : "There are Q queries, each of which contains a range. Find out the number of lucky numbers lying in each range (inclusive of end points)." Are you up for this hack?

Note : A number is a lucky number if it contains 4 or 7, or both in its decimal representation.

Examples:
1) 147 is not a Lucky Number as it contains 1 as an extra element, which violates the property of Lucky Numbers.
2) 47, 44, 77 , 447, 774, 4, 7 are the examples of Lucky number.

Input Format
First line contains T, the number of test cases. Each of the next T lines contains two space separated integers L and R, depicting the range.

Output Format

For each of the T lines, output one integer : The answer as specified.

Constraints

1 <= T <= 10000

1 <= L <= R <= 10^18

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

a) Between 1 and 4, there is only 1 Lucky number, i.e 4.
b) Between 2 and 3, there are no Lucky Numbers.
c) Between 1 and 1, there are no Lucky Numbers.

Editor Image

?