#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
//const LL mod=9973;
LL n,mod;
struct matrix{
LL f[10][10];
};
matrix mul(matrix a,matrix b)
{
LL i,j,k;
matrix c;
memset(c.f,0,sizeof(c.f));
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;
c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j])%mod;//mod<1e5,a.f[i][k]*b.f[k][j]<1e10,会超int。可int就过了,数据有点水
}
}
}
return c;
}
matrix pow_mod(matrix a,LL b)
{
matrix s;
memset(s.f,0,sizeof(s.f));
for(LL i=0;i<10;i++)
s.f[i][i]=1;
while(b)
{
if(b&1)
s=mul(s,a);
a=mul(a,a);
b=b>>1;
}
return s;
}
int main()
{
while(cin>>n>>mod)
{
LL i,j,k;
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])%mod;
cout<<ans<<endl;
}
return 0;
}
/*
矩阵:
|a0 1 0 0 0 0 0 0 0 0|
|a1 0 1 0 0 0 0 0 0 0|
|a2 0 0 1 0 0 0 0 0 0|
|a3 0 0 0 1 0 0 0 0 0|
|a4 0 0 0 0 1 0 0 0 0|*|f[x-1]...f[x-10]|=|f[x] f[x-1] ... f[x-9]|
|a5 0 0 0 0 0 1 0 0 0|
|a6 0 0 0 0 0 0 1 0 0|
|a7 0 0 0 0 0 0 0 1 0|
|a8 0 0 0 0 0 0 0 0 1|
|a9 0 0 0 0 0 0 0 0 0|
*/
hdu 1757 A Simple Math Problem 矩阵基础题
最新推荐文章于 2020-03-08 19:50:52 发布