Problem #506
✰ - click to bookmark
★ - in your bookmarks
Tags:
unlabeled
Author of this fine problem is our colleague Clive Fraser - many thanks!
We will define a function F(n) which gives a value to any positive integer n. F(n) can only take one of the integer values from 1 to 9 inclusive. The function is evaluated using one or more prime numbers, taken in increasing order of size. So we start with the prime number 2 and then continue with 3, 5, 7, 11, 13, ... where necessary.
For a given prime p we find the remainder after dividing n by p. In other words we evaluate n % p. The value of F(n) is just the final digit of this remainder, provided that the final digit is not 0. If the final digit is 0 we move on to the next prime, and continue doing so until we get a remainder with a final digit which is not 0. The process is illustrated for n = 6720 below.
6720 % 2 = 0
6720 % 3 = 0
6720 % 5 = 0
6720 % 7 = 0
6720 % 11 = 10
6720 % 13 = 12
For each of the primes 2, 3, 5, 7 and 11 we get a remainder (after division by the prime) which has a final digit of 0. Finally we reach the prime 13 where the remainder has a final digit of 2 (so no longer 0) and hence F(6720) = 2. Note that the primes 11 and 13 have given rise to 2-digit remainders but we use only the final digit of the remainder (which must be greater than 0). The table below shows that F(1890) = 9.
1890 % 2 = 0
1890 % 3 = 0
1890 % 5 = 0
1890 % 7 = 0
1890 % 11 = 9
You can easily verify that the F(n) values for the numbers from 1 to 10 are: 1, 2, 1, 1, 1, 1, 1, 2, 1, 1.
In this problem you will be given two positive integers A and B (both less than 4 x 10^17) and are asked to find the total of all F(n) values in the range from A to B inclusive.
Input/Output description: The first and only line of the input data will consist of 2 space-separated integers A and B, corresponding to the range of values to be considered. Your answer is a single integer, which is the sum of all F(n) values in the range A to B inclusive.
Example 1:
input:
1 10
answer:
12
Example 2:
input:
12345 12345678
answer:
17964772
Example 3:
input:
38023117274 301202520622367617
answer:
438732231169461890