Dynamic planning _ Hundred 1664 put apples

Source: Internet
Author: User

1 #define_crt_secure_no_warnings2#include <stdio.h>3#include <math.h>4#include <algorithm>5#include <stdlib.h>6#include <vector>7#include <map>8#include <queue>9#include <string>Ten#include <iostream> One#include <ctype.h> A#include <string.h> -#include <Set> -#include <stack> the#include <functional> - using namespacestd; - #defineSize 10001 - #defineMAXN 1<<30 + intdp[size][ One]; - /*The type I can put on a J plate. + Dp[i][j]=dp[i][j-1]+dp[i-j][j] A I put an apple on a J plate, at only a few plates are empty, dp[i][j-1] - No plate is empty, dp[i-j][j], the same way every dish is removed from an apple. - if the number of plates is greater than the number of apples then i>j - Dp[i][j]=dp[j][j] -  - find the way to export!! is to identify the elements that were initially filled out to derive in First - dp (1,0) +DP (0,1)//DP (=DP) is the first element we fill in the form with a value of 1 to dp (2,1) =DP (2,0) +DP (up to)//its value is 1 obviously the exit is m==0 and n==0 section above two equations can know DP (i,0) =0 dp (0,J) =1; + dp (2,2) =DP (2,0) +DP (UP) - */ the intSolveintNintm) { *     if(Dp[n][m]! =-1)returnDp[n][m]; $     if(n = =0) {//look at the exit, one way is to gradually reduce the n, may eventually reduce to 0Panax NotoginsengDP[N][M] =1; -         return 1; the     } +     if(M = =0) { ADP[N][M] =0; the         return 0; +     } -     if(M >N) { $dp[n][m]=solve (n, n); $         returnDp[n][m]; -     } -  theDp[n][m]=solve (N-m, M) + Solve (n, M-1);//can walk to this certainly is n-m>=1, but the export condition Dp[1][m] because the condition that the topic gives has the fruit number is 1, does not need us to define, its value is 1 -     returnDp[n][m];Wuyi  the } - intMain () { Wu  -     intT; AboutCIN >>T; $      while(t--){ -         intN, M; -CIN >> N >>m; -          for(inti =0; I <= N; i++) A              for(intj =0; J <= M; J + +) +DP[I][J] =-1; thecout << Solve (n, m) <<Endl; -     } $System"Pause"); the     return 0; the}

Dynamic planning _ Hundred 1664 put apples

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.