HDOJ 题目1588 Gauss Fibonacci(矩阵快速幂,矩阵加,二分)

57 篇文章 1 订阅

Gauss Fibonacci

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2403    Accepted Submission(s): 1006


Problem Description
Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.

Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.

Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)

The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead.
 

Input
The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.
 

Output
For each line input, out the value described above.
 

Sample Input
  
  
2 1 4 100 2 0 4 100
 

Sample Output
  
  
21 12
 

Author
DYGG
 

Source
 

Recommend
linle   |   We have carefully selected several similar problems for you:   1757  3117  2604  2294  2276 
 
思路:
  1. 构造矩阵: 
  2.           |1  1|      | f(2)    f(1)| 
  3.     A= |1  0|   =  | f(1)    f(0)| 
  4.          
  5.            
  6.         |1  1| ^b   | f(b+1)    f(b)|             
  7. A^b =|1  0|  =   | f(b)    f(b-1)| 
  8.                  
  9.     f(b) = matrix[0][1]=matrix[1][0]; 
  10.                        
  11.     首项是:A^b 
  12.     公比是:A^k 
  13.     项数是:N 
  14.     可以把问题进一步简化 
  15.     因为矩阵的加法对乘法也符合分配律,我们提出一个A^b来,形成这样的式子: 
  16.     A^b*( I + A^k + (A^k)^2 + .... + (A^k)^(N-1) ) 
  17. 化简成A^b+A^b*( B+B^2+……B^(n-1) ),其中B=A^
  18. G(N)=B^I + ... + B^(N-1), 
  19. 设q为N/2
  20. 如果q为偶数
  21. 则G(N)=G(q)+G(q)*B^q
  22. 如果q为奇数的话
  23. G(N)=G(q)+B^(q+1)+G(q)*B(q+1)
  24. ac代码
  25. #include<stdio.h>
    #include<string.h>
    struct s
    {
    	__int64 m[2][2];
    };
    __int64 k,x,n,m;
    struct s muti(struct s a,struct s b)
    {
    	__int64 i,j,k;
    	struct s c;
    	memset(c.m,0,sizeof(c.m));
    	for(i=0;i<2;i++)
    	{
    		for(j=0;j<2;j++)
    		{
    			for(k=0;k<2;k++)
    			{
    				c.m[i][j]+=(a.m[i][k]*b.m[k][j])%m;
    			}
    			c.m[i][j]%=m;
    		}
    	}
    	return c;
    }
    struct s addm(struct s a,struct s b)
    {
    	struct s c;
    	int i,j;
    	for(i=0;i<2;i++)
    	{
    		for(j=0;j<2;j++)
    			c.m[i][j]=(a.m[i][j]+b.m[i][j])%m;
    	}
    	return c;
    }
    struct s powm(struct s a,__int64 n)
    {
    	struct s b;
    //	int i,j;
    	b.m[0][0]=b.m[1][1]=1;
    	b.m[0][1]=b.m[1][0]=0;
    	while(n)
    	{
    		if(n&1)
    			b=muti(b,a);
    		a=muti(a,a);
    		n/=2;
    	}
    	return b;
    }
    struct s badd(struct s a,__int64 n)
    {
    	struct s g,o;
    	if(n==1)
    	{
    		struct s tt=a;
    		return tt;
    	}
    	g=badd(a,n/2);
    	if(n&1)
    	{
    		o=powm(a,n/2+1);
    		return addm(addm(g,o),muti(g,o));
    	}
    	else
    	{
    		o=powm(a,n/2);
    		return addm(g,muti(g,o));
    	}
    }
    int main()
    {
    	//int k,b,n,m
    	while(scanf("%I64d%I64d%I64d%I64d",&k,&x,&n,&m)!=EOF)
    	{
    		struct s a,t,mb,ans;
    		a.m[0][0]=a.m[1][0]=a.m[0][1]=1;
    		a.m[1][1]=0;
    		t=powm(a,k);
    		t=badd(t,n-1);
    		mb=powm(a,x);
    		ans=addm(mb,muti(mb,t));
    		printf("%I64d\n",ans.m[1][0]);
    	}
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值