题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015
通过这道例题就能大概入一个门吧。
参考知乎链接(快速幂/矩阵快速幂):https://www.zhihu.com/tardis/sogou/art/42639682
参考题目博客:https://blog.csdn.net/dragon60066/article/details/60339236
233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4735 Accepted Submission(s): 2669
Problem Description
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a
0,1 = 233,a
0,2 = 2333,a
0,3 = 23333...) Besides, in 233 matrix, we got a
i,j = a
i-1,j +a
i,j-1( i,j ≠ 0). Now you have known a
1,0,a
2,0,...,a
n,0, could you tell me a
n,m in the 233 matrix?
Input
There are multiple test cases. Please process till EOF.
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
Output
For each case, output a
n,m mod 10000007.
Sample Input
1 1 1 2 2 0 0 3 7 23 47 16
Sample Output
234 2799 72937
Hint
Source
Recommend
易错概念:矩阵乘法
1.左乘:设A为m*p的矩阵,B为p*n的矩阵,那么称m*n的矩阵C为矩阵A与B的乘积,记作C=AB,称为A左乘以B。
2.右乘:设A为m*p的矩阵,B为p*n的矩阵,那么称m*n的矩阵C为矩阵A与B的乘积,记作C=AB,称为B右乘以A。
3.矩阵左乘向量得的是向量,而矩阵右乘向量得的是矩阵。
ac代码:
#include<iostream> #include<string.h> typedef long long ll; using namespace std; const int mod=1e7+7; int n,m; struct matrix{ ll mat[15][15]; matrix() { memset(mat,0,sizeof(mat)); } }; matrix mul(matrix B,matrix A)//B左乘一个列矩阵A 也就是B * A { int i,j,k; matrix C; for(i=1;i<=n+2;i++)//B 有几行 for(j=1;j<=n+2;j++)// A 有几列 for(k=1;k<=n+2;k++)// B有几列 / A 有几行 C.mat[i][j]=(C.mat[i][j]+B.mat[i][k]*A.mat[k][j])%mod; return C; } matrix fastpow(matrix A,int m)//矩阵快速幂 { matrix ans; for(int i=1;i<=n+2;i++)ans.mat[i][i]=1; while(m>0) { if(m&1)ans=mul(ans,A); A=mul( A , A ); m>>=1; } return ans; } int main() { while(cin>>n>>m) { matrix A,B; A.mat[1][1] = 23; for(int i=1;i<=n;i++) cin>>A.mat[i+1][1]; A.mat[n+2][1] = 3; for(int i=1;i<=n+1;i++) B.mat[i][1] = 10; for(int i=1;i<=n+2;i++) B.mat[i][n+2] = 1; for(int i=2;i<n+2;i++) { for(int j=2;j<=i;j++) { B.mat[i][j]=1; } } B=fastpow(B,m); A=mul(B,A); cout<<A.mat[n+1][1]<<endl; } return 0; }