hdu 3306 结合本题说下矩阵构造方法

Another kind of Fibonacci

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 994    Accepted Submission(s): 397


Problem Description
As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate S(N) , S(N) = A(0) 2 +A(1) 2+……+A(n) 2.

 

Input
There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 2 31 – 1
X : 2<= X <= 2 31– 1
Y : 2<= Y <= 2 31 – 1
 

Output
For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.
 

Sample Input
  
  
2 1 1 3 2 3
 

Sample Output
  
  
6 196

由题意

s(n)=s(n-1)+a(n)^2

=s(n-1)+x^2*a(n-1)^+y^2^a(n-2)^2+2xy*a(n-1)*a(n-2)

右边共有4项  所以构造4*4矩阵

则 用矩形A*[s(n-1),   a(n-1)^2,  a(n-2)^2,  a(n-1)*a(n-2)]=[s(n),  a(n)^2   ,a(n-1)^2,     a(n)*a(n-1)]  

后者比前者全部大一个层次 (即n-1变n  常数除外 这个木有常数)

借用下下面博客的图片

http://www.darkswordzone.com/?p=1432


根据s(n)=s(n-1)+x^2*a(n-1)^+y^2^a(n-2)^2+2xy*a(n-1)*a(n-2)  很容易知道第一行填什么

根据a(n)^2=x^2*a(n-1)^+y^2^a(n-2)^2+2xy*a(n-1)*a(n-2)   也很容易知道第二行填什么 

第三行更简单

第四行 不好直接找出来 这时候就要充分利用题目所给条件了 

把后者分开 即a(n)*a(n-1)=(X * A(N - 1) + Y * A(N - 2))*a(n-1)

=x*a(n-1)^2+y*a(n-2)*a(n-1)  这时候很容易就能看出来矩阵应该是什么 了

矩阵构造完毕     我以前不知道如何去构造矩阵  煞是头疼啊,我想这种矩阵构造方法应该是可以的   没有人教我  只能 自己参考了一些资料 不知道有没有更简单快速的方法 求大神教授


AC代码


#include<stdio.h>
#define N 4
#define mod 10007
struct mat
{
	int mar[4][4];
};
mat a,b,c,init,temp;
mat res=
	  {
		  1,0,0,0,

          0,1,0,0,

          0,0,1,0,

          0,0,0,1
	  };
mat mul(mat a1,mat b1)  
{  
    int i,j,l;  
    mat c1;  
    for (i=0;i<N;i++)  
    {  
        for (j=0;j<N;j++)  
        {  
            c1.mar[i][j]=0;  
            for (l=0;l<N;l++)  
            {  
                c1.mar[i][j]+=(a1.mar[i][l]*b1.mar[l][j])%mod;  
                c1.mar[i][j]%=mod;  
            }  
        }  
    }  
    return c1;  
}  
mat er_fun(mat e,int x)
{
	mat tp;
	tp=e;
	e=res;
	while(x)
	{
		if(x&1)
			e=mul(e,tp);
		tp=mul(tp,tp);
		x>>=1;
	}
	return e;
	
}



int main()
{
      int n,x,y,i,j;

	  while(scanf("%d %d %d",&n,&x,&y)!=EOF)
	  {
		  x=x%mod; y=y%mod;
             init.mar[0][0]=1;
			 init.mar[0][1]=(x*x)%mod;
			 init.mar[0][2]=(y*y)%mod;
			 init.mar[0][3]=(2*x*y)%mod;
             init.mar[1][0]=0;
			 init.mar[1][1]=(x*x)%mod;
			 init.mar[1][2]=(y*y)%mod;
			 init.mar[1][3]=(2*x*y)%mod;
			 init.mar[2][0]=0;
			 init.mar[2][1]=1;
			 init.mar[2][2]=0;
			 init.mar[2][3]=0;
			 init.mar[3][0]=0;
			 init.mar[3][1]=x;
			 init.mar[3][2]=0;
			 init.mar[3][3]=y;
			 a=init;
             b=er_fun(a,n-1);
			 printf("%d\n",(2*b.mar[0][0]+b.mar[0][1]+b.mar[0][2]+b.mar[0][3])%mod);
             
				
	  }
return 0;
}

hnust_xiehonghao


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值