For any sequence with linear recursion, the exponential matrix algorithm works.
If, for example, the sequence has recurrence
a[k] = x*a[k-1] + y*a[k-2] + z*a[k-3]
for k >= 3 and the initial values a[0], a[1], a[2] , you get a triple (a[n+2], a[n+1], a[n]) by multiplying
|xyz|^n |a[2]| |1 0 0| * |a[1]| |0 1 0| |a[0]|
The matrix can be raised to power n th using exponentiation by re-squaring in steps O(log n) .
Daniel Fischer
source share