(UVA

来源:互联网 发布:java获取当前年月日 编辑:程序博客网 时间:2024/06/06 20:44

(UVA - 12034)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.

  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

题目大意:两匹马(记为1,2)赛跑可以有:1快2慢;1,2同时到达终点;1慢2快这样3种比赛结果。现有n匹马赛马,问比赛有多少种可能的结果。由于这个结果数可以很大,所以将结果%10056输出。

思路:设dp[i][j]表示前i匹马产生j种名次的比赛结果数,我们以第i匹马为研究对象,第i匹马的加入对结果可能有两种影响:1.第i匹马的加入没有增加名次种数,那么第i匹马可以是j种名次中的任意一种,即dp[i][j]=j*dp[i-1][j];2.第i匹马的加入是名次种数增加了1,那么它可以是前i-1匹马j-1种名次中增加一个不同的名次,而这个名次有j种可能性,即dp[i][j]=j*dp[i-1][j-1]。综上所述,可得状态转移方程:
dp[i][j]=j*(dp[i-1][j]+dp[i-1][j-1])。(初值为dp[1][1]=1)
ps: 结果不要忘了%10056

#include<cstdio>using namespace std;const int maxn=1005;int dp[maxn][maxn];const int mod=10056;int res[maxn];int main(){    int T,n,kase=1;    scanf("%d",&T);    dp[1][1]=1;    for(int i=2;i<=1000;i++)        for(int j=1;j<=i;j++)            dp[i][j]=j*(dp[i-1][j]+dp[i-1][j-1])%mod;    int ans;    for(int i=1;i<=1000;i++)    {        ans=0;        for(int j=1;j<=i;j++)            ans=(ans+dp[i][j])%mod;        res[i]=ans;    }    while(T--)    {        scanf("%d",&n);        printf("Case %d: %d\n",kase++,res[n]);    }    return 0;}
原创粉丝点击