1245.这题超难的,建议先做其他的,看都不要看
一.题目大意:
问 组合数C(n,m)的值。
T组样例,0<n,m<60。
二.分析:
一开始我觉得是简单的运用组合公式,阶乘计算一下就好了,然后就爆了。
所以这到题我就觉得要简化计算,就采用了边乘边除。
但这题最好还是用组合数的模板进行处理。真香!
三.代码实现:
常规思路的实现:
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int d=0;d<t;d++)
{
int n,m;
scanf("%d %d",&n,&m);
if(m>n) swap(n,m);
if(n==m) printf("1");
else{
int b;
b = max(m,n-m);
long long ans;
ans = 1;
for(int a=n;a>b;a--)
{
ans = ans * a/(n-a+1);
}
printf("%lld",ans);
}
if(d!=t-1) printf("\n");
}
return 0;
}
组合数模板的实现:
#include <cstdio>
using namespace std;
long long C[1005][1005];
int main()
{
C[0][0] = 1;
for (int i = 1; i <= 1000; i++)
{
C[i][0] = 1;
for (int j = 1; j <= 1000; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}//第一部分为打表过程
int t;
scanf("%d",&t);
for(int a=0;a<t;a++)
{
int n,m;
scanf("%d %d",&n,&m);
printf("%lld",C[n][m]);
if(a!=t-1) printf("\n");
}//根据题意,选择要输出的数据。
return 0;
}
四.题目网址:
https://sdnuoj.rainng.com/problem/show/1245。