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 on
- Both first
- horse1 first and horse2 second
- horse2 first and horse1 second
Input starts with an integer T (1000), denoting the number of test cases. Each case starts with a line containing an integer n ( 1n1000).
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.
3
1
2
3
Case 1: 1
Case 2: 3
Case 3: 13
题意:给定n匹马,要求出可能的排名情况(可能并列)
思路:在刘汝佳书紫书《入门经典第二版》上(具体322页),假设答案为f[n],假设有i个第一,则有c(n,i)中可能,接下来f[n-i]种情况,所以,f[n]=c[n,i]*f[n-i](i为1到n)
co
#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]);
}
}
}