矩阵快速幂
struct Matrix { int mm[3][3]; Matrix() { memset(mm, 0, sizeof(mm)); } void init(){ for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) mm[i][j] = (i == j); } Matrix operator * (const Matrix &p)const { Matrix C; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { C.mm[i][j] = 0; for (int k = 0; k < 3; ++k) C.mm[i][j] = (C.mm[i][j] + 1LL * mm[i][k] * p.mm[k][j] % MOD) % MOD; } } return C; } Matrix operator ^ (ll &p) { Matrix res, x = *this; res.init(); while (p) { if (p & 1) res = res * x; x = x * x; p >>= 1; } return res; } } ;