#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
long long gcd(long long a,long long b)
{
return b == 0 ? a :gcd(b,a % b);
}
long long conbination(long long x,long long y)
{
long long i , j , l ,r;
if(x == 0 || y == 0) return 1;
y = min(y,x - y);
for(i = 1,l = 1,r = 1;i <= y;i++)
{
l = l*(x - i + 1);//未进行运算前分子
r = r * i;//未进行运算前分母
long long xx = gcd(l,r);
if(xx < 0) xx = -xx;
l /= xx;//运算后的值
r /= xx;//判断是否分母一进行约分
}
if(r > 1) l /= r;
return l;
}
int main(int argc, char const *argv[])
{
/* code */
long long n,m;
while(cin>>n>>m && (n || m))
printf("%lld\n",conbination(n,m));
return 0;
}
代码实现组合数
最新推荐文章于 2023-01-26 12:37:22 发布