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.
- Both first
- horse1 first and horse2 second
- 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
思路:一开始以为是数学题,组合数相关,结果后来发现是个dp,,,而且一点也不难,,就一个公式dp[i][j]=dp[i-1][j-1]*j+dp[i-1][j]*j;
dp[i][j]表示i个数,有j个名次的种数。所以公式是很好理解的,
(PS:因为数据小,LSD直接跑出所有结果,交了个表,也是优秀?)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 10056
#define maxn 10056
int dp[1005][1005],T,n,ans;
int main()
{
dp[0][0]=1;
for(int i=1;i<=1000;i++)
{
for(int j=1;j<i;j++)
{
dp[i][j]=dp[i-1][j-1]*(j)%mod+dp[i-1][j]*j%mod;
dp[i][j]%=mod;
}//
dp[i][i]=dp[i-1][i-1]*i%mod;
}
scanf("%d",&T);
int cnt=0;
while(T--)
{
++cnt;
scanf("%d",&n);
ans=0;
for(int i=1;i<=n;i++)
{
ans+=dp[n][i];
ans%=mod;
}
printf("Case %d: %d\n",cnt,ans);
}
}