#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 things taken %ld at a time is %ld exactly.\n", n, m, 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;
}
UVa369 - Combinations
最新推荐文章于 2021-05-29 12:47:15 发布