HDU 2049 考新郎-错排列

不容易系列之(4)——考新郎

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47243    Accepted Submission(s): 17353


 

Problem Description

国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:


首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排;
然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个.
最后,揭开盖头,如果找错了对象就要当众跪搓衣板...

看来做新郎也不是容易的事情...

假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能.

 

Input

输入数据的第一行是一个整数C,表示测试实例的个数,然后是C行数据,每行包含两个整数N和M(1<M<=N<=20)。

 

Output

对于每个测试实例,请输出一共有多少种发生这种情况的可能,每个实例的输出占一行。

 

Sample Input

 

2 2 2 3 2

 

Sample Output

 

1 3

 

Author

lcy

 

Source

递推求解专题练习(For Beginner)

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  2048 2045 2050 2046 2044 

题目思路,选择顺序对的,在乘上选择错误的人的错排列

直接上代码:

This is the link

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include <iomanip>
#include<list>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
// ios.sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了
LL c[22][22];
LL f[22];
int main()
{
    //求组合数
    c[0][0]=0;
    for(int i=1; i<=20; ++i)
    {
        for(int j=0; j<=i; ++j)
            if(!j||i==j)
                c[i][j]=1;
            else
                c[i][j]=c[i-1][j]+c[i-1][j-1];
    }
    //求错排列
    //错排列公式f(n)=(n-1)*(f(n-1)+f(n-2));
    f[1]=0;
    f[2]=1;
    for(int i=3; i<=20; ++i)
        f[i]=(i-1)*(f[i-1]+f[i-2]);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        LL ans=c[n][n-m]*f[m];
        printf("%lld\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/sdau_fangshifeng/article/details/81660335