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.
A single line contains a number n. 0 <= n <= 5000.
Calculate the fibonacci convolution and output the result.
先给出结论:此题的结果是
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]);
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]);
相关文章推荐
- HOJ1102 计算几何 判断两个线段是否会相交
- HOJ 1163
- HOJ 1008 How many n
- hoj 10511 、zoj 1962 ——100位数内的fibonacci
- hoj1740
- HOJ Journey to Tibet(小毛病大问题,下标搞混了,导致很严重的后果)
- HOJ Pearls-学习DP1
- HOJ Magic Bracelet polya置换第二个里程碑
- HOJ-10513 Allocation Scheme[简单DFS]
- HOJ---11525 Matchsticks
- HOJ 2576 Simple Calculation 容斥原理(DFS)
- HOJ 1081 Scramble Sort
- hoj 2086 A1 = ?
- hoj 2275 树状数组 水··不能int 64
- HOJ 2892 YY, and YY again
- HOJ 2267 Alice and Bob
- HOJ 2012 Sort and Merge
- HOJ 1637 HangOver
- HOJ 1439 The Circumference of the Circle
- hoj 1868 八数码(双广+hash)