标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #include<algorithm> 7 #define inf 0x7fffffff 8 using namespace std; 9 10 int K,m; 11 struct matrix 12 { 13 int an[12][12]; 14 }temp; 15 16 matrix multiply(matrix a,matrix b) 17 { 18 matrix c; 19 memset(c.an,0,sizeof(c.an)); 20 for (int i=1 ;i<=10 ;i++) 21 { 22 for (int j=1 ;j<=10 ;j++) 23 { 24 for (int k=1 ;k<=10 ;k++) 25 { 26 c.an[i][j]=(c.an[i][j]+a.an[i][k] * b.an[k][j])%m; 27 c.an[i][j] %= m; 28 } 29 } 30 } 31 return c; 32 } 33 34 int calc(int u) 35 { 36 matrix x; 37 memset(x.an,0,sizeof(x.an)); 38 for (int j=1 ;j<=10 ;j++) x.an[j][1]=10-j; 39 while (u) 40 { 41 if (u&1) x=multiply(temp,x); 42 u >>= 1; 43 temp=multiply(temp,temp); 44 } 45 int sum=0; 46 return x.an[1][1]%m; 47 } 48 49 int main() 50 { 51 while (scanf("%d%d",&K,&m)!=EOF) 52 { 53 for (int i=1 ;i<=10 ;i++) scanf("%d",&temp.an[1][i]); 54 if (K<=9) {printf("%d\n",K);continue; } 55 for (int i=2 ;i<=10 ;i++) 56 { 57 for (int j=1 ;j<=10 ;j++) 58 temp.an[i][j]= i==j+1 ? 1 : 0 ; 59 } 60 printf("%d\n",calc(K-9)); 61 } 62 return 0; 63 }
hdu 1757 A Simple Math Problem 矩阵快速幂
标签:
原文地址:http://www.cnblogs.com/huangxf/p/4399891.html