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 (1 ≤ n ≤ 1000).
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
问题链接:UVA12034 Race
问题简述:(略)
问题分析:
求n匹马可能的排列情况,可以并列 。
dp[i][j]表示i匹马j个排名的情况,则dp[i-1][j]和dp[i-1][j-1]都有j个插入可能。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C语言程序如下:
/* UVA12034 Race */
#include <stdio.h>
#include <string.h>
#define N 1000
#define MOD 10056
int dp[N + 1][N + 1], ans[N + 1];
void init_table()
{
int i, j;
memset(dp, 0, sizeof(dp));
memset(ans, 0, sizeof(ans));
for(i = 1; i <= N; i++) {
dp[i][1] = 1;
for(j = 2; j <= i; j++)
dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) * j % MOD;
for(j = 1; j <= i; j++)
ans[i] = (ans[i] + dp[i][j]) % MOD;
}
}
int main(void)
{
init_table();
int t, n, i;
scanf("%d", &t);
for(i = 1; i <= t; i++) {
scanf("%d", &n);
printf("Case %d: %d\n", i, ans[n]);
}
return 0;
}