POJ1664 count DP

Source: Internet
Author: User
 

Question Portal http://poj.org/problem? Id = 1664

Set $ dp [I] [j] $ to the total number of $ I $ apples on $ j $ plates.

$1. $ When the number of apples is less than the number of plates $ (M <N) $, the remaining $ N-M $ plates are empty, and the problem is equivalent to placing an apple in $ M $ plates: $ dp [M] [N] = dp [M] [M] $

$2. $ When the number of apples is greater than or equal to the number of dishes $ (M \ geq N) $, it can be broken down into at least one Apple for all dishes $ (dp [M-N] [N]) $ and at least one plate is empty $ (dp [M] [N-1]) $ merge of the two subresults: $ dp [M] [N] = dp [M-N] [N] + dp [M] [N-1] $

$3. $ recursive exit:

$ Dp [M] [1] = 1; \ dp [0] [N] = 1 $

Note: Why not use $ M = 1 $? There is only one fruit left. However, for example, if $ dp [3] [3] = dp [3] [2] + dp [0] [3] $, $ M $ is $0 $, data is not found at exit $ M = 1 $.

/******************************** Author: jusonalienEmail: [email protected] school: South China Normal UniversityOrigin: POJ *********************************/# include <iostream> # include <cstdio> # include <map> # include <cstring> # include <string> # include <set> # include <queue> using namespace std; int dp (int m, int n) {// recursive version if (m = 0 | n = 1) return 1; if (m <n) return dp (m, m); else return (dp (m-n, n) + dp (m, n-1);} int DP [15] [15]; void solve () {// recursive version for (int I = 0; I <= 12; ++ I) DP [0] [I] = 1; for (int I = 0; I <= 12; ++ I) DP [I] [1] = 1; for (int I = 1; I <= 10; ++ I) {for (int j = 1; j <= 10; ++ j) {if (I <j) DP [I] [j] = DP [I] [I]; else DP [I] [j] = DP [I-j] [j] + DP [I] [j-1] ;}} int main () {int m, n, t; solve (); scanf ("% d", & t); while (t --) {scanf ("% d", & m, & n ); printf ("% d \ n", DP [m] [n]); // printf ("% d \ n", dp (m, n ));} return 0 ;}

POJ1664 count DP

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.