先贴四份矩阵快速幂的模板:http://www.cnblogs.com/shangyu/p/3620803.html
http://www.cppblog.com/acronix/archive/2010/08/23/124470.aspx?opt=admin
http://www.cnblogs.com/vongang/archive/2012/04/01/2429015.html
http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html
233 Matrix
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 257 Accepted Submission(s): 165
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 a0,1 = 233,a0,2 = 2333,a0,3 = 23333...) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,...,an,0, could you tell me an,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 ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
Output
For each case, output an,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:http://www.cnblogs.com/whatbeg/p/3971994.html
题解2:http://blog.csdn.net/u013368721/article/details/39271565
题目分析:矩阵快速幂,构建一个如下的矩阵即可:
- n+2行的矩阵
- -- -- -- --
- | 1 1 1 1 1 1 1 0 | | a1 |
- | 0 1 1 1 1 1 1 0 | | a2 |
- | 0 0 1 1 1 1 1 0 | | a3 |
- | 0 0 0 1 1 1 1 0 | | a4 |
- | 0 0 0 0 1 1 1 0 | * | a5 |
- | 0 0 0 0 0 1 1 0 | | an |
- | - - - - - - - - - - - | | |
- | 0 0 0 0 0 0 10 1 | | 233|
- | 0 0 0 0 0 0 0 1 | | 3 |
- -- -- -- --
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<string> 10 11 #define N 15 12 #define M 15 13 #define mod 10000007 14 #define p 10000007 15 #define mod2 100000000 16 #define ll long long 17 #define LL long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20 21 using namespace std; 22 23 ll nn,m; 24 ll n; 25 ll x[15]; 26 //ll ans; 27 28 struct Mat 29 { 30 ll mat[N][N]; 31 }; 32 33 Mat e,f,g; 34 Mat operator * (Mat a,Mat b) 35 { 36 Mat c; 37 memset(c.mat,0,sizeof(c.mat)); 38 ll i,j,k; 39 for(k =0 ; k < n ; k++) 40 { 41 for(i = 0 ; i < n ;i++) 42 { 43 if(a.mat[i][k]==0) continue;//优化 44 for(j = 0 ;j < n ;j++) 45 { 46 if(b.mat[k][j]==0) continue;//优化 47 c.mat[i][j] = (c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod; 48 } 49 } 50 } 51 return c; 52 } 53 Mat operator ^(Mat a,ll k) 54 { 55 Mat c; 56 ll i,j; 57 for(i =0 ; i < n ;i++) 58 for(j = 0; j < n ;j++) 59 c.mat[i][j] = (i==j); 60 for(; k ;k >>= 1) 61 { 62 if(k&1) c = c*a; 63 a = a*a; 64 } 65 return c; 66 } 67 68 69 void ini() 70 { 71 ll i,j; 72 for(i=1;i<=nn;i++){ 73 scanf("%I64d ",&x[i]); 74 } 75 memset(e.mat,0,sizeof(e.mat)); 76 memset(f.mat,0,sizeof(f.mat)); 77 e.mat[0][0]=233; 78 e.mat[0][1]=3; 79 e.mat[0][2]=233+x[1]; 80 for(i=2;i<=nn;i++){ 81 e.mat[0][i+1]=e.mat[0][i]+x[i]; 82 } 83 for(j=0;j<nn+2;j++){ 84 if(j!=1){ 85 f.mat[0][j]=10; 86 } 87 f.mat[1][j]=1; 88 } 89 for(i=2;i<nn+2;i++){ 90 for(j=i;j<nn+2;j++){ 91 f.mat[i][j]=1; 92 } 93 } 94 n=nn+2; 95 } 96 97 void solve() 98 { 99 if(m>1){ 100 g= e* (f^(m-1) ); 101 } 102 else{ 103 g.mat[0][nn+1]=e.mat[0][nn+1]; 104 } 105 } 106 107 void out() 108 { 109 printf("%I64d ",g.mat[0][nn+1]); 110 } 111 112 int main() 113 { 114 // freopen("data.in","r",stdin); 115 // freopen("data.out","w",stdout); 116 //scanf("%d",&T); 117 //for(int cnt=1;cnt<=T;cnt++) 118 // while(T--) 119 while(scanf("%I64d%I64d",&nn,&m)!=EOF) 120 { 121 ini(); 122 solve(); 123 out(); 124 } 125 126 return 0; 127 }