矩阵快速幂模板
数据结构 struct Mat{ ll mat[3][3]; void init(){ for (int i = 0; i < 2; ++i){ for (int j = 0; j < 2; ++j){ mat[i][j] = ; } } mat[1][1] = 0; } };
乘法
Mat operator * (Mat a,Mat b){ Mat c; int i,j,k; CL(c.mat,0); for (i = 0; i < 2; ++i){ for (j = 0; j < 2; ++j){ for (k = 0; k < 2; ++k){ if (!a.mat[i][k] || !b.mat[k][j]) continue; c.mat[i][j] += a.mat[i][k]*b.mat[k][j]; if (c.mat[i][j] > MOD) c.mat[i][j] %= MOD; } } } return c; }
幂:
Mat operator ^ (Mat a,ll k){ Mat c; int i,j; //单位矩阵 for (i = 0; i < 2; ++i){ for (j = 0; j < 2; ++j){ c.mat[i][j] = (i == j); } } //求k次幂 while (k){ if (k&1) c = c*a; k >>= 1; a = a*a; } return c; }