HOJ—Generalized Fibonacci

本文深入探讨了Fibonacci序列的通用形式,并通过近似估计技术解决了一个具有挑战性的数学问题。文章详细介绍了如何通过特征根求解通项公式,以及在特定条件下将复杂表达式简化以避免数值溢出。通过实例分析,读者能够掌握一种通用的求解方法,适用于Fibonacci序列的扩展形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Generalized Fibonacci

 Source : The 34th ACM/ICPC Asia Regional Harbin Preliminary
 Time limit : 1 sec Memory limit : 64 M

Submitted : 39, Accepted : 17

Description

ACMers of HIT like to play with fibonacci sequences. Today, let's consider a generalized fibonacci sequence {an}. Given a0, a1 and a positive integer p and an integer q, with an+2 = p*an+1+q*an(n >= 0), we can get any element of {an}.

However, nowadays, algorithms are always required to be scalable, so we need to consider problems with n up to 105. Fortunately, we do not need the exact values. Instead, you just need to tell how many digits there are.

For simplicity, you can safely assume that {an} will be non-negative with p2+4q >= 0.

Input

There will be no more than 100 test cases, each of which has five integers a0, a1, p, q, n.

0 <= a0, a1 <= 10000 are the first two elements of the sequence. 1 <= p <= 10000 and -10000 <= q <= 10000 are parameters of the recursive equation. 0 <= n <= 100000 denotes that an is what we want to calculate.

Process to the end of the file.

Output

One integer on a single line for each case, indicating the digits an contains.

Sample Input

0 1 1 1 6
0 1 1 1 7
8466 5819 4001 4757 99999

Sample Output

1
2
360227
 
 
 
 
一、观察此题不难发现此题是个近似估计的题目,此题同第一篇Fibonacci,不过与其比较更具一般性。通项公式很容易求出,可以预见:
1.当特征值两个相同时,
a(n)=(A*n+B)*x^n;
2.当特征值两个不同时,
a(n)=A*x^n+B*y^n;
二、研究两种形式
对于1就很简单,直接取log就行。而对于第二种需要一定的变化,这个变化与求极限很容易就联想到一块。
如:(2^n+3^n)/3^n,求极限我们利用的是一个小于1的数的指数函数是单调递减的,且趋向于0。这里我们
解决的是越界问题,所以也可以拿来用。
比如:log(2^n+3^n),先求内部当然会越界。如果变为:log((2^n+3^n)/3^n*3^n)=log((2/3)^n+1)+n*log3
这样对于前一项:log((2/3)^n+1)是单调递减的,那么就能保证绝对不会越界的的。同样的思路,解决此题
就很简单了,先求通项公式,按上述思想变化一下,然后一切就OK了!
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值