您的位置:首页 > 其它

HDU 5015 233 Matrix(矩阵快速幂)

2017-04-12 16:32 411 查看

233 Matrix

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



Source

2014 ACM/ICPC Asia Regional Xi’an Online

思路:刚开始看题时也以为是矩阵快速幂,但是不会构造转换矩阵,所以就放弃了这种想法。。。

矩阵是这样构造的

通过题目我们可以推出矩阵

a1 a1+233 a1+2333 a1+23333…..

a2 a1+a2+233 a1+a2+2333 a1+a2+23333…..

a3 a1+a2+a3+233 a1+a2+a3+2333 a1+a2+a3+23333…..

……

所以我构造这样一个初始矩阵

23

a1

a2

a3

….

3

转换矩阵A是这样的

10 0 0 0 ….. 1

10 1 0 0 ….. 1

10 1 1 0 ….. 1

10 1 1 1 ….. 1

…….

0 0 0 0 …… 1

那么第n行的第m个数就是A的m次方*第一列

代码:

#include<stdio.h>
#include<string.h>

#define N 15
#define mod 10000007
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;

struct Matrix
{
LL mat

;
};
Matrix unit_matrix;
int n,m;
int s[15];

Matrix mul(Matrix a,Matrix b)//矩阵相乘
{
Matrix res;
for(int i=0; i<=n+1; ++i)
for(int j=0; j<=n+1; ++j)
{
res.mat[i][j]=0;
for(int k=0; k<=n+1; ++k)
res.mat[i][j]=(res.mat[i][j]+a.mat[i][k]*b.mat[k][j]%mod)%mod;
}
return res;
}

Matrix pow_matrix(Matrix a,int k)//矩阵快速幂
{
Matrix res=unit_matrix;
while(k)
{
if(k&1)
res=mul(res,a);
a=mul(a,a);
k>>=1;
}
return res;
}

int main()
{
Matrix arr,p;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<=n+1; ++i)//单位矩阵
unit_matrix.mat[i][i]=1;
for(int i=1; i<=n; ++i)//构造初始矩阵
scanf("%d",&s[i]);
s[0]=23,s[n+1]=3;//初始矩阵
mem(arr.mat,0);
for(int i=0; i<=n+1; ++i)//构造转换矩阵
{
arr.mat[i][0]=10,arr.mat[i][n+1]=1;
for(int j=1; j<=i; ++j)
arr.mat[i][j]=1;
}
for(int i=0; i<=n; ++i)//转换矩阵
arr.mat[n+1][i]=0;
p=pow_matrix(arr,m);
int ans=0;
for(int i=0; i<=n+1; ++i)//找到第n行第m个数
ans=(ans+s[i]*p.mat
[i]%mod)%mod;
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: