如何使用主定理来计算递归,分而治之
答
为什么你必须在这里使用主定理?它可以直接解决这样的:
T(n) = T(n-1) + n^3
T(n-1) = T(n-2) + (n-1)^3
T(n-2) = T(n-3) + (n-2)^3
. . .
. . .
. . .
T(1) = T(0) + 1^3
----------------------- (Add them all and cancel)
T(n) = T(0) + (n(n-1)/2)^2 (Sum of the cubes of the first n numbers)
因此,这是O(n^4)
答
T(n) = T(n-1) + n^3
= T(n-2) + n^3 + (n-1)^3
= T(n-i+1) + (n-i)^3 + ... + (n-1)^3 + n^3
= 1^3 + 2^3 + ... + (n/2)^3 + (n/2+1)^3 + ... + (n-1)^3
Throw bottom half and decrease the half top to n/2
> ((n/2)^3)*(n/2)
Ω(n^4)
Increase all to (n-1)
= 1^3 + 2^3 + ... + (n/2)^3 + (n/2+1)^3 + ... + (n-1)^3 < (n-1)^3*n = O(n^4)
T(n) = θ(n^4)
+0
好的解决方法谢谢,有没有办法直接计算1^3 + 3^3 + ... +(n + 2)^ 3? – flower
聪明的想法。 – flower
好吧,我明白了,但这样计算太复杂,谢谢你的回答。我提出了你的答案,但由于我的名声不到15,它还没有显示出来。 – flower
但就是这样。看看这个(https://brilliant.org/wiki/sum-of-n-n2-or-n3/)。解决“尝试自己”的挑战,那么你将获得更多关于如何解决序列的知识。 – Miraj50