Fibonacci Sequence

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

The Fibonacci sequence is a well known sequence of numbers in which

F(n)={0,if n=01,if n=1F(n2)+F(n1),if n2

Given a number N (1N1019), find the Nth Fibonacci number, modulo 1000000007 (=109+7).

Note: For 30% of the marks of this problem, it is guaranteed that (1N1000000).

Input Specification

The first line of input will have the number N.

Output Specification

The Nth Fibonacci number, modulo 1000000007 (=109+7).

Sample Input

Copy
26

Sample Output

Copy
121393

Comments


  • 0
    _va  commented on March 31, 2024, 4:48 a.m.

    it's harder than i imgine. i just AC 1 test.


  • 47
    FatalEagle  commented on Sept. 16, 2014, 1:16 a.m. edit 2

    The problem requires you to output mod 1000000007. That means that you need to get the remainder of the final answer when it is divided by 1000000007. This operation can be done in Python, C++, and Java by using %.

    Copy
    Example:
    999999999999999999999999 mod 1000000007
    is 999999999999999999999999 % 1000000007
    which is equal to 48999999.
    Because 999999999999999999999999 = 999999993000000 * 1000000007 + 48999999,
    48999999 is 999999999999999999999999 (mod 1000000007).

    It may interest you to know that 1000000007 is a prime number.

    There are two highly useful properties of modular arithmetic we often use in programming competitions (% has the same precedence as multiplication and division):

    (a+b)modm((amodm)+(bmodm))modm(a×b)modm((amodm)×(bmodm))modm


  • 26
    quantum  commented on Sept. 13, 2014, 8:30 p.m. edited

    This problem is a lot more difficult than it may appear. There is a reason for 15 points. The input can be as big as 1019.