您的位置:首页 > 理论基础 > 计算机网络

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

2014-09-15 10:24 941 查看

先贴四份矩阵快速幂的模板:/article/6813095.html

http://www.cppblog.com/acronix/archive/2010/08/23/124470.aspx?opt=admin

/article/5121531.html

/article/5658813.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

[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each case, output an,m mod 10000007.

[align=left]Sample Input[/align]

1 1
1
2 2
0 0
3 7
23 47 16

[align=left]Sample Output[/align]

234
2799
72937

Hint



[align=left]Source[/align]
2014 ACM/ICPC Asia Regional Xi'an Online

[align=left]Recommend[/align]
hujie | We have carefully selected several similar problems for you: 5017 5016 5014 5013 5012

题解1:/article/5250232.html

题解2:/article/1972363.html

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

[cpp] view plaincopyprint?

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 |

-- -- -- --

#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[15];
//ll ans;

struct Mat
{
ll mat

;
};

Mat e,f,g;
Mat operator * (Mat a,Mat b)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
ll i,j,k;
for(k =0 ; k < n ; k++)
{
for(i = 0 ; i < n ;i++)
{
if(a.mat[i][k]==0) continue;//优化
for(j = 0 ;j < n ;j++)
{
if(b.mat[k][j]==0) 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 =0 ; i < n ;i++)
for(j = 0; j < n ;j++)
c.mat[i][j] = (i==j);
for(; k ;k >>= 1)
{
if(k&1) c = c*a;
a = a*a;
}
return c;
}

void ini()
{
ll i,j;
for(i=1;i<=nn;i++){
scanf("%I64d\n",&x[i]);
}
memset(e.mat,0,sizeof(e.mat));
memset(f.mat,0,sizeof(f.mat));
e.mat[0][0]=233;
e.mat[0][1]=3;
e.mat[0][2]=233+x[1];
for(i=2;i<=nn;i++){
e.mat[0][i+1]=e.mat[0][i]+x[i];
}
for(j=0;j<nn+2;j++){
if(j!=1){
f.mat[0][j]=10;
}
f.mat[1][j]=1;
}
for(i=2;i<nn+2;i++){
for(j=i;j<nn+2;j++){
f.mat[i][j]=1;
}
}
n=nn+2;
}

void solve()
{
if(m>1){
g= e*  (f^(m-1) );
}
else{
g.mat[0][nn+1]=e.mat[0][nn+1];
}
}

void out()
{
printf("%I64d\n",g.mat[0][nn+1]);
}

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 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: