UVA - 12034 Race —递推

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
题目大意:n匹马进行赛跑,每匹马都是不一样的,求赛跑结果数。
思路:多一匹马,可能多一种比赛名次,也可能不变。
设F(i,j)表示有i匹马是有j种名次的结果数,所以:
F(i+1,j)=F(i,j-1)*j+F(i,j)*j)*j;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
int t,n;
long long a[1010][1010];
int ans[1010]; 
int main(){
    a[1][1] = 1;
    a[2][1] = 1;
    a[2][2] = 2;
    ans[1] = 1;
    ans[2] = 3;
    for(int i = 3 ; i < 1010 ; ++i){
        long long sum = 0;
        for(int j = 1 ; j <= i ; ++j){
            a[i][j] = (a[i-1][j-1] + a[i-1][j])%10056 *j % 10056;
            sum = (sum + a[i][j]) % 10056;
        }
        ans[i] = sum;
    }
    scanf("%d",&t);
    for(int w = 1 ; w <= t ; ++w){
        scanf("%d",&n);
        printf("Case %d: %d\n",w,ans[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Love_Yourself_LQM/article/details/81711245
今日推荐