hdu-5015(矩阵快速幂)

本文深入探讨了如何使用快速幂算法解决特定矩阵运算问题,详细介绍了矩阵乘法、幂运算及其在实际场景中的应用。通过实例分析,展示了如何通过矩阵的幂次求解复杂矩阵问题,并运用快速幂算法提高计算效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

233 Matrix
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K
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

题意:给出矩阵的第0行(233,2333,23333,...)和第0列a1,a2,...an(n<=10,m<=10^9),给出式子: A[i][j] = A[i-1][j] + A[i][j-1],要求A[n][m]。


如上图:红色部分为绿色部分之和,而顶上的绿色部分很好求,左边的绿色部分(最多10个)其实就是:A[1][m-1],A[2][m-1]..A[n][m-1],即对每个1<=i<=n, A[i][m]都可由A[1][m-1],A[2][m-1]..A[n][m-1],于是建立12*12的矩阵:

将中间矩阵求m-1次幂,与右边[A[0][1],A[1][1]..A[n][1],3]^T相乘,结果就可以得出了。

矩阵的幂可用快速幂算法。

#include<stdio.h>
#include<string.h>
#define mod 10000007
#define ll __int64
int n,m;
struct matrix
{
	ll x[12][12];
}a;
/*
10,1,0,0,0,0,0,0,0,0,0,0,
	 0,1,0,0,0,0,0,0,0,0,0,0,
	10,1,1,0,0,0,0,0,0,0,0,0,
	10,1,1,1,0,0,0,0,0,0,0,0,
	10,1,1,1,1,0,0,0,0,0,0,0,
	10,1,1,1,1,1,0,0,0,0,0,0,
	10,1,1,1,1,1,1,0,0,0,0,0,
	10,1,1,1,1,1,1,1,0,0,0,0,
	10,1,1,1,1,1,1,1,1,0,0,0,
	10,1,1,1,1,1,1,1,1,1,0,0,
	10,1,1,1,1,1,1,1,1,1,1,0,
	10,1,1,1,1,1,1,1,1,1,1,1
*/
matrix operator * (matrix &s,matrix &z)
{
	matrix c;
	for(int i=0;i<n+2;i++)
		for(int j=0;j<n+2;j++)
		{
			c.x[i][j]=0;
			for(int k=0;k<n+2;k++)
				c.x[i][j]=(c.x[i][j]+(s.x[i][k]*z.x[k][j])%mod)%mod;
		}
		return c;
}
matrix pow(matrix s,int z)//快速幂
{
	matrix c;
	memset(c.x,0,sizeof(c.x));
	for(int i=0;i<n+2;i++)
		c.x[i][i]=1;
	while(z)  
    {  
        if(z&1) c=c*s;  
        s=s*s;  
        z>>=1;  
    }  
	return c;
}
int main()
{
	ll b[12];
	b[0]=23;
	b[1]=3;
	for(int i=0;i<12;i++)
		for(int j=0;j<12;j++)
		{
			if(j==0){
				if(i!=1) a.x[i][j]=10;
				else a.x[i][j]=0;
			}
			else{
				if(j<=i) a.x[i][j]=1;
				else a.x[i][j]=0;
			}
		}
		a.x[0][1]=1;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i=2;i<n+2;i++)
			scanf("%I64d",&b[i]);
		matrix c;
		ll z[12];
		memset(z,0,sizeof(z));
		c=pow(a,m);
		for(int j=0;j<n+2;j++)
			for(int k=0;k<n+2;k++)
				z[j]=(z[j]+(c.x[j][k]*b[k])%mod)%mod;
			printf("%I64d\n",z[n+1]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值