【题目链接】:click here~~
【题目大意】:
If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
问f(k)%m的值。
【思路】:矩阵快速幂,具体思路看代码吧,注意一些细节。
代码:
/*
* Problem: HDU No.1757
* Running time: 15MS
* Complier: C++
* Author: javaherongwei
* Create Time: 9:55 2015/9/21 星期一
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int siz=10; // max size of the matrix,
LL k,mod;
struct mut
{
LL mat[siz][siz]; // value of the matrix
mut(){
memset(mat,0,sizeof(mat));
}
void init(LL v){
for(int i=0; i<=siz; ++i)
mat[i][i]=v;
}
} a,b,c;
mut operator * (mut a,mut b) // * of matrix
{
mut c;
for(int i=0; i<siz; ++i) //矩阵优化
{
for(int k=0; k<siz; ++k)
{
for(int j=0; j<siz; ++j)
{
c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%mod;
c.mat[i][j]%=mod;
}
}
}
return c;
}
mut operator ^(mut a,LL n) // ^ of matrix
{
mut c;
c.init(1);
while(n){
if(n&1) c=a*c;
a=a*a;
n>>=1;
}
return c;
}
void init() //init
{
for(int i=0; i<10; ++i)
a.mat[9-i][0]=i;
for(int i=0; i<9; ++i)
b.mat[i+1][i]=1;
}
int main(){
init();
while(~scanf("%lld %lld",&k,&mod)){
for(int i=0; i<10; ++i){
scanf("%lld",&b.mat[0][i]);
}
if(k<10) printf("%lld\n",k%mod);
else{
c=b^(k-9);
c=c*a;
printf("%lld\n",c.mat[0][0]%mod);
}
}
return 0;
}