Race
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 (![$ \le$](http://uva.onlinejudge.org/external/120/12034img1.png)
![$ \le$](http://uva.onlinejudge.org/external/120/12034img1.png)
![$ \le$](http://uva.onlinejudge.org/external/120/12034img1.png)
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,我们用dp[i][j]表示i匹马,最低排位是j的情况数,当多一匹马的时候,它能不改变原来的排名情况直接为1~j的任意一个排名,即dp[i+1][j] += dp[i][j] * j . 或者不改变原来的排名,自己的排名是j+1, 即dp[i+1][j+1] += dp[i][j] , 或者自己的排名变为k (1<=k<=j) ,并且原来排名不比k小的全部排名加1,即dp[i+1][j+1] += dp[i][j] * j;
代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <map>
#include <set>
#include <stdio.h>
#include <cmath>
#include <cassert>
#include <math.h>
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define rrep(i,b,a) for(int i = (b); i >= (a); --i)
#define clr(a,x) memset(a,(x),sizeof(a))
#define LL long long
#define eps 1e-9
#define mp make_pair
using namespace std;
const int maxn = 1000 + 5;
const int mod = 10056;
LL dp[maxn][maxn];
LL sum[maxn];
void pre_init()
{
dp[1][1] = 1;
rep(i,1,maxn-1) {
rep(j,1,i+1) {
dp[i+1][j] += dp[i][j] * j % mod;
dp[i+1][j+1] += dp[i][j] * (j+1) % mod;
if (dp[i+1][j] >= mod) dp[i+1][j] -= mod;
if (dp[i+1][j+1] >= mod) dp[i+1][j+1] -= mod;
sum[i] = (sum[i] + dp[i][j]) % mod;
}
}
}
int main()
{
// Getinput(); return 0;
#ifdef ACM
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif // ACM
pre_init();
int T; cin >> T;
rep(cas,1,T+1) {
int n; scanf("%d",&n);
printf("Case %d: %lld\n",cas,sum[n]);
}
}