0
点赞
收藏
分享

微信扫一扫

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.

  1. Both first
  2. horse1 first and horse2 second
  3. h
  4. orse2 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人赛马时名次的可能性的个数除以10056的余数(注意可以有好几个人并列第一)。

分析:

我们设答案为ans(n).假设第一名有i个人,有C(n,i)种可能性,这样我们就可以把i个人删去,继续递推下去,接下来有f(n-i)种可能性,因此答案为∑C(n,i)f(n-i)。
 

#include <stdio.h>  
#include <string.h>
#include <iostream>
#define N 1005
using namespace std;
const int MOD=10056;
typedef long long ll;
ll ans[N],C[N][N];

void init()
{
int i,j;
for(i=0; i<N; ++i)
{
C[0][i] = 0;
C[i][0] = 1;
}
for(i=1; i<N; ++i)
{
for(j=1; j<N; ++j)
C[i][j] = (C[i-1][j] + C[i-1][j-1]) % MOD;
}
for(int i=1;i<N;i++)
{
ans[i]=1;
for(int j=1;j<i;j++)
{
ans[i]+=C[i][j]*ans[i-j];
ans[i]=ans[i]%MOD;
}
}
}

int main()
{

int T;
scanf("%d",&T);
int k=1;
init();
while(T--)
{

int n;
scanf("%d",&n);
if(n==0) {
printf("Case %d: %d\n",k++,0);
continue;
}
printf("Case %d: %lld\n",k++,ans[n]);
}
return 0;
}

 

举报

相关推荐

0 条评论