haechandev
Algorithms

[Baekjoon 2749] Fibonacci Number 3

Use fast matrix exponentiation to compute Fibonacci numbers with a different modulus.

August 26, 2022

This uses the same matrix exponentiation structure as Fibonacci Number 6, but asks for the remainder modulo 1,000,000.

Q = |1 1|
    |1 0|

The top-left entry of Q^(n - 1) is F(n).

I halved the exponent and squared the current matrix, multiplying by the base matrix once more for odd exponents. Only four entries need to be maintained, and every multiplication applies the modulus.

Problem: Baekjoon · Code: View on GitHub