http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意:给你一个公式 f( x ) ,叫你输出 f(k) % m 的值 解题思路:矩阵快速求幂 #include <stdio.h> #define size 10 int n,m; struct node { int map[size][size]; }unit,init,a; node Mul(node a,node b) { node c; int i,j,k; for (i=0;i<10;i++) { for (j=0;j<10;j++) { c.map[i][j] = 0; for (k=0;k<10;k++) c.map[i][j]+=a.map[i][k]*b.map[k][j]; c.map[i][j]%=m; } } return c; } void InitData() { int i,j; for(i=0;i<10;i++) { for (j=0;j<10;j++) { if(j==i-1) init.map[i][j] = 1; else init.map[i][j] = 0; if(i==j) unit.map[i][j] = 1; else unit.map[i][j] = 0; a.map[i][j] = 0; } } for (i=9;i>=0;i--) a.map[9-i][0] = i; } int Cal() { int i = n-9; node c,p,q; p = unit,q = init; while (i!=1) { if (i%2==1) { p = Mul(p,q); i--; } else { i>>=1; q = Mul(q,q); } } c = Mul(p,q); c = Mul(c,a); return c.map[0][0]; } int main() { int i; InitData(); /*freopen("d://1.txt","r",stdin);*/ while (scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<10;i++) scanf("%d",&init.map[0][i]); printf("%d/n",Cal()); } return 0; }