// O(log n) public static long GetValueWithMod(long n, long mod) { var m = new ModMatrixOperator(mod); var a = new long[2, 2]; a[0, 0] = a[0, 1] = a[1, 0] = 1; a = m.Pow(a, n); return(a[1, 0]); }
// O(log n) public static long GetValueWithMod(long p, long q, long a0, long a1, long n, long mod) { var m = new ModMatrixOperator(mod); var a = new long[2, 2]; a[0, 0] = p; a[0, 1] = -q; a[1, 0] = 1; a = m.Pow(a, n); var v = m.Mul(a, new[] { a1, a0 }); return(v[1]); }