Lucky Numbers
Time limit1sMemory limit128 MB
Count numbers in [A,B] up to 10^12 expressible as products of one or more lucky numbers made only of digits 4 and 7, across many queries.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Combinatorics
- Solved
- No attempts yet
Problem
John recently arrived in Bucharest, Eastern Europe, for a regional contest. John is famous for his theory of lucky numbers, which is exactly why the contestants and spectators are so glad to see him.
According to his theory, among the digits 0–9 the digits 4 and 7 are lucky, and all other digits are not. A lucky number is a number whose decimal representation consists only of lucky digits (4 or 7). A great lucky number is a number that can be written as a product of one or more lucky numbers. A lucky number by itself is also considered a great lucky number (as a product of a single lucky number). For example, 47 (), 49 (), and 112 () are all great lucky numbers.
Your task is to count the great lucky numbers that are between and , inclusive. John gives you and .
Here, a 'digit' means a single decimal digit (0–9), and a 'number' means a natural number.
Input
The first line contains an integer , the number of test cases.
Each of the next lines contains two integers and separated by a space.
Output
For each test case, print the number of great lucky numbers between and , inclusive, one per line, for a total of lines.
Hint
When and , the great lucky numbers are 4, 7, 16 (), 28 (), 44, 47, 49 (), 64 (), 74, and 77.