Huawei OJ Platform--putting apples (Typical integer division problem)

Source: Internet
Author: User
Tags integer division

Title Description:

Input m,n, respectively, the number of apples and the total number of plates, the number of apples required to output an n plate ( note 511 and 151 is a case ), such as input 7 3 output 8 ((7), (6,1), (5,2), (4,3), (5,1,1), (4,2,1) , (3,3,1), (3,2,2))

Ideas:

The most typical solution to integer decomposition , such as a given n apples, put the apples on the K-plate, allow the tray to be empty, may wish to set F (n, K) (The Edge condition is when n = 0, 1 o'clock, return 1, when k = 1 o'clock, return 1) to indicate the results, analysis can know there are two methods, a There is a free disk, one is not available.

You can know that there is at least one apple on each plate in the case of an empty disk, which means that the total number of such cases is f (n-k, K).

In the case of a free disk, we can assume that the last plate is empty, then the total number of such cases is f (n, K-1) (no need to consider the case of multiple plates, recursion will inevitably occur)

So The state transfer equation is f (n, k) = f (n-k, K) + f (n, k-1)

1 ImportJava.util.Scanner;2 3 /**4 * Put m the same apples on n identical plates, allow some plates to be empty,5 * How many different methods are there? (denoted by k) 5,1,1 and 1,5,1 are the same kind of sub-method. 6  */7  Public classPlayapples {8 9      Public Static voidMain (string[] args) {Ten         //Input Read Parameters OneScanner cin =NewScanner (system.in);  A         intApples =cin.nextint (); -         intPlanes =cin.nextint (); - cin.close (); the          - System.out.println (Count (Apples,planes)); -  -     } +  -     /** + * Most typical integer decomposition A * For example, for a given n apples, put the apples on K-plates, allow the plates to be empty, and set F (M, N) at * (Edge condition is when m = = 0, 1 o'clock, returns 1, when n = = 1 o'clock, returns 1) indicates the result, - * Analysis can know there are two ways to put, one is a free disk, one is not empty disk, - * You can know that there is at least one apple per plate in the case of an empty disk, which means that the total number of such cases is f (n-k, K).  - * In the case of an empty disk, we can assume that the last plate is empty, then the total number of such cases is f (n, K-1) (no need to consider the case of multiple plates empty, the recursion will inevitably occur) - * So the state transfer equation is f (n, k) = f (n-k, K) + f (n, k-1).  -      *  in * And if it is not allowed to have empty plates, it can be introduced by the above situation, - * Set D (n, K) to put n apples on K-plates, the total number of methods that do not allow empty plates, to * There is f (n, K) =σ (1 <= i <= k) d (n, i) + * So D (n, k) = f (n, K)-F (n, k-1) -      *  the      * @paramm number of apples *      * @paramn Number of plates $      * @returnPanax Notoginseng      */ -     Private Static intCountintMintN) { the         //N is 0 is wrong, so return 0 +         if(n = = 0){ A             return0 ; the         } +         //m = = 0, 1 o'clock and n = = 1 o'clock there is only one method of release -         if(m = = 0 | | n = = 1 | | m = = 1 ){ $             return1 ; $}Else if(M < 0){ -             //m < 0 o'clock is also the wrong situation, so return 0 -             return0 ; the}Else{ -             //Recursive invocationWuyi             returnCount (m-n,n) + count (m,n-1) ; the         }     -     } Wu}
Code

Extended:

if it is not allowed to have empty plates , it can be introduced by the above situation, set D (n, K) to put n apples on the K-plate, not allowed to have empty plates of the total number of methods, there is

F (n, K) =σ (1 <= i <= k) d (n, i) so D (n, k) = f (n, K)-F (n, k-1)

Huawei OJ Platform-put apples (Typical integer division problem)

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.