hdu 1757

http://acm.hdu.edu.cn/showproblem.php?pid=1757
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);
给出k,m和a0~a9,求f(k)%m, k<2*10^9 ,="" m="" <="" 10^5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<iostream>
#define mem(x,y) memset(x,y,sizeof(x))
#define inf 0x3f3f3f3f
using namespace std;
#define siz 10
#define Mytype long long
int N=10;
Mytype mod;
struct matrix
{
Mytype a[siz][siz];
matrix operator*(const matrix &y)const
{
matrix res;
mem(res.a,0);
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(a[i][j])
for(int k=0;k<N;k++)
res.a[i][k]+=a[i][j]*y.a[j][k],res.a[i][k]%=mod;
return res;
}
matrix operator+(const matrix &y)const
{
matrix res;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
res.a[i][j]=a[i][j]+y.a[i][j],res.a[i][j]%=mod;
return res;
}
matrix operator*=(const matrix &y)
{
*this=y* *this;
return *this;
}
};
matrix qmod(matrix a,int k)
{

matrix res;
mem(res.a,0);
for(int i=0;i<N;i++)
res.a[i][i]=1;
while(k)
{
if(k&1)
res*=a;
a*=a;
k>>=1;
}
return res;
}
int main()
{

int k;
while(~scanf("%d%d",&k,&mod))
{
matrix t;
mem(t.a,0);
for(int i=0;i<10;i++)
scanf("%I64d",&t.a[0][i]);
if(k<10)
{
printf("%d\n",k);
continue;
}
for(int i=0;i<9;i++)
t.a[i+1][i]=1;
t=qmod(t,k-9);
long long ans=0;
for(int i=0;i<10;i++)
ans+=t.a[0][i]*(9-i),ans%=mod;
printf("%I64d\n",ans);
}
}

EOF