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

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
 
Source
 
思路:
先进行组合,然后再错排
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 long long int c,m,n,cuopai[25];
 4 long long int C(int n,int m){
 5     if(m == n || m == 0)
 6         return 1;
 7     else if(m == 1)
 8         return n;
 9     else
10         return C(n - 1,m - 1) + C(n - 1,m);
11 }
12 void init(){
13     cuopai[1] = 0,cuopai[2] = 1;
14     for(int i = 3;i <= 20;i++)
15         cuopai[i] = (i - 1) * (cuopai[i - 2] + cuopai[i - 1]);
16     return;
17 }
18 int main(){
19     init();//错排打表
20     scanf("%lld",&c);
21     while(c--){
22         scanf("%lld%lld",&n,&m);
23         printf("%lld\n",C(n,m) * cuopai[m]);
24     }
25     return 0;
26 }
View Code

 

posted @ 2018-10-04 13:18  永不&言弃  阅读(319)  评论(0编辑  收藏  举报