HDU 1588 Gauss Fibonacci(矩阵快速幂)

思路:显然的有f(n)=A^n,那么把g(i)代入后有sum = A^b+A^(k+b)+A^(2k+b)+...A^((n-1)k+b)

           提取A^b之后sum = A^b*(A^k^0+A^k^1+A^k^2+.....A^k^(n-1)),将A^k设为B的话那么就是一个简单的二分求和过程,注意最后要加上B^0

坑点:虽然最后结果不会爆long long,可是中途乘法过程中是会爆的


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
int n,mod,k,b;
struct Mat
{
	LL a[31][31];   //矩阵大小
}pi;
Mat mul(Mat a,Mat b)
{
	Mat t;
	memset(t.a,0,sizeof(t.a));
	for(int i = 0;i<2;i++)
	{
		for(int k = 0;k<2;k++)
		{
			if(a.a[i][k])
              for(int j = 0;j<2;j++)
			  {
				  t.a[i][j]+=a.a[i][k]*b.a[k][j];
				  if(t.a[i][j]>=mod)
					  t.a[i][j]%=mod;
			  }
		}
	}
	return t;
}
Mat expo(Mat p,int k)
{
	if(k==1)return p;
	Mat e;
	memset(e.a,0,sizeof(e.a));
	for(int i = 0;i<2;i++)   //初始化单位矩阵  
		e.a[i][i]=1;
    if(k==0)return e;
	while(k)
	{
		if(k&1)
			e = mul(p,e);
		p = mul(p,p);
		k>>=1;
	}
	return e;
}
Mat add(Mat a,Mat b)
{
	Mat t;
	memset(t.a,0,sizeof(t.a));
	for(int i = 0;i<2;i++)
		for(int j = 0;j<2;j++)
            t.a[i][j]=(a.a[i][j]+b.a[i][j])%mod;
	return t;
}
Mat sum(int k)
{
	if(k==1)
		return pi;
	if(k&1)
		return add(sum(k-1),expo(pi,k));
	else
	{
		Mat tmp = expo(pi,k/2);
		Mat temp = sum(k/2);
		return add(temp,mul(tmp,temp));
	}
}
int main()
{
    while(scanf("%d%d%d%d",&k,&b,&n,&mod)!=EOF)
	{
		memset(pi.a,0,sizeof(pi.a));
		Mat p;
		p.a[0][0]=p.a[0][1]=p.a[1][0]=1;
		p.a[1][1]=0;
		pi = expo(p,k);        //A^k
		Mat res = expo(p,b);   //A^b
		Mat tmp = sum(n-1);

	   Mat e;
	   memset(e.a,0,sizeof(e.a));
	    for(int i = 0;i<2;i++)   //初始化单位矩阵  
	 	   e.a[i][i]=1;
	    tmp = add(tmp,e);	
		tmp = mul(tmp,res);
		printf("%d\n",tmp.a[0][1]);
	}
}


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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值