您的位置:首页 > 其它

Race - UVa 12034 递推

2014-07-21 06:15 281 查看


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.

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


题意:有n匹马,每次可以乘至少一匹,问有多少种情况。(马与马不同)。

思路:递推,num[i][j]表示i匹马,一共乘j次,num[i][j]=(num[i-1][j]*j+j*num[i-1][j-1])%mod;前一个的意思是i-1匹马乘j次后,第i匹马可以放在任意次去乘,后一个的意思是第i匹马被单独乘,可以有j种单独的方式。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
int num[1100][1100],mod=10056,ans[1100];
int main()
{ int i,j,k,n,t;
  num[1][1]=1;
  for(i=2;i<=1000;i++)
   for(j=1;j<=i;j++)
    num[i][j]=(num[i-1][j]*j+j*num[i-1][j-1])%mod;
  for(i=1;i<=1000;i++)
   for(j=1;j<=i;j++)
    ans[i]=(ans[i]+num[i][j])%mod;
  scanf("%d",&t);
  for(i=1;i<=t;i++)
  { scanf("%d",&n);
    printf("Case %d: %d\n",i,ans
);
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: