问题 A: 组合数

唯一分解定理在此

问题 A: 组合数

时间限制: 1 Sec  内存限制: 128 MB
提交: 1938  解决: 147
[提交] [状态] [命题人:jsu_admin]

题目描述

求组合数C(N,M),以及C(N,M)因子个数。

输入

N和M,其中0<=M<=N<=50,以EOF结束。

输出

该组合数结果

样例输入 Copy

3 2
4 2

样例输出 Copy

3 2
6 4
#include<cstdio>
#include<cstring> 
using namespace std;
typedef long long ll;
ll ans1;
int a[60];
ll C(int n,int m)
{
	memset(a,0,sizeof(a));
	for(int i=n;i>=n-m+1;i--)
	{
		int t=i;
		for(int j=2;j<=t;j++)
			while(t%j==0)a[j]++,t/=j;
	}
	for(int i=2;i<=m;i++)
	{
		int t=i;
		for(int j=2;j<=t;j++)
			while(t%j==0)	a[j]--,t/=j;
	}
	ll ans=1;
	for(int i=2;i<60;i++)
		if(a[i]) ans*=a[i]+1;
	ans1=1;
	for(int i=2;i<60;i++)
		if(a[i])
			for(int j=1;j<=a[i];j++) ans1*=i;
	return ans;
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		ll ans=C(n,m);
		printf("%lld %lld\n",ans1,ans);
	}
} 

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/85308246