Narayana's Cows Modulo — Solution & Editorial
Build the 3×3 companion matrix of the order-3 recurrence and exponentiate it in O(log N).
Complexity: O(log N)
Build the 3×3 companion matrix of the order-3 recurrence and exponentiate it in O(log N).
Complexity: O(log N)