#abc235d. D - Multiply and Rotate
D - Multiply and Rotate
Score : points
问题描述
我们有一个正整数 。另外,黑板上写有一个以 base 表示的数字。 设 为黑板上的数字。Takahashi 可以通过以下操作来改变这个数字。
- 擦除 并以 base 写出 乘以 的结果。
- 将 视作字符串并将最右侧的数字移动到开头。
这个操作只能在 且 不是 的倍数时执行。
例如,当 时,Takahashi 可以执行以下任一操作:
- 擦除 并写出 。
- 将 视作字符串并把
123
的最右侧数字3
移动到开头,将数字从 改变为 。
黑板上最初的数字是 。请问最少需要多少次操作才能将黑板上的数字变为 ?如果无法将数字变为 ,则输出 。
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
3 72
Sample Output 1
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 .
Sample Input 2
2 5
Sample Output 2
-1
It is impossible to change the number on the blackboard to .
Sample Input 3
2 611
Sample Output 3
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.
Sample Input 4
2 767090
Sample Output 4
111
update @ 2024/3/10 10:11:51