Hdu--1331--function Run fun--Memory Search

Source: Internet
Author: User

Function Run FunTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 2376 Accepted Submission (s): 1187

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
Test instructions: Modify this recursion to you so that he can handle larger data

Parsing, A,b,c is controlled between 1~20, so memory of those data in a memory-search way prevents duplication.


#include <iostream> #include <cstdio> #include <cstring>using namespaceStd;intDp[ A][ A][ A],S=0; /  /Records prevent duplicate searchesintW(intA,intB,intC){if(A<=0||B<=0||C<=0)return1;if(A> -||B> -||C> -)returnW( -, -, -);if(Dp[A][B][C])returnDp[A][B][C]; /  /Use memory points to prevent duplicationif(A<B&&B<C)returnDp[A][B][C]=W(A,B,C-1)+W(A,B-1,C-1)-W(A,B-1,C);returnDp[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);}intMain (void){intA,B,C; while(~scanf("%d%d%d",&A,&B,&C))    {if(A==-1&&B==-1&&C==-1) Break;Memset(Dp,0,sizeof(Dp));Printf("W (%d,%d,%d) =%d\n",A,B,C,W(A,B,C)); }return0;}

Inexplicably, directly to the memory of the idea of adding in, it was really white alive AH!!


Hdu--1331--function Run fun--Memory Search

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.