题意:http://acm.hdu.edu.cn/showproblem.php?pid=1757
f[10]=(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9)*(9,8,7,6,5,4,3,2,1,0)'
f[11]=(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9)*(f[10],9,8,7,6,5,4,3,2,1)'
f[n]=(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9)*(f[n-1],f[n-2],f[n-3],f[n-4],f[n-5],f[n-6],f[n-7],f[n-8],f[n-9],f[n-10])'
让我们填完左边和右边的f系列对应完整看看会发生什么:
递推式找到了,最后确定原始的f系列值:
9,8,7,6,5,4,3,2,1,0
接下来写出相应的代码即可:
#include <iostream> #include<cstdio> using namespace std; int k,m; struct matrix{ int q[10][10]; }; matrix I; matrix A; matrix multi(matrix a,matrix b){ matrix c={0}; for(int i=0;i<10;i++){ for(int j=0;j<10;j++){ for(int k=0;k<10;k++){ c.q[i][j]+=(a.q[i][k]%m*(b.q[k][j]%m))%m; } } } return c; } matrix power(matrix a,int po){ matrix c=I; while(po){ if(po&1){ po--; c=multi(c,a); } po>>=1; a=multi(a,a); } return c; } int main() { //freopen("cin.txt","r",stdin); int i,j; for(i=0;i<10;i++){ for(j=0;j<10;j++){ if(i==j)I.q[i][j]=1; else I.q[i][j]=0; } } for(i=1;i<10;i++){ A.q[i][i-1]=1; } while(cin>>k>>m){ for(i=0;i<10;i++){ scanf("%d",&A.q[0][i]); } if(k>9){ matrix rem=power(A,k-9); int ans=0; for(j=0;j<10;j++){ ans+=(9-j)*rem.q[0][j]%m; } printf("%d\n",ans%m); } else printf("%d\n",k%m); } return 0; }