您的位置:首页 > 其它

uva 12034 Race(dp)

2014-05-06 10:43 309 查看



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 ( 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


容易找到dp[i][j]表示i个数种选j个相同的方案数,转移方程详见代码。

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1000 + 5;
const int mod = 10056;

int dp[maxn][maxn];

void solve(){
    memset(dp, 0, sizeof(dp));
    dp[1][0] = dp[1][1] = 1;
    for(int i = 2;i < maxn;i++){
        dp[i][0] = (dp[i-1][0]*i) % mod;
        dp[i][i] += dp[i][0];
        for(int j = 1;j < i-1;j++){
            dp[i][j] = ((i-j)*(dp[i-1][j] + dp[i-1][j-1])) % mod;
            dp[i][i] += dp[i][j];
        }
        dp[i][i-1] = 1;
        dp[i][i] = (dp[i][i] + dp[i][i-1]) % mod;
    }
}

int main(){
    int t,kase = 0;
    solve();
    scanf("%d", &t);
    while(t--){
        kase++;
        int n;
        scanf("%d", &n);
        printf("Case %d: %d\n", kase, dp

);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: