public static long GetFibonacciNumber(long n, long mod = 0) { long[,] matrix = new long[, ] { { 0, 1 }, { 1, 1 } }; MatrixInt result = MatrixBinPower(new MatrixInt(matrix, mod), n); return(result[1, 1]); }
public static MatrixInt MatrixBinPower(MatrixInt a, long n) { MatrixInt result = MatrixInt.IdentityMatrix(a.Size, a.Mod); while (n > 0) { if ((n & 1) != 0) { result *= a; } a *= a; n >>= 1; } return(result); }