很简单的矩阵构造看似很麻烦,其实直接写就ok了。
⎡⎣⎢⎢⎢⎢f(n)f(n−1)....f(n−9)⎤⎦⎥⎥⎥⎥=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢a100....0b0100c0010d0000e0000f0000g0000h0001i0000⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎡⎣⎢⎢⎢⎢f(n−1)f(n−2)....f(n−10)⎤⎦⎥⎥⎥⎥
code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
#include<time.h>
//a&3==a%4
using namespace std ;
#define ll int
#define mem(a) memset(a,0,sizeof(a))
const double eps = 1e-8;
const int maxn = 110010;
const int inf = 0x3f3f3f3f;
ll mod;
struct matrix
{
ll mat[30][30];
matrix()
{
memset(mat,0,sizeof(mat));
}
};
matrix multiply(matrix a,matrix b,int n)
{
matrix res;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j]%mod;
res.mat[i][j]%=mod;
}
}
}
return res;
}
matrix quickmi(matrix a,ll b,int n)
{
matrix E;
E.mat[0][0]=1;
matrix res;
if(b==0)
{
return E;
}
for(int i=0;i<n;i++)
{
E.mat[i][i]=1;
res.mat[i][i]=1;
}
E=multiply(a,E,n);
while(b>0)
{
if(b%2==1)
{
res=multiply(res,E,n);
}
b=b/2;
E=multiply(E,E,n);
}
return res;
}
void out(matrix res,int n)
{
for(int i=0;i<n;i++)
{
cout<<res.mat[i][0];
for(int j=1;j<n;j++)
{
cout<<" "<<res.mat[i][j];
}
cout<<endl;
}
}
int main()
{
matrix res;
matrix one;
for(int i=0;i<10;i++)
{
one.mat[i][0]=i;
}
matrix sum;
ll k,m;
while(cin>>k>>m)
{
if(k>=10)
{
for(int i=0;i<10;i++)
{
scanf("%d",&res.mat[0][i]);
if(i>=1)
res.mat[i][i-1]=1;
}
mod=m;
sum=quickmi(res,k-9,10);
//out(sum,10);
int hehe=0;
for(int i=9;i>=0;i--)
{
hehe+=(i*sum.mat[0][9-i])%mod;
hehe=hehe%mod;
}
cout<<hehe<<endl;
}
else
{
for(int i=0;i<10;i++)
{
scanf("%d",&res.mat[0][i]);
}
cout<<k<<endl;
}
}
return 0;
}