Uva12034 Race(递归)
Uva12034 Race
Description
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.
- Both first
- horse1 first and horse2 second
- 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 ( 1n1000).
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个人比赛的所有可能的名次种数。比如:n=2时,有A第一B第二、B第一A第二、AB并列第一三种名次。
思路
动态规划,思路:
dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%Mod*j%Mod;
含义是对于第j匹马,次数为与前面的马同时到达+单独到达的次数和。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAXN =1e3+10;
int ans[MAXN];
int h[MAXN][MAXN]={0};
int T;
const int MOD =10056;
int horse(){
for(int i=1;i<MAXN;i++){
h[i][1]=1;
for(int j=2;j<=i;j++){
h[i][j]=(h[i-1][j-1]*j%MOD+h[i-1][j]*j%MOD)%MOD;
}
for(int j=1;j<=i;j++){
ans[i]=(ans[i]+h[i][j])%MOD;
}
}
return 0;
}
int main(void){
horse();
scanf("%d",&T);
//freopen("out.txt","wa",stdout);
for(int i=0;i<T;i++){
int a=0;
scanf("%d",&a);
printf("Case %d: %d\n",i+1,ans[a]);
}
return 0;
}

浙公网安备 33010602011771号