hdu 1588 Gauss Fibonacci (矩阵)

Gauss Fibonacci

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


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 
 

Statistic |  Submit |  Discuss | Note

题目大意: 

g[i]=k*i+b  

读入k,b,n,p

求sigma  (i=0..n-1)   f[k*i+b]  在模p 意义下的值,其中f[i]  表示fibonacci 数列的第i项。

题解:矩阵。

设Fibonacci数列的矩阵为A ={(1,1)(1,0)}  ()中的表示同一行。

那么如果我们要求Fibonacci数列的第K项,就是求A^K 后第一行第二列的数的答案。

那么我们就可以把   sigma  (i=0..n-1)   f[k*i+b] 化简成

A^b+A^(k+b)+A^(k*2+b)+....+A^(k*(n-1)+b)  的答案。

继续化简,提取公因式 ,得到

A^b *( A^0+A^k+...+A^(k*(n-1))  )

令  A^k=B

那么就可以得到

A^b *( B^0+B^1+...+B^(n-1))

这道题的数据范围是1000000000,所以肯定不能直接枚举,需要用递归来优化时间

对于矩阵S = A + A2 + A3 + … + Ak   

当k为偶数时,s(k)=(1+A^(k/2))*(A+A^2+…+A^(k/2))

当k为奇数时,s(k)=A+(A+A^(k/2+1))*(A+A^2+…+A^(k/2)

所以式子最终化简成了  A^b*(B^0+S)

对于B^0 = { (1,0) (0,1)}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 2
#define LL long long 
using namespace std;
int n,m,k,b;
LL p;
struct data
{
	LL a[10][10];
}a,e,t2;
void clear(data &x)
{
	for (int i=1;i<=N;i++)
	 for (int j=1;j<=N;j++)
	  x.a[i][j]=0;
}
void change(data &a,data b)
{
	for (int i=1;i<=N;i++)
	 for (int j=1;j<=N;j++)
	  a.a[i][j]=b.a[i][j];
}
data mul(data a,data b)
{
	data c; 
	for (int i=1;i<=N;i++)
	 for (int j=1;j<=N;j++)
	  {
	  	c.a[i][j]=0;
	  	for (int k=1;k<=N;k++)
	  	 c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%p)%p;
	  }
	return c;
}
data pow(data num,int x)
{
	data ans; clear(ans);
	for (int i=1;i<=N;i++) ans.a[i][i]=1;
	data base; change(base,num);
	while (x)
	{
		if (x&1)  ans=mul(ans,base);
		x>>=1;
		base=mul(base,base);
	}
	return ans;
}
data add(data x,data y)
{
	data c;
	for (int i=1;i<=N;i++)
	 for (int j=1;j<=N;j++)
	  c.a[i][j]=(x.a[i][j]+y.a[i][j])%p;
	return c;
}
data solve(int k)//当k为偶数时,s(k)=(1+A^(k/2))*(A+A^2+…+A^(k/2))
//当k为奇数时,s(k)=A+(A+A^(k/2+1))*(A+A^2+…+A^(k/2)
{
   if (k==1) return t2;  
   data b=pow(t2,(k+1)/2);  //对于偶数(k+1)/2=k/2,对于奇数 (k+1)/2=k/2+1; 
   data c=solve(k/2);  
   if (k%2)  
    return add(t2,mul(add(t2,b),c));  
   else  
    return mul(add(e,b),c);   

}
int main()
{
	freopen("a.in","r",stdin);
	e.a[1][1]=e.a[2][2]=1;
	a.a[1][1]=1; a.a[1][2]=1; a.a[2][1]=1; a.a[2][2]=0;
	while(scanf("%d%d%d%lld",&k,&b,&n,&p)!=EOF)
	{
		data t1=pow(a,b);
	    t2=pow(a,k);
		data ans=mul(t1,add(e,solve(n-1)));
		printf("%lld\n",ans.a[1][2]);
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值