Function Run Fun
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 2602 Accepted Submission (s): 1263
problem DescriptionWe all love recursion! Don ' t we?
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 > B > or C >, 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)
This is a easy function to implement. The problem is, if implemented directly, for moderate values of a, B and C (for example, a = d, B = d, c = All), the prog Ram takes hours to run because of the massive recursion.
InputThe input for your program would be a series of an integer triples, one per line, until the END-OF-FILE flag of-1-1-1. Using The above technique, you is to calculate W (A, B, c) efficiently and print the result.
OutputPrint The value for W (a,b,c) for each triple.
Sample Input
Sample Output
W (1, 1, 1) = 2w (2, 2, 2) = 4w (Ten, 4, 6) = 523w (-1, 7, 18) = 1
SourcePacific Northwest 1999
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1331
Title: Title
Title Analysis: For a situation of less than or equal to 0 direct output 1, if there is a greater than 20, then search W (20,20,20), the other direct search W (a,b,c) with the memory of the search can be
#include <cstdio> #include <cstring>int const MAX = 60;int dp[max][max][max];int DFS (int A, int b, int c) {i F (a <= 0 | | b <= 0 | | c <= 0) return 1; if (Dp[a][b][c]) return dp[a][b][c]; if (a < b && B < c) return dp[a][b][c] = DFS (A, B, c-1) + DFS (A, b-1, c-1)-Dfs (A, b-1, c); else return dp[a][b][c] = DFS (A-1, B, c) + DFS (A-1, b-1, C) + DFS (A-1, B, C-1)-DFS (A-1, B-1, C -1);} int main () {int A, b, C; while (scanf ("%d%d", &a, &b, &c)! = EOF) {if (a = =-1 && b = =-1 && c = =-1) Break if (a <= 0 | | b <= 0 | | C <= 0) {printf ("W (%d,%d,%d) = 1\n", A, B, c); Continue } if (A > | | b > | | c >) {printf ("W (%d,%d,%d) =%d\n", A, B, C, DFS (20, 20, 2 0)); Continue } printf ("W (%d,%d,%d) =%d\n", A, B, C, DFS (A, B, c));} }
HDU 1331 Function Run Fun (basic memory Search)