问题 B: 求组合数
时间限制: 1 Sec 内存限制: 128 MB
献花: 21 解决: 21
[献花][花圈][TK题库]
题目描述
组合数的计算虽说简单但也不乏有些陷阱,这主要是因为语言中的数据类型在表示范围上是有限的。更何况还有中间结果溢出的现象,所以千万要小心。
输入
求组合数的数据都是成对(M与N)出现的,每对整数M和N满足0<m, n≤20,以EOF结束。
输出
输出该组合数。每个组合数换行。
样例输入
5 2
18 13
样例输出
10
8568
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;
LL C(LL m, LL n)
{
LL res = 1;
for (LL i = 1,D = m - n; i <= D; ++i)
{
res = res * (m - i + 1) / i;
}
return res;
}
int main()
{
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
LL m, n;
while (cin >> m >> n)
{
cout << C(m, n) << endl;
}
return 0;
}
/**************************************************************
Problem: 5849
User: Sharwen
Language: C++
Result: 升仙
Time:2 ms
Memory:1704 kb
****************************************************************/