CSU暑期集训day04_动态规划_赛马

题目:

    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

题目大意:

    给定马的数量,求赛马的可能结果的数量,每匹马是不同的,它们可能一部分或全部并列到达终点。

解题思路:

    设num[p][q]表示p匹马分为q批到达终点的可能结果的总数,显然p大于等于q,根据第p匹马到达终点的方式分类:若第p匹马是和某一或某些马同时到达终点,其余p-1匹马到达终点的结果数是num[p-1][q],而且第p匹马可能是q批次中的任一批,所以这种类别下的结果数是q*num[p-1][q];若第p匹马是单独到达终点的,它可能是q批中的任一批,其余p-1匹马到达终点结果数是num[p-1][q-1],总共是q*num[p-1][q-1]。因此得到转换方程num[p][q]=q*num[p-1][q]+q*num[p-1][q-1]。

代码:

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

/*long long int fun(int n,int j){
    if(n==1||j==1)return 1;
    if(n==j)return j*(fun(n-1,j-1))%10056;
    return (j*((fun(n-1,j)+fun(n-1,j-1))%10056))%10056;
}*/

int main(){
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        int n;
        cin>>n;
        /*vector<vector<int> > num(1002);
        for(int p=0;p<1002;p++){
            num[p].resize(1002);
            for(int q=0;q<1002;q++){
                num[p][q]=0;
            }
        }*///用vector会超时 
        
        int num[1002][1002]={0};//直接用数组部分编译器会报错,但是能AC 

        long long int ans[n+1]={0};
        num[1][1]=1;
        num[0][0]=1;
        for(int p=1;p<=n;p++){
            long long int sum=0;
            for(int q=1;q<=p;q++){
                num[p][q]=q*((num[p-1][q]+num[p-1][q-1])%10056)%10056;
                sum=(sum+num[p][q])%10056;
            }
            ans[p]=sum;
        }
        cout<<"Case "<<i<<": "<<ans[n]<<endl; 
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42774699/article/details/81226601