10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
45 104
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
struct matrix {
ll f[11][11];
};
int n,m;
matrix mul(matrix a,matrix b){
matrix s;
memset(s.f,0,sizeof s.f);
int i,j,k;
for(k=0;k<10;k++)
for(i=0;i<10;i++){
if(!a.f[i][k]) continue;
for(j=0;j<10;j++){
if(!b.f[k][j]) continue;
s.f[i][j]+=a.f[i][k]*b.f[k][j];
s.f[i][j]%=m;
}
}
return s;
}
matrix pow_mod(matrix a,int k){
matrix s;
memset(s.f,0,sizeof s.f);
for(int i=0;i<10;i++)
s.f[i][i]=1;
while(k){
if(k&1) s=mul(s,a);
a=mul(a,a);
k>>=1;
}
return s;
}
int main(){
while(cin>>n>>m){
int i;
matrix e;
memset(e.f,0,sizeof e.f);
for(i=0;i<10;i++)
cin>>e.f[i][0];
if(n<10){
cout<<n<<endl;
continue;
}
for(i=1;i<10;i++)
e.f[i-1][i]=1;
e=pow_mod(e,n-9);
ll ans=0;
for(i=0;i<10;i++)
ans=(ans+(9-i)*e.f[i][0])%m;
cout<<ans<<endl;
}
return 0;
}
hdu 1757 A Simple Math Problem 矩阵
原文地址:http://blog.csdn.net/hyccfy/article/details/41381171