Function run fun
InterviewTime limit1sMemory limit128 MB
Compute the recursive function w(a, b, c) with memoization for each query line until -1 -1 -1.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Recursion
- Solved
- No attempts yet
Problem
Recursive calls are exciting! Or are they?
A recursive function w(a, b, c) on three integers is defined as follows.
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)
Copying the definition into code is easy. Speed is the problem. The same arguments get recomputed over and over, so even a = 15, b = 15, c = 15 never produces an answer.
Given a, b, and c, write a program that computes w(a, b, c) and prints it.
Input
The input consists of several lines. Each line holds three integers a, b, and c separated by one space.
The last line is -1 -1 -1 and is not evaluated. That last line is the only one whose three values are all -1.
Output
For each input line, print one line in the form w(a, b, c) = result. Write the three integers inside the parentheses exactly as they were given, with one space after each comma and one space on either side of the equals sign.
Print nothing for the final -1 -1 -1 line.