AC代码
#include <set>
#include <queue>
#include<algorithm>
#include <iostream>
#include<string.h>
#include<map>
#include<stack>
#include<string>
#include <stdio.h>
#define max 10
using namespace std;
int vec[10];
struct matrix{
int a[max][max];
};
int k,m;
matrix mul(matrix a,matrix b){
matrix temp;
for(int i=0;i<max;i++){
for(int j=0;j<max;j++){
temp.a[i][j]=0;
for(int k=0;k<10;k++){
temp.a[i][j]+=a.a[i][k]*b.a[k][j];
}
temp.a[i][j]%=m;
}
}
return temp;
}
matrix quickpower(matrix a){
matrix temp;
memset(temp.a,0,sizeof(temp.a));
for(int i=0;i<10;i++)
temp.a[i][i]=1; ///千万要注意temp是单位矩阵不是全为1的矩阵,找了好久的bug
int num=k-9;
while(num>0){
if(num%2==1)
temp=mul(temp,a);
num/=2;
a=mul(a,a);
}
return temp;
}
int main()
{
matrix temp;
while(cin>>k>>m){
memset(temp.a,0,sizeof(temp.a));
for(int i=0;i<10;i++){
cin>>temp.a[0][i];
}
for(int i=1;i<10;i++){
temp.a[i][i-1]=1;
}
temp=quickpower(temp);
int res=0;
for(int i=0;i<10;i++)
res+=temp.a[0][i]*(9-i);
res%=m;
cout<<res<<endl;
}
return 0;
}