uva 12034——Race

来源:互联网 发布:骚男的淘宝店网址 编辑:程序博客网 时间:2024/06/05 16:36

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 monthsin this way. But everything has an end. A holy person, Munsiji came into theirlife. Munsiji took them to derby (horse racing). Munsiji enjoyed the race, butas usual Disky and Sooma did their as usual task instead of passing someromantic moments. They were thinking- in how many ways a race can finish! Whoknows, maybe this is their romance!

In a race thereare n horses. You have to output the number of ways the racecan finish. Note that, more than one horse may get the same position. Forexample, 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 casestarts with a line containing an integer n ( 1n1000).

Output 

For each case, print the case number and the number ofways the race can finish. The result can be very large, print the result modulo10056.

Sample Input 

3

1

2

3

Sample Output 

Case 1: 1

Case 2: 3

Case 3: 13

 

题意:给定n匹马,要求出可能的排名情况(可能并列)

思路:在刘汝佳书紫书《入门经典第二版》上(具体322页),假设答案为f[n],假设有i个第一,则有cn,i)中可能,接下来f[n-i]种情况,所以,f[n]=c[n,i]*f[n-i](i1n)

code:

#include <iostream>
#include <cstdio>
using namespace std;

const int N=1005;
const int mod=10056;
int f[N],c[N][N];

void init()
{
int i,j;
c[1][0]=c[1][1]=1;
for (i=2;i<N;i++)
{
c[i][0]=1;
for (j=1;j<=i/2;j++)
c[i][j]=c[i][i-j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
//f[1]=1;
for (i=1;i<N;i++)
{
f[i]=1;
for (j=1;j<i;j++)
{
f[i]+=c[i][j]*f[i-j];
f[i]%=mod;
}
}
}

int main()
{
int t,n;
init();
while (cin>>t)
{
for(int ca=1;ca<=t;ca++)
{
cin>>n;
printf("Case %d: %d\n",ca,f[n]);
}
}
}


0 0