标签:
公式:(C(m+n, n)-C(m+n, m+1)) * m! * n! 化简即 (m+n)! * (m-n+1) / (m+1) 由公式:C(m, n) = m! / (n!*(m-n)!)推导得来 (C(m+n, n)-C(m+n, m+1)) * m! * n!:总共的排列减去违法的排列,又因为每个人不一样再乘以本身的全排列
#include<stdio.h> #include<string.h> # define N 1010 int a[N], m, n; void Fact() { int i, j, s, r = 0; for (i = 2; i <= (m+n); i++) { for (j = 0; j < N; j++) { s = a[j] * i + r; r = s / 10; a[j] = s % 10; } } } //求出(m+n)! void Sum() { int i, j, c = m-n+1, d = m+1, b[N], k = 0, s, r = 0; for (i = 0; i < N; i++) a[i] *= c; for (i = 0; i < N-1; i++) { if (a[i] > 9) { a[i+1] += a[i] / 10; a[i] %= 10; } } //求出(m+n)! * (m-n+1) i = N-1; while (a[i] == 0) i--; for (j = i; j >= 0; j--) b[k++] = a[j]; for (i = 0; i < k; i++) { s = b[i] + 10*r; r = s % d; b[i] = s / d; } //求出(m+n)! * (m-n+1) / (m+1) i = 0; while (b[i] == 0) i++; for (j = i; j < k; j++) printf("%d", b[j]); printf("\n"); } int main () { int k = 0; while (scanf("%d %d", &m, &n), m+n) { k++; printf("Test #%d:\n", k); if (m < n) printf("0\n"); //要是拿100的人比拿50的人还多的话,那么不存在这种排法 else { memset(a, 0, sizeof(a)); a[0] = 1; Fact(); Sum(); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4453170.html