您的位置:首页 > 其它

HOJ——2086 Fibonacci Convolution

2012-02-24 01:23 309 查看
Fibonacci is a very beautiful girl. Almost every year, HIT acmers invite her to join the HCPC contest and she will request acmers to solve a puzzle. She will come to join us only if somebody have solved the puzzle. This time when owen invites her to join HCPC
contest, she gives him a simple problem because owen is a handsome boy. She just let us calculate a fibonacci convolution.

The definition of fibonacci number is:

f0 =
0, f1 = 1, and for k >= 2, fk =
fk-1 + fk-2

The definition of fibonacci convolution is:

∑fkfn-k
where 0 <= k <= n



"" If your answer is accepted by Fibonacci, the beautiful girl will come and give you a kiss.


input

A single line contains a number n. 0 <= n <= 5000.


output

Calculate the fibonacci convolution and output the result.


Sample Input

4


Sample Output

5


先给出结论:此题的结果是

dp
=2*(dp[n-1]-dp[n-3])+(dp[n-2]-dp[n-4]);

当然我们追求的不是简单的找规律,而是寻求一种解法能让此类题目可以简单而形象的解出来。在看了组合数学中相关母函数的知识点后,我发现母函数真的很强大,不过要用在此题他还只是第一步。

我们设要求的结果母函数为:g(x)

x^0: f(0)*f(0)

x^1: f(0)*f(1)+f(1)*f(0)

x^2: f(0)*f(2)+f(1)*f(1)+f(2)*f(0)

......

x^n: f(0)*f(n)+f(1)*f(n-1)+f(2)*f(n-2)+......+f(n)*f(0)

......

所以:

g(x)=f(0)*f(0)+ [f(0)*f(1)+f(1)*f(0)]*x^1+[f(0)*f(2)+f(1)*f(1)+f(2)*f(0)]*x^2+......+[f(0)*f(n)+f(1)*f(n-1)+f(2)*f(n-2)+......+f(n)*f(0)]*x^n+......

=f(0)*[f(0)+f(1)*x^1+f(2)*x^2+......+f(n)*x^n+......]+f(1)*x^1*[f(0)+f(1)*x^1+f(2)*x^2+......+f(n)*x^n+......]

+......+f(n)*x^n*[f(0)+f(1)*x^1+f(2)*x^2+......+f(n)*x^n+......]+......

=[f(0)+f(1)*x^1+f(2)*x^2+......+f(n)*x^n+......]*[f(0)+f(1)*x^1+f(2)*x^2+......+f(n)*x^n+......]

很容易看出:f(0)+f(1)*x^1+f(2)*x^2+......+f(n)*x^n+......即飞普纳契数列的母函数,我们设为:p(x)

那么可以得到:

g(x)=p(x)^2;

根据飞普纳契数列的递推关系得到:

p(x)=x/(1-x-x^2);

那么:g (x)=x^2/[(1-x-x^2)^2]=x^2/(1-2*x-x^2+2*x^3+x^4);

现在分析一下分母:

从x^0到x^4,由于每次除法必然消去一项,因此总是最多剩下四项:依次设为:a(n),b(n),c(n),d(n)

模拟求解过程:

a(n-1) b(n-1) c(n-1) d(n-1)

a(n-1) -2*a(n-1) -a(n-1) 2*a(n-1) a(n-1)

= b(n-1)+2*a(n-1) c(n-1)+a(n-1) d(n-1)-2*a(n-1) -a(n-1)

那么就得出等式:

a(n)=b(n-1)+2*a(n-1);

b(n)= c(n-1)+a(n-1);

c(n)= d(n-1)-2*a(n-1);

d(n)=-a(n-1);

化简即可得出:

a
=2*(a[n-1]-a[n-3])+(a[n-2]-a[n-4]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: