/*
难点在构造矩阵
构造好矩阵
就可以在原有的模板求
矩阵的N次方
在乘上初始值
我求矩阵快速幂用的是递归
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define max_n 10
#define n 10
using namespace std;
struct M
{
int s[max_n][max_n];
};
int mod;
M mul(M a,M b)
{
int i,j,k;
M t;
memset(t.s,0,sizeof(t.s));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
t.s[i][j]=(t.s[i][j]+a.s[i][k]*b.s[k][j])%mod;
}
}
return t;
}
M pow(M a,int t)
{
if(t==1)
return a;
else
{
M b=pow(a,t/2);
if(t&1)
return mul(mul(b,b),a);
else
return mul(b,b);
}
}
int main()
{
int k;
while(scanf("%d%d",&k,&mod)!=EOF)
{
M a,b,c,d;
int i;
int s1[max_n][max_n]={0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,
0,0,0,0,0,1,0,0,0,0,
0,0,0,0,0,0,1,0,0,0,
0,0,0,0,0,0,0,1,0,0,
0,0,0,0,0,0,0,0,1,0};
memcpy(a.s,s1,sizeof(a.s));
for(i=0;i<10;i++)
scanf("%d",&a.s[0][i]);
b=pow(a,k-9);
memset(c.s,0,sizeof(c.s));
for(i=0;i<10;i++)
c.s[i][0]=9-i;//注意这个地方,我错了好长时间。
d=mul(b,c);
printf("%d\n",d.s[0][0]);
//int sum=0;
//for(i=0;i<10;i++)
//sum+=(sum+b.s[0][i]*(9-i))%mod;
//printf("%d\n",sum);
}
return 0;
}
矩阵裸题hdu 1757 A Simple Math Problem
最新推荐文章于 2021-04-08 19:21:24 发布