HDU 5015 233 Matrix(网络赛1009) 矩阵快速幂

先贴四份矩阵快速幂的模板: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 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).
 
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
hujie   |   We have carefully selected several similar problems for you:   5017  5016  5014  5013  5012 

 

 题解1:http://www.cnblogs.com/whatbeg/p/3971994.html

题解2:http://blog.csdn.net/u013368721/article/details/39271565

题目分析:矩阵快速幂,构建一个如下的矩阵即可:

 

  1. n+2行的矩阵  
  2. --                      --   --  --  
  3. | 1  1  1  1  1  1  1  0 |   | a1 |  
  4. | 0  1  1  1  1  1  1  0 |   | a2 |  
  5. | 0  0  1  1  1  1  1  0 |   | a3 |  
  6. | 0  0  0  1  1  1  1  0 |   | a4 |  
  7. | 0  0  0  0  1  1  1  0 | * | a5 |  
  8. | 0  0  0  0  0  1  1  0 |   | an |  
  9. |  - - - - - - - - - - - |   |    |  
  10. | 0  0  0  0  0  0 10  1 |   | 233|  
  11. | 0  0  0  0  0  0  0  1 |   | 3  |  
  12. --                      --   --  --  

 

  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\n",&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\n",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 }

 

转载于:https://www.cnblogs.com/njczy2010/p/3972316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值