Under construction

Still being built. Things may change

Game

Interview

Time limit1sMemory limit1024 MB

Summary
Sum g(N) for every N from L to R, where g is 1 if repeatedly applying f reaches a fixed point, 0 if it never does, and -1 if a value exceeds 100000.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

Rang, who is good at math, learned a number game from her butler today. The game starts with a positive integer NN as the initial value. Let AA be the sum of the digits of NN, and let BB be the product of the digits of NN. Then f(N)f(N) is the number formed by writing AA and BB one after another. For example, if N=12352N = 12352, the digit sum is 1+2+3+5+2=131 + 2 + 3 + 5 + 2 = 13 and the digit product is 1×2×3×5×2=601 \times 2 \times 3 \times 5 \times 2 = 60, so f(N)f(N) is 13601360. Applying the same operation to 13601360 gives 1+3+6+0=101 + 3 + 6 + 0 = 10 and 1×3×6×0=01 \times 3 \times 6 \times 0 = 0, so f(1360)=100f(1360) = 100. Applying it to 100100 gives f(100)=10f(100) = 10, and applying it to 1010 gives f(10)=10f(10) = 10. In this game, given NN, the important question is whether repeatedly computing f(N)f(N), f(f(N))f(f(N)), f(f(f(N)))f(f(f(N))), ⋯\cdots eventually reaches some xx with f(x)=xf(x) = x.

If repeatedly applying the operation to NN reaches some xx with x=f(x)x = f(x), let g(N)=1g(N) = 1. If it never does, let g(N)=0g(N) = 0. However, if any of N,f(N),f(f(N)),⋯N, f(N), f(f(N)), \cdots is greater than 100 000100\,000, the computation is hard, so g(N)=−1g(N) = -1. For example, when N=1N = 1, we get f(1)=11f(1) = 11, f(11)=21f(11) = 21, f(21)=32f(21) = 32, f(32)=56f(32) = 56, f(56)=1130f(56) = 1130, f(1130)=50f(1130) = 50, and f(50)=50f(50) = 50. Since f(x)=xf(x) = x holds at 5050, g(1)=1g(1) = 1.

Given positive integers LL and RR (L≤RL \le R), write a program that computes g(L)+g(L+1)+⋯+g(R)g(L) + g(L + 1) + \cdots + g(R).

Input

The first line contains positive integers LL and RR, separated by a space. (1≤L≤R≤100 0001 \le L \le R \le 100\,000)

Output

Print g(L)+g(L+1)+⋯+g(R)g(L) + g(L + 1) + \cdots + g(R).

Examples1

  1. Example 1

    Input
    1 10
    
    Expected output
    9