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

题目链接:https://vjudge.net/problem/HDU-2049

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

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


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
 
Recommend
lcy
 
 
 
 
题解:
部分错排,先在n个个中条线m个,然后这m个进行错排。总数: C[n][m] * D[m]。
 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 20+10;
18 
19 LL C[MAXN][MAXN], D[MAXN];
20 
21 void init()
22 {
23     memset(C, 0, sizeof(C));
24     for(int i = 0; i<MAXN; i++)
25     {
26         C[i][0] = 1;
27         for(int j = 1; j<=i; j++)
28             C[i][j] = C[i-1][j-1] + C[i-1][j];
29     }
30 
31     D[0] = 1; D[1] = 0;
32     for(int i = 2; i<MAXN; i++)
33         D[i] = 1LL*(i-1)*(D[i-1]+D[i-2]);
34 }
35 
36 int main()
37 {
38     init();
39     int T, n, m;
40     scanf("%d", &T);
41     while(T--)
42     {
43         scanf("%d%d", &n,&m);
44         printf("%lld
", 1LL*C[n][m]*D[m]);
45     }
46 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8324408.html