欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 5015 233 Matrix --矩阵快速幂

程序员文章站 2022-07-03 18:25:28
...

题意:

ai,j = a i-1,j +a i,j-1( i,j > 0) , a0,i=23333...., i+1个3,ai,0为输入的数值

给出n,m,给出(a0,1 ), ( a0,n)

求an,m

思路:

我们知道这是NXM的矩阵

试着画出该矩阵,想推出ai,j发现推不出来,但是可以推出下一列

HDU 5015 233 Matrix --矩阵快速幂

应该一目了然了吧

#include<bits/stdc++.h>
#define N  10000007
using namespace std;
struct node
{
	long long a[12][12];
};
const node A={
	          { 
			    {1,1 ,0,0,0,0,0,0,0,0,0,0},
	            {0,10,1,1,1,1,1,1,1,1,1,1},
				{0,0 ,1,1,1,1,1,1,1,1,1,1},
				{0,0 ,0,1,1,1,1,1,1,1,1,1},
				{0,0 ,0,0,1,1,1,1,1,1,1,1},
				{0,0 ,0,0,0,1,1,1,1,1,1,1},
				{0,0 ,0,0,0,0,1,1,1,1,1,1},
				{0,0 ,0,0,0,0,0,1,1,1,1,1},
				{0,0 ,0,0,0,0,0,0,1,1,1,1},
				{0,0 ,0,0,0,0,0,0,0,1,1,1},
				{0,0 ,0,0,0,0,0,0,0,0,1,1},
				{0,0 ,0,0,0,0,0,0,0,0,0,1}  
			  }
             };
const node B={
	          {
	          	{3,233,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0,0}
	          }
             };
node cheng(node a,node b,bool f)
{
	node c;
	memset(c.a,0,sizeof(c.a));
	for(int i=0;i<12;i++){
	if(f&&i==1) break;
	for(int j=0;j<12;j++)
	for(int k=0;k<12;k++)
	c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j])%N)%N;
	}
	return c;
}

int main()
{
	int t,n,m,sum;
	node a,b;
	while(~scanf("%d%d",&n,&m))
    {
    	if(n==m&&n==0)
    	{
    		printf("0\n");
    		continue;
    	}
    	a=A;
    	b=B;
    	for(int i=0;i<n;i++)
    	scanf("%lld",&(b.a[0][2+i]));
    
    	while(m)
    	{
    		if(m&1) b=cheng(b,a,true);
    		m>>=1;
    		a=cheng(a,a,false);
    	}
    	printf("%lld\n",(b.a[0][n+1]%N+N)%N);
    }	
	return 0;
}


相关标签: 数学