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了!