Fibonacci Strikes Back

P, m, 그리고 P-피보나치 수열에서 F(F_n)의 낮은 k개 십진 자릿수가 주어질 때, 그 자릿수로 끝나는 F(F_n)을 갖는 m 이상의 가장 작은 n을 구하거나 존재하지 않으면 보고한다.

어려움9정수론수학이분 탐색구현아직 제출이 없습니다시간 제한3초메모리 제한512 MB

문제

In this problem, you need to solve a well-known problem about P-Fibonacci sequence:

\[F_n = \begin{cases} 0 & \text{if }n = 0 \\ 1 & \text{if } n = 1 \\ PF_{n-1} + F_{n-2} & \text{otherwise} \end{cases}\]

Now given \(P\), \(m\) and the lowest \(k\) decimal digits of \(F_{F_n}\), you are asked to determine the minimum possible \(n\) such that \(n \ge m\) and \(F_{F_n}\) has at least \(k\) lowest decimal digits as given above, or report it is impossible otherwise.

입력

The input contains several test cases. The first line contains an integer \(T\) indicating the number of test cases. The following describes all test cases. For each test case:

The only line contains two integers \(P\), \(m\) and a string of length \(k\), consisting of only digits, which represents the lowest \(k\) decimal digits of \(F_{F_n}\). Note the string may contain leading zeros.

출력

For each test case, output a line containing “Case #x: y” (without quotes), where x is the test case number starting from 1, and y is the minimum possible n to this test case if it exists, or y is −1 otherwise.

제한

  • \(1 \le T \le 10^4\)
  • \(1 \le P, m ≤ 10^{18}\)
  • \(1 \le k \le 18\)
  • The sum of \(k\) in all test cases does not exceed \(10^4\).
  • It is guaranteed that the greatest common divisor of \(P\) and \(10^{18}\) is less than \(5\) for each test case.

힌트

When \(P = 1, \{F_{F_n}\}_{n=0}^{\infty} = \{0, 1, 1, 1, 2, 5, 21, 233, 10946, 5702887, 139583862445, \dots\}\).

When \(P = 2, \{F_{F_n}\}_{n=0}^{\infty} = \{0, 1, 2, 29, 13860, 44560482149, \dots\}\).