Hdu-1331-function Run Fun (Dynamic planning 3)

Source: Internet
Author: User

Description

We 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.

Input

The 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.

Output

Print the value for W (a,b,c) for each triple.

Sample Input

1 1 12 2 210 4 650 50 50-1 7 18-1-1-1

Sample Output

W (1, 1, 1) = 2w (2, 2, 2) = 4w (Ten, 4, 6) = 523w (-1, 7, 18) = 1

Similar to the dynamic planning, to avoid duplication of calculations, the previous count of the array to save, directly behind the use of it can be
#include <stdio.h>#include<string.h>inta,b,c;intans[ +][ +][ +];intWintAintBintc) {    if(a<=0|| b<=0|| c<=0)    {        return 1; }    if(a> -|| B> -|| C> -)    {        returnW -, -, -); }    if(Ans[a][b][c])returnAns[a][b][c]; if(a<b&&b<c) {Ans[a][b][c]=w (a,b,c-1) +w (a,b-1, C-1)-W (a,b-1, c); }    Else{Ans[a][b][c]=w (A-1, B,c) + W (A-1, B-1, c) + W (A-1, b,c-1)-W (A-1, B-1, C-1); }    returnans[a][b][c];}intMain () { while(SCANF ("%d%d%d", &a,&b,&c) &&a+b+c!=-3) {memset (ans,0,sizeof(ans));        W (A,B,C); printf ("W (%d,%d,%d) =%d\n", A,b,c,w (a,b,c)); }    return 0;}

Hdu-1331-function Run Fun (Dynamic planning 3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.