12
26
#include<cstdio> #include<cstring> #define mod 10007 struct Matrix { int mat[4][4]; Matrix() { memset(mat, 0, sizeof(mat)); for(int i = 0; i < 4; i++) mat[i][i] = 1; } }; Matrix Multi(Matrix a, Matrix b) { Matrix res; for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { res.mat[i][j] = 0; for(int k = 0; k < 4; k++) { res.mat[i][j] += a.mat[i][k] * b.mat[k][j]; res.mat[i][j] %= mod; } } } return res; } Matrix Pow(Matrix a, int x) { Matrix res; while(x) { if(x&1) res = Multi(res, a); a = Multi(a, a); x >>= 1; } return res; } int main() { int T, n; Matrix A; //系数矩阵 A.mat[0][0] = 2; A.mat[0][1] = 1; A.mat[0][2] = 1; A.mat[0][3] = 0; A.mat[1][0] = 1; A.mat[1][1] = 2; A.mat[1][2] = 0; A.mat[1][3] = 1; A.mat[2][0] = 1; A.mat[2][1] = 0; A.mat[2][2] = 2; A.mat[2][3] = 1; A.mat[3][0] = 0; A.mat[3][1] = 1; A.mat[3][2] = 1; A.mat[3][3] = 2; scanf("%d",&T); while(T--) { scanf("%d", &n); Matrix ans = Pow(A, n); printf("%d\n", ans.mat[0][0]); } return 0; }
原文地址:http://blog.csdn.net/lyhvoyage/article/details/39525785