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

可以开一个二维数组,d[i][j](i>=j),其中i代表有i匹马比赛,j表示到达终点有几组(应对同时到达的情况)

易知 d[2][1]=1;d[2][2]=2;

递推到第n匹,d[i][j]=d[i-1][j-1]*j+d[i-1][j]*j 代表新加入的马可以加入任意一组(后者)或在j-1组空隙里随便独成一组(前者)

 1 #include<iostream>
 2 #include<string.h>
 3 
 4 using namespace std;
 5 
 6 int f[1005][1005];
 7 int ff[1005];
 8 
 9 int main()
10 {
11     int T,s=1;
12     cin>>T;
13     memset(f,0,sizeof(f));
14     memset(ff,0,sizeof(ff));
15     f[1][1]=1;
16     f[2][1]=1;
17     f[2][2]=2;
18     int sum =0;
19     for(int i=3;i<1001;i++)
20         for(int j=1;j<=i;j++)
21             f[i][j]=(f[i-1][j-1]*j+f[i-1][j]*j)%10056;
22     for(int i=1;i<1001;i++)
23     {
24         sum=0;
25         for(int j=1;j<=i;j++)
26             sum=(sum+f[i][j])%10056;
27         ff[i]=sum;    
28     }
29     
30     while(T--)
31     {
32         cout<<"Case "<<s++<<": ";
33         int x;
34 
35         cin>>x;
36 
37         cout<<ff[x]<<endl;
38     }
39     
40     return 0;
41 }

 

posted @ 2017-07-27 15:44  西北会法语  阅读(164)  评论(0编辑  收藏  举报