POJ 1664 putting apples

Source: Internet
Author: User

POJ 1664 put the apples

Time Limit: + MS Memory Limit:10000KB 64bit IO Format:%i64d &%i64u

Description

Put M the same apples on N the same plate, allow some plates to be empty, ask how many different ways? (denoted by K )5,1,1 and 1,5,1 are the same sub-method.

Input

The first line is the number of test data T(0 <= T <=). Each of the following lines contains two integers M and N, separated by a space. 1<=m,n<=10.

Output

For each set of data entered M and N, the corresponding Kis output in one line.

Sample Input

1
3 ·

Sample Output

8

Problem Solving Analysis:

Set F (m,n) to M apples, n the number of different methods of the plate, first analysis of N:

1, when the n〉m, because the unnecessary plate useless, it can be redundant plate, that is f (m,n) =f (m,m).

2, when n<=m, in two cases: 1. At least one of the plates is empty, then f (m,n) =f (m,n-1); 2. All the plates are full, and test instructions and 5,1,1 are the same division. Therefore, from each plate to take out a number that does not affect its release, then in this case f (m,n) =f (M-n,n), in the case of n〈=m, the total number f (m,n) =f (m,n-1) +f (m-n,n);

Description of the recursive exit:

When the Apple does not have, that is, when the m=0 , defined as a situation. When n=1 , all apples have to be placed in the same basket, so there is only one situation.

In the process of recursion, in the first case, the value ofn is reduced, so the end will be reduced to n=1 , in the 12th case, the value ofm is reduced, the final m=0;

In the whole process of recursion, there are only two things to consider, one is the size comparison of m and N , the other is the entire recursive exit.

#include <stdio.h>

int s (int m,int n)

{

if (m==0| | N==1)

RETURN1;

if (n>m)

Returns (M,M);

Else

Returns (M,n-1) +s (m-n,n);

}

int main ()

{

Intt,x,m,n;

scanf ("%d", &t);

while (t--)

{

scanf ("%d%d", &m,&n);

X=s (M,n);

printf ("%d\n", X);

}

Return0;

}


This article is from the "My Heart to Stay" blog, please make sure to keep this source http://chengd415.blog.51cto.com/10570483/1683676

POJ 1664 putting 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.