【递推】Race UVA - 12034 【赛马】

【递推】Race UVA - 12034 【赛马】

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.

  1. Both first
  2. horse1 first and horse2 second
  3. 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种排名的数量。

  1. 考虑并列,即第i匹马和前面的马搭伙达到,即i 匹马和前i - 1匹马得到的排名数量相同,然后它可以在这j种排名中选一种并列。 即dp[i][j] = dp[i - 1][j] * j
  2. 没有并列,第i匹马比前面i - 1匹获得的排名不同,也就是多了一种新的排名状态,(也就是说前i - 1匹马有就 j - 1种排名状态)。那么可以将第i匹马插入到这j - 1 种排名的空隙照中,总共有 (j - 1)+1 (可以排在最后一匹马的后面,或者最前一匹马的前面)。即dp[i][j] = dp[i - 1][j - 1] * j

也就是dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) * j

初始状态:dp[i][1] = 1 dp[0][i] = 0

AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#define MOD 10056

using namespace std;

const int maxn = 1005;
long long dp[maxn][maxn];

int main()
{
    for(int i = 0; i <= 1000; i++)
    {
        dp[0][i] = 0;
        dp[i][1] = 1;
    }
    for(int i = 1; i <= 1000; i++)
    {
        for(int j = 2; j <= i; j++)
            dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD * j % MOD;
    }
    int t, n;
    scanf("%d", &t);
    for(int k = 1; k <= t; k++)
    {
        long long ans = 0;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            ans = (ans + dp[n][i]) % MOD;
        printf("Case %d: %lld\n", k, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Floraqiu/article/details/81224052