atcoder#ABC235D. [ABC235D] Multiply and Rotate
[ABC235D] Multiply and Rotate
Score : points
Problem Statement
We have a positive integer . Additionally, there is a blackboard with a number written in base . Let be the number on the blackboard. Takahashi can do the operations below to change this number.
- Erase and write multiplied by , in base .
- See as a string and move the rightmost digit to the beginning. This operation can only be done when and is not divisible by .
For example, when , Takahashi can do one of the following.
- Erase and write .
- See as a string and move the rightmost digit
3
of123
to the beginning, changing the number from to .
The number on the blackboard is initially . What is the minimum number of operations needed to change the number on the blackboard to ? If there is no way to change the number to , print .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
3 72
4
We can change the number on the blackboard from to in four operations, as follows.
- Do the operation of the first type: .
- Do the operation of the first type: .
- Do the operation of the first type: .
- Do the operation of the second type: .
It is impossible to reach in three or fewer operations, so the answer is .
2 5
-1
It is impossible to change the number on the blackboard to .
2 611
12
There is a way to change the number on the blackboard to in operations: $1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 611$, which is the minimum possible.
2 767090
111