The Lucky Numbers
Time limit1sMemory limit128 MB
Count lucky numbers (digits 4 and 7 only) in [A,B], plus lucky numbers outside the range whose digit-reversal lands inside it, for B up to 10^47.
- Level
Medium6 of 10
- Topics
- Combinatorics, Math, Brute force
- Solved
- No attempts yet
Problem
John and Brus are freshmen at primary school. Their first homework is to learn some integers, and to impress their teacher they decide to memorize every lucky number in the range from to , inclusive.
As you may recall from last year's contest, the lucky digits are and ; every other digit is not lucky. A lucky number is a number whose decimal representation contains only lucky digits.
After memorizing every lucky number in , the two still have free time, so they additionally memorize each lucky number that lies outside but whose reversed number falls inside . The reversed number of is written in decimal with the order of its digits reversed. For example, the reversed number of is , and the reversed number of is .
Given the integers and , find the total number of lucky numbers that John and Brus memorize.
Input
The first line contains a single integer , the number of test cases. Each test case is a single line containing two integers and separated by a single space.
Output
For each test case, print on its own line the total number of lucky numbers that John and Brus memorize.