classSolution { public: vvl mat_mul(vvl& A, vvl& B){ int a = A.size(), b = B.size(), c = B[0].size(); vvl C(a, vl(c, 0)); for (int i = 0; i < a; ++i) { for (int j = 0; j < c; ++j) { for (int k = 0; k < b; ++k) { (C[i][j] += A[i][k] * B[k][j]) %= p; } } } return C; }
vvl mat_pow(vvl& A, int n){ int m = A.size(); vvl B(m, vl(m, 0)); for (int i = 0; i < m; ++i) B[i][i] = 1; while (n > 0) { if (n&1) B = mat_mul(B, A); A = mat_mul(A, A); n >>= 1; } return B; }
vvl mat_pow_rec(vvl& A, int n){ if (n == 1) return A; vvl B = mat_pow_rec(A, n/2); B = mat_mul(B, B); if (n&1) returnmat_mul(A, B); return B; }
intwaysToStep(int n){ vl f = {1, 2, 4}; if (n <= 3) return f[n-1]; vvl A = {{0, 0, 1}, {1, 0, 1}, {0, 1, 1}}; vvl B = mat_pow(A, n-3); ll res = 0; for (int i = 0; i < 3; ++i) { (res += f[i] * B[i][2]) %= p; } return res; } };
python
import numpy as np
classSolution: defmat_pow(self, A, n): m = A.shape[0] B = np.eye(m, dtype=np.int64) while n > 0: if (n&1)!=0: B = np.mod(np.matmul(B, A), self.p).astype(np.int64) A = np.mod(np.matmul(A, A), self.p).astype(np.int64) n >>= 1 return B;
defwaysToStep(self, n: int) -> int: self.p = int(1e9+7) f = [1, 2, 4] if n <= 3: return f[n-1] A = np.array([[0, 0, 1], [1, 0, 1], [0, 1, 1]], dtype=np.int64) B = self.mat_pow(A, n-3) res = 0 for i inrange(3): res += f[i] * B[i][2] returnint(res%self.p)