#include <iostream> #include <algorithm> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <cstdio> #include <queue> #include <cmath> using namespace std; const int Max = 13; struct Matrix{ int m[Max][Max]; } ; Matrix I; Matrix pag; int n,M; void init(int n){ for(int i=1;i<=n;i++) I.m[i][i]=1; } void print(Matrix c){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ printf("%d ",c.m[i][j]); } printf("\n"); } } Matrix matrixmul(Matrix a,Matrix b) //矩阵乘法 { int i,j,k; Matrix c; for (i = 1 ; i <= n; i++) for (j = 1; j <= n;j++) { c.m[i][j] = 0; for (k = 1; k <= n; k++) c.m[i][j] += (a.m[i][k] * b.m[k][j])%M; c.m[i][j] %= M; } return c; } Matrix quickpagow(int nn) { Matrix m = pag, b = I;//b是单位矩阵 while (nn >= 1) { if (nn & 1) b = matrixmul(b,m); nn = nn >> 1; m = matrixmul(m,m); } return b; } int main() { int r; while(scanf("%d %d",&r,&M) == 2){ n=10; init(n); if(r<=9){ printf("%d\n",n); continue; } memset(pag.m,0,sizeof(pag.m)); for(int i=1;i<=10;i++) scanf("%d",&pag.m[1][i]); for(int i=2;i<=10;i++){ pag.m[i][i-1] = 1; } r=r-9; Matrix A = quickpagow(r); int ans=0; for(int i=1;i<=10;i++){ ans += A.m[1][i] * (10-i); ans %= M; } printf("%d\n",ans); } return 0; }