#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int siz=10; // max size of the matrix,
LL k,mod;
struct mut
{
LL mat[siz][siz]; // value of the matrix
mut(){
memset(mat,0,sizeof(mat));
}
void init(LL v){
for(int i=0; i<=siz; ++i)
mat[i][i]=v;
}
} a,b,c;
mut operator * (mut a,mut b) // * of matrix
{
mut c;
for(int i=0; i<siz; ++i)
{
for(int j=0; j<siz; ++j)
{
c.mat[i][j]=0;
for(int k=0; k<siz; ++k)
{
c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod;
c.mat[i][j]%=mod;
}
}
}
return c;
}
mut operator ^(mut a,LL n) // ^ of matrix
{
mut c;
c.init(1);
while(n){
if(n&1) c=a*c;
a=a*a;
n>>=1;
}
return c;
}
void init() //init
{
for(int i=0; i<10; ++i)
a.mat[9-i][0]=i;
for(int i=0; i<9; ++i)
b.mat[i+1][i]=1;
}
int main(){
init();
while(~scanf("%lld %lld",&k,&mod)){
for(int i=0; i<10; ++i){
scanf("%lld",&b.mat[0][i]);
}
if(k<10) printf("%lld\n",k%mod);
else{
c=b^(k-9);
c=c*a;
printf("%lld\n",c.mat[0][0]%mod);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1757 A Simple Math Problem (矩阵快速幂)
原文地址:http://blog.csdn.net/u013050857/article/details/48092103