Fibonacci using Matrix Exponentiation

The Fibonacci recurrence can be written as

Since

After computing , the answer is simply the element at position .

long long fibonacci(long long n) {
    if (n == 0) return 0;
 
    Matrix M(2);
    M.a = {
        {1, 1},
        {1, 0}
    };
 
    Matrix res = matrix_power(M, n - 1);
    return res.a[0][0];
}