不容易系列之(4)——考新郎
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27461 Accepted Submission(s): 10032
Problem Description
国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做”考新郎”,具体的操作是这样的:
首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排;
然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个.
最后,揭开盖头,如果找错了对象就要当众跪搓衣板…
看来做新郎也不是容易的事情…
假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能.
Input
输入数据的第一行是一个整数C,表示测试实例的个数,然后是C行数据,每行包含两个整数N和M(1
#include "cstring"
#include "cstdio"
#include "string.h"
#include "iostream"
#include "cmath"
using namespace std;
long long store[30][30];
long long Combination(long long x, long long y)
{
__int64 sum = 1;
for (int i = x; i >= x - y + 1; --i)
sum *= i;
for (int j = y; j >= 2; --j)
sum /= j;
return sum;
}
int main()
{
int ncase;
while (scanf("%d", &ncase) != EOF)
{
while (ncase--)
{
long long total, wrong;
scanf("%lld%lld", &total, &wrong);
long long a[30];
a[1] = 0;
a[2] = 1;
for (int i = 3; i <= wrong; i++)
{
a[i] = (i - 1)*(a[i - 1] + a[i - 2]);
}
printf("%lld\n", a[wrong] * Combination(total, wrong));
}
}
}