#include <stdio.h>
long gcd(long a, long b);
void divideGcd(long *a, long *b);
long c(long n, long k);
int main()
{
long n, m;
while (scanf("%ld%ld", &n, &m) && (n + m))
{
printf("%ld\n", c(n, m));
}
return 0;
}
long gcd(long a, long b)
{
return b == 0?a:gcd(b, a % b);
}
void divideGcd(long *a, long *b)
{
long g = gcd(*a, *b);
*a = *a / g;
*b = *b / g;
}
long c(long n, long k)
{
long numerator = 1, denominator = 1, toMul, toDiv, i;
if (k > n / 2)
k = n - k;
for (i = k; i > 0; i--)
{
toMul = n - k + i;
toDiv = i;
divideGcd(&toMul, &toDiv);
divideGcd(&numerator, &toDiv);
divideGcd(&toMul, &denominator);
numerator *= toMul;
denominator *= toDiv;
}
return numerator / denominator;
}
UVa530 - Binomial Showdown
最新推荐文章于 2022-02-25 21:57:21 发布