pku1579Function Run Fun动态规划题:递归函数(会TLE)

题目大意:

Consider a three-parameter recursive function w(a, b, c):

if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1

if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)

if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)

otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)

如果直接用递归函数肯定会tle,到目前为止碰到两种解决方案:

1)先用暴力法列出数值表,找规律,或用数学方法推导,直接找到递推式。

2)在程序开始先算出所有题目要求的数值范围,并保存,在主程序里直接调用就行了。(对于某些递归不适用)。

愚见。。。。。。有时候我自己都表达不清楚,以后发现错误了再修改咯。

这样一来,题目就很简单了。用题目所给的递推式事先算好所有值。

代码如下:

 

Code
原文地址:https://www.cnblogs.com/pandy/p/1371472.html