下面为求取组合数的代码:
1 #include <stdio.h> 2 #define MAX 10009 3 int prime[168]; 4 void print(int *v, int length) 5 { 6 int i = 0; 7 for (; i < length; i++) 8 printf("%d ", v[i]); 9 putchar(' '); 10 } 11 12 int getPrime() 13 { 14 int i = 3, k = 1; 15 int j = 0; 16 int flag; 17 prime[0] = 2; 18 for (; i < 1000; i++) { 19 for (flag = 0, j = 0; j < k; j++) { 20 if (i % prime[j] == 0) { 21 flag = 1; 22 break; 23 } 24 } 25 if (!flag) 26 prime[k++] = i; 27 } 28 print(prime, 168); 29 } 30 31 int comb(int m, int n) 32 { 33 int count[168] = { 0 }; 34 int i = 0, j = 0; 35 int mm; 36 int mul = 1, tmp; 37 for (i = m; i > m - n; i--) { 38 mm = i; 39 for (j = 0; mm != 1; j++) { 40 if (mm % prime[j] == 0) { 41 mm /= prime[j]; 42 ++count[j]; 43 --j; 44 } 45 } 46 } 47 for (i = n; i >= 2; i--) { 48 mm = i; 49 for (j = 0; mm != 1; j++) 50 if (mm % prime[j] == 0) { 51 mm /= prime[j]; 52 --count[j]; 53 --j; 54 } 55 } 56 for (i = 0; i < 168; i++) { 57 tmp = count[i]; 58 while (tmp) { 59 mul = (mul * prime[i]) % MAX; 60 tmp--; 61 } 62 } 63 return mul; 64 } 65 66 int main() 67 { 68 getPrime(); 69 printf("%d ", comb(2, 1)); 70 return 0; 71 }