UVA 12034 Race【组合数+递推】

版权声明:转载什么的好说,附上友链就ojek了,同为咸鱼,一起学习。<br> https://blog.csdn.net/sodacoco/article/details/88830030

参看资料:

https://blog.csdn.net/sodacoco/article/details/86497758

http://www.cnblogs.com/shingen/p/7590247.html

https://www.cnblogs.com/long98/p/10352209.html


题目:

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

题目大意: 

       A,B两人赛跑,可能出现三种情况:

       1、A,B并列第一  2、A第一,B第二  3、B第一,A第二

       现在有N个人赛跑,问可能出现多少种情况,答案对10056取模。

解题思路:

       C(n,k) = C(n-1,k-1) + C(n-1,k);

       假设答案为 f [n],假设有 i 个第一,则有 c(n,i)中可能;

       接下来 f [ n-i ]种情况,所以,f [ n ] = c [ n , i ] * f [ n - i ] ( i 为 1 到 n );

实现代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int MOD=10056;
const int N=1005;
//组合数打表模板,适用于N<=3000
//c[i][j]表示从i个中选j个的选法。

int C[N][N],F[N];

void get_C(int maxn){
    C[0][0] = 1;
    for(int i=1;i<=maxn;i++){
        C[i][0] = 1;
        for(int j=1;j<=i;j++)
            //C[i][j] = C[i-1][j]+C[i-1][j-1];
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD;
    }
}

int main(){
    get_C(1001);
    //cout<<"!!!"<<endl;
    F[0]=1;
    for(int i=1;i<=1000;i++){
        F[i]=0;
        for(int j=1;j<=i;j++){
            F[i]=(F[i]+C[i][j]*F[i-j])%MOD;
        }
    }
    int t,n;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        scanf("%d",&n);
        printf("Case %d: %d\n",i,F[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/88830030
今日推荐