hdu 1757 和1005差不多 (矩阵快速幂)
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);
And ai(0<=i<=9) can only be 0 or 1
求f(n)%MOD
Sample Input
10 9999 //n mod
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
Sample Output
45
104
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 using namespace std ; 7 8 int MOD ; 9 10 struct Matrix 11 { 12 int mat[10][10]; 13 }; 14 15 Matrix mul(Matrix a,Matrix b) //矩阵乘法 16 { 17 Matrix c; 18 for(int i=0;i<10;i++) 19 for(int j=0;j<10;j++) 20 { 21 c.mat[i][j]=0; 22 for(int k=0;k<10;k++) 23 { 24 c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD; 25 } 26 } 27 return c; 28 } 29 Matrix pow_M(Matrix a,int k) //矩阵快速幂 30 { 31 Matrix ans; 32 memset(ans.mat,0,sizeof(ans.mat)); 33 for (int i=0;i<10;i++) 34 ans.mat[i][i]=1; 35 Matrix temp=a; 36 while(k) 37 { 38 if(k&1)ans=mul(ans,temp); 39 temp=mul(temp,temp); 40 k>>=1; 41 } 42 return ans; 43 } 44 45 46 47 int main () 48 { 49 //freopen("in.txt","r",stdin) ; 50 int n ; 51 while(cin>>n>>MOD) 52 { 53 Matrix t ; 54 int i , j ; 55 for (i = 0 ; i < 10 ; i++) 56 cin>>t.mat[0][i] ; 57 if (n < 10) 58 { 59 cout<<n<<endl ; 60 continue ; 61 } 62 for (i = 1 ; i < 10 ; i++) 63 for (j = 0 ; j < 10 ; j++) 64 { 65 if (i ==(j+1)) 66 t.mat[i][j] = 1 ; 67 else 68 t.mat[i][j] = 0 ; 69 } 70 Matrix ans = pow_M(t,n-9) ; 71 int sum = 0 ; 72 for (i = 0 ; i < 10 ; i++) 73 { 74 sum = (sum + ans.mat[0][i]*(9-i))%MOD ; 75 } 76 cout<<sum<<endl ; 77 78 79 } 80 81 82 return 0 ; 83 }