1326 - Race
PDF (English) | Statistics | Forum |
Time Limit: 1 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
3 1 2 3 |
Case 1: 1 Case 2: 3 Case 3: 13 |
题意:问n匹马赛跑,一共有多少结局。
两匹马有三种结局
1. Both first
2. horse1 first and horse2 second
3. horse2 first and horse1 second
做法:
dp[i][j] i代表有几匹马,j代表有多少团。
把一样快的马放在一个团里。 按团算 ,两匹马 有 两种, 平局 一个团,dp[2][1]计数1,不平局,有两个团,dp[2][2]计数2。 三匹马的时候,dp[3][1]=dp[2][1]*1; dp[3][2]=dp[2][1]*2+dp[2][2]*2; dp[3][3]=dp[2][2]*3
然后dp就很好推了。
要预处理不然会超时。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> int dp[1010][1010]; int aa[1010]; int main() { int t,n; int mod=10056; scanf("%d",&t); int cas=1; int ans; n=1000; memset(dp,0,sizeof dp); dp[1][1]=1; for(int i=2;i<=n;i++)//当前第几只 { for(int j=1;j<=i;j++)//几团 { dp[i][j]+=(dp[i-1][j]*j+dp[i-1][j-1]*j)%mod; } } for(int i=1;i<=n;i++) { ans=0; for(int j=1;j<=i;j++) { ans=(ans+dp[i][j])%mod;; } aa[i]=ans; } while(t--) { scanf("%d",&n); printf("Case %d: %d\n",cas++,aa[n]); } return 0; }