[ACM] POJ 1664 put apples (n the same ball into the same box of M)

Source: Internet
Author: User

Put the Apples
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 25952 Accepted: 16509

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 kind of sub-method.

Input

The first line is the number of test data t (0 <= T <= 20). 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 k is output in one line.

Sample Input

17 3

Sample Output

8

Source

[email protected]


Put n the same ball into the same box of M, ask how many ways.

Analysis: Some topics can be translated into such topics, such as having n the same ball, m the same box, requires at least a K ball in each box, ask how many ways: http://blog.csdn.net/duanxian0621/article/ details/7864791 method is: In advance in each box has put the K ball, then also left n-k*m a small ball, and then converted in order to, put n-k*m the same ball into m the same box inside, how many ways.

Sub-case discussion: A[i][j] represents the number of ways I put a small ball into a J box

① when the ball is placed, the smallest number of the box is empty, then the equivalent, I put a small ball into the j-1 box, that is a[i][j-1].

② when the ball is placed, the box with the fewest number of balls is not empty, so that there is at least one ball in each box, then put a small ball in each box beforehand, and there is a[i-j] [j].

So, the recursive formula for a[i][j]= A[i][j-1]+a[i-j][j], here is i>=j.

When i<j, no matter how to put, there will be empty box, it is advisable to take away an empty box (guaranteed to have an empty box), and then put I the same ball into the remaining j-1 the same box, there are a[i][j-1] methods, so a[i][j]=a[i][j-1].

Total: n the same ball into the same box

When I>=j, A[i][j]=a[i][j-1]+a[i-j][j];

When i<, A[i][j]=a[i][j-1], (think about it, can also be written here A[i][j]=a[i][i], because a[i][i]=a[i][i+1]=a[i][i+2]=a[i][i+3] ...

Initialize the A array code as:

int A[MAXN][MAXN];

void Prepare ()

{

for (int i=0;i<m;i++)

A[i][1]=a[0][i]=1;

for (int i=1;i<maxn;i++)

for (int j=2;j<maxn;j++)

if (j<=i)

A[i][j]= (a[i-j][j]+a[i][j-1])%mod;

Else

A[i][j]=a[i][i]; In fact, it's a[i][j-1], but it's the same as A[i][i.

}

Code:

#include <iostream>using namespace Std;const int maxn=11;int a[maxn][maxn];void init () {for    (int i=1;i<=10 ; i++)        a[0][i]=1,a[i][1]=1;    for (int i=1;i<maxn;i++) for        (int j=2;j<=maxn;j++)            if (j<=i)                a[i][j]=a[i][j-1]+a[i-j][j];            else                a[i][j]=a[i][i];} int main () {    init ();    int t,n,m;    cin>>t;    while (t--)    {        cin>>n>>m;        cout<<a[n][m]<<endl;    }    return 0;}


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.