#abc235d. D - Multiply and Rotate

D - Multiply and Rotate

Score : 400400 points

问题描述

我们有一个正整数 aa。另外,黑板上写有一个以 base 1010 表示的数字。 设 xx 为黑板上的数字。Takahashi 可以通过以下操作来改变这个数字。

  • 擦除 xx 并以 base 1010 写出 xx 乘以 aa 的结果。
  • xx 视作字符串并将最右侧的数字移动到开头。
    这个操作只能在 x10x \geq 10xx 不是 1010 的倍数时执行。

例如,当 a=2,x=123a = 2, x = 123 时,Takahashi 可以执行以下任一操作:

  • 擦除 xx 并写出 x×a=123×2=246x \times a = 123 \times 2 = 246
  • xx 视作字符串并把 123 的最右侧数字 3 移动到开头,将数字从 123123 改变为 312312

黑板上最初的数字是 11。请问最少需要多少次操作才能将黑板上的数字变为 NN?如果无法将数字变为 NN,则输出 1-1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a positive integer aa. Additionally, there is a blackboard with a number written in base 1010.
Let xx be the number on the blackboard. Takahashi can do the operations below to change this number.

  • Erase xx and write xx multiplied by aa, in base 1010.
  • See xx as a string and move the rightmost digit to the beginning.
    This operation can only be done when x10x \geq 10 and xx is not divisible by 1010.

For example, when a=2,x=123a = 2, x = 123, Takahashi can do one of the following.

  • Erase xx and write x×a=123×2=246x \times a = 123 \times 2 = 246.
  • See xx as a string and move the rightmost digit 3 of 123 to the beginning, changing the number from 123123 to 312312.

The number on the blackboard is initially 11. What is the minimum number of operations needed to change the number on the blackboard to NN? If there is no way to change the number to NN, print 1-1.

Constraints

  • 2a<1062 \leq a \lt 10^6
  • 2N<1062 \leq N \lt 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

aa NN

Output

Print the answer.

Sample Input 1

3 72

Sample Output 1

4

We can change the number on the blackboard from 11 to 7272 in four operations, as follows.

  • Do the operation of the first type: 131 \to 3.
  • Do the operation of the first type: 393 \to 9.
  • Do the operation of the first type: 9279 \to 27.
  • Do the operation of the second type: 277227 \to 72.

It is impossible to reach 7272 in three or fewer operations, so the answer is 44.

Sample Input 2

2 5

Sample Output 2

-1

It is impossible to change the number on the blackboard to 55.

Sample Input 3

2 611

Sample Output 3

12

There is a way to change the number on the blackboard to 611611 in 1212 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