luogu#P4994. 终于结束的起点
终于结束的起点
Background
The starting point that finally ends.
Finally writing the period.
Finally we say goodbye.
Finally we return to the starting point again.
……
An OIer’s contest journey always starts with a NOIp, and most likely also ends with a NOIp, as if the same cycle keeps playing again and again.
If this NOIp is your starting point, then may your OI journey bloom as brilliantly as summer flowers.
If this NOIp is your ending point, then may your OI memories shine like countless stars.
Maybe this is your last time competing on Luogu, maybe not.
But no matter what, good luck in the contest one week later.
Of course, this problem is also related to cycles.
Problem Description
The well-known Fibonacci sequence is computed as follows.
$$\mathrm{fib}(n)=\begin{cases} 0,& n=0 \\ 1,& n=1 \\ \mathrm{fib}(n-1) + \mathrm{fib}(n-2),& n>1 \end{cases}$$That is, , where each term is the sum of the previous two terms.
Little F discovered that if we take every term of the Fibonacci sequence modulo any positive integer greater than , the sequence will always become periodic.
Of course, Little F soon understood why: () and () have at most possible pairs of values, so a cycle must appear within steps.
Even more generally, we can prove that no matter what modulus we choose, the Fibonacci sequence modulo will eventually look like .
Now, given a modulus , please find the smallest such that and .
Input Format
Input one line containing a positive integer .
Output Format
Output one line containing a positive integer .
2
3
6
24
Hint
Explanation for Sample 1
The Fibonacci sequence is . After taking modulo , it becomes .
We can see that when , and , which is exactly what we need, and it is the smallest such .
Constraints
For of the testdata, .
For of the testdata, .
For of the testdata, .
Notes
If you still do not know what modulo means, I would be happy to tell you. Modulo means taking the remainder of integer division, that is, the part that “cannot be divided evenly” at the end of long division. In other words,
$$a \bmod M =k \iff a = bM + k\ (M > 0, 0 \leq k < M)$$where , , and are all non-negative integers.
If you use C / C++, you can use % for modulo.
If you use Pascal, you can use mod for modulo.
Translated by ChatGPT 5