Problem 750
Minimum Multiplications to Reach End
Medium · 20 pts · Graph, Breadth-First Search
Problem
You start with the number START. In one step you may multiply the current number by any element of the array ARR and take the result modulo 100000. Find the minimum number of steps needed to reach the number END, or -1 if it can never be reached. Every number from 0 to 99999 is a node and every multiplication is an edge of weight 1, so a breadth-first search from START finds the answer in O(100000 × N). Some values computed along the way can be larger than a 32-bit int can hold (about 2.1 × 10^9), even when the input looks small, so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
The first line contains N, START and END. The second line contains the N array elements.
Output
Print the minimum number of steps, or -1.
Constraints
1 <= N <= 10; 1 <= ARR[i] <= 10^5; 0 <= START, END < 100000
Example input
3 3 30 2 5 7
Example output
2
Explanation
With ARR = 2 5 7, START = 3 and END = 30: 3 × 2 = 6, then 6 × 5 = 30. Two steps are enough and one step cannot reach 30, so the answer is 2.