Uva--12034(数学,组合数)
2014-09-03 23:59:30
Race |
Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded and wandered around, even in their holidays. They passed several months in this way. But everything has an end. A holy person, Munsiji came into their life. Munsiji took them to derby (horse racing). Munsiji enjoyed the race, but as usual Disky and Sooma did their as usual task instead of passing some romantic moments. They were thinking- in how many ways a race can finish! Who knows, maybe this is their romance!
In a race there are n horses. You have to output the number of ways the race can finish. Note that, more than one horse may get the same position. For example, 2 horses can finish in 3 ways.
- Both first
- horse1 first and horse2 second
- horse2 first and horse1 second
Input
Input starts with an integer T (1000), denoting the number of test cases. Each case starts with a line containing an integer n ( 1n1000).
Output
For each case, print the case number and the number of ways the race can finish. The result can be very large, print the result modulo 10056.
Sample Input
3 1 2 3
Sample Output
Case 1: 1 Case 2: 3 Case 3: 13
思路:可以用计数DP来做,这里为了练一下组合数,所以敲了数学版的,f(n) = ∑ C(n,i) * f(n - i),要注意的是这里的组合数由于要取模,故不能用递推式来求,要用
C(n,m) = C(n - 1,m) + C(n - 1,m - 1)来求。而且求 C 和求 f 的过程可以合并。
1 /************************************************************************* 2 > File Name: 12034.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 03 Sep 2014 10:35:11 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 const int mod = 10056; 16 17 int Case,n; 18 int c[1005][1005]; 19 int f[1005]; 20 21 void Pre(){ 22 f[0] = 1; 23 for(int i = 1; i <= 1000; ++i){ 24 c[i - 1][0] = 1; 25 for(int j = 1; j <= i; ++j){ 26 c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; 27 f[i] = (f[i] + c[i][j] * f[i - j]) % mod; 28 } 29 } 30 } 31 32 int main(){ 33 Pre(); 34 scanf("%d",&Case); 35 for(int t = 1; t <= Case; ++t){ 36 scanf("%d",&n); 37 printf("Case %d: %d\n",t,f[n]); 38 } 39 return 0; 40 }