hdu 5015 233 Matrix 矩阵快速幂

本文介绍了一种名为233Matrix的特殊矩阵及其构造方法,并通过递推关系求解特定位置元素的值。利用矩阵乘法和快速幂运算进行高效计算。

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

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 98    Accepted Submission(s): 47


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
 


构造矩阵就行了。具体看代码把,可以用print 把矩阵打出来看;

不知道为什么G++能过,C++数组越界,路过大神求指点。

#include<stdio.h>
#include<string.h>
#define Matr 15 //矩阵大小,注意能小就小   矩阵从1开始   所以Matr 要+1 
#define ll __int64
struct mat//矩阵结构体,a表示内容,size大小 矩阵从1开始   但size不用加一
{
	ll a[Matr][Matr];
	mat()//构造函数
	{
		memset(a,0,sizeof(a));
	}
};
int Size;
ll mod= 10000007;

mat multi(mat m1,mat m2)//两个相等矩阵的乘法,对于稀疏矩阵,有0处不用运算的优化 
{
	mat ans=mat(); 
	for(int i=1;i<=Size;i++)
		for(int j=1;j<=Size;j++)
			if(m1.a[i][j])//稀疏矩阵优化 
				for(int k=1;k<=Size;k++)
					ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%mod; //i行k列第j项
	return ans;
}

mat quickmulti(mat m,ll n)//二分快速幂 
{
	mat ans=mat();
	int i;
	for(i=1;i<=Size;i++)ans.a[i][i]=1;
	while(n)
	{
		if(n&1)ans=multi(m,ans);
		m=multi(m,m);
		n>>=1;
	}
	return ans;
}

 void print(mat m)//输出矩阵信息,debug用   
{  
    int i,j;  
    printf("%d\n",Size);  
    for(i=1;i<=Size;i++)  
    {  
        for(j=1;j<=Size;j++)
<span style="white-space:pre">			</span>printf("%I64d ",m.a[i][j]);  
        printf("\n");  
    }  
}  
int main()
{
	int i;
	mat gou,chu=mat();//构造矩阵  初始矩阵  
	int n,m;
	__int64 ans;
	while(scanf("%I64d%I64d",&n,&m)!=EOF)//n+2*n+2
	{
		Size=n+2;
		chu=mat();
		chu.a[1][n+1]=23;
		for(i=1;i<=n;i++)
		{
			scanf("%I64d",&chu.a[1][i]);
			chu.a[1][i]%=mod;
		}
		chu.a[1][n+2]=3;
		gou=mat();
		for(int j=1;j<=n;j++)
		{
			if(j==1)
			{
				gou.a[n+1][j]=10;
				gou.a[n+2][j]=1;
			}
			gou.a[j][j]=1;
			for(int i=1;i<=n+2;i++)
			{
				gou.a[i][j]+=gou.a[i][j-1];
			}
		}
		gou.a[n+1][n+1]=10;
		gou.a[n+2][n+2]=1;
		gou.a[n+2][n+1]=1;
		ans=multi(chu,quickmulti(gou,m)).a[1][n];
		printf("%I64d\n",ans%mod);
	}
	return 0;
}
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值