[ACM] Hard-to-use HDU 2049 series (4) -- Test groom (combination + error row)

Source: Internet
Author: User

During the National Day, the provincial capital Hz just held a grand collective wedding. in order to enrich the wedding, the SI Yi temporarily came up with an interesting program called "test groom ", the specific operation is as follows:


First, dress almost identical to each bride and place them in a row with a big red cover;
Then, let the groom look for his/her own bride. Each person is only allowed to find one, and many people are not allowed to find one.
Finally, uncover the cover. If you find the wrong object, you have to kneel in front of the public...

It seems that it is not easy to be a groom...

Suppose there are n newly-married couples, and m of them are looking for the wrong bride. How many possibilities does this happen.
 
The first line of input data is an integer c, indicating the number of test instances, followed by data in Row C, each row contains two integers, N and M (1 <m <= n <= 20 ).
 
Output for each test instance, please output a total of how many possibilities this situation may occur, the output of each instance occupies a line.
 
Sample Input
22 23 2
 
Sample output
13
 
Authorlcy


Solution:

Suppose there are n newly-married couples, and m of them are looking for the wrong bride. How many possibilities does this happen.

First, select m from N grooms and multiply them by the dislocation of M.

Note that the range of error Row 20 is greater than that of int type...

Code:

#include <iostream>using namespace std;const int maxn=21;typedef long long ll;ll f[maxn];ll c[maxn][maxn];void init(){    f[0]=1,f[1]=0;    for(int i=2;i<maxn;i++)        f[i]=(i-1)*(f[i-1]+f[i-2]);    c[0][0]=1;c[1][0]=1;c[1][1]=1;    for(int i=2;i<maxn;i++)    {        c[i][0]=c[i][i]=1;        for(int j=1;j<i;j++)            c[i][j]=c[i-1][j]+c[i-1][j-1];    }}int main(){    init();    int t,n,m;    cin>>t;    while(t--)    {        cin>>n>>m;        cout<<c[n][m]*f[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.