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

时间:2024-01-12 17:28:50

先贴四份矩阵快速幂的模板: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).
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

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

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. --                      --   --  --
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string> #define N 15
#define M 15
#define mod 10000007
#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll nn,m;
ll n;
ll x[];
//ll ans; struct Mat
{
ll mat[N][N];
}; Mat e,f,g;
Mat operator * (Mat a,Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
ll i,j,k;
for(k = ; k < n ; k++)
{
for(i = ; i < n ;i++)
{
if(a.mat[i][k]==) continue;//优化
for(j = ;j < n ;j++)
{
if(b.mat[k][j]==) continue;//优化
c.mat[i][j] = (c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod;
}
}
}
return c;
}
Mat operator ^(Mat a,ll k)
{
Mat c;
ll i,j;
for(i = ; i < n ;i++)
for(j = ; j < n ;j++)
c.mat[i][j] = (i==j);
for(; k ;k >>= )
{
if(k&) c = c*a;
a = a*a;
}
return c;
} void ini()
{
ll i,j;
for(i=;i<=nn;i++){
scanf("%I64d\n",&x[i]);
}
memset(e.mat,,sizeof(e.mat));
memset(f.mat,,sizeof(f.mat));
e.mat[][]=;
e.mat[][]=;
e.mat[][]=+x[];
for(i=;i<=nn;i++){
e.mat[][i+]=e.mat[][i]+x[i];
}
for(j=;j<nn+;j++){
if(j!=){
f.mat[][j]=;
}
f.mat[][j]=;
}
for(i=;i<nn+;i++){
for(j=i;j<nn+;j++){
f.mat[i][j]=;
}
}
n=nn+;
} void solve()
{
if(m>){
g= e* (f^(m-) );
}
else{
g.mat[][nn+]=e.mat[][nn+];
}
} void out()
{
printf("%I64d\n",g.mat[][nn+]);
} int main()
{
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
// while(T--)
while(scanf("%I64d%I64d",&nn,&m)!=EOF)
{
ini();
solve();
out();
} return ;
}