码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ 1326 - Race(第二类斯特林数啊 )

时间:2015-03-29 10:56:49      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://lightoj.com/volume_showproblem.php?problem=1326


百度百科: 斯特林数


ACdreamer:第一类Stirling数和第二类Stirling


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



PS:

第二类Stirling数 S(p,k)

S(p,k)的递推公式是:S(p,k)=k*S(p-1,k)+S(p-1,k-1) ,1<= k<=p-1

边界条件:S(p,p)=1 ,p>=0    S(p,0)=0 ,p>=1


此题是求出第二类斯特林数后再乘以阶乘!

代码如下:

#include <cstdio>
#include <cstring>
#define mod 10056
int sti[1017][1017];
int f[1017];

void init()
{
    sti[1][1]=1;
    for(int i = 2; i <= 1000; i++)   //初始化第二类斯特林数
    {
        for(int j = 1; j <= i; j++)
        {
            sti[i][j] = (j*sti[i-1][j] + sti[i-1][j-1])%mod;
        }
    }
    f[0] = 1;
    for(int i = 1; i <= 1000; i++) //阶乘
    {
        f[i] = f[i-1]*i;
        f[i] %= mod;
    }
}

int main()
{
    init();
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            ans += sti[n][i]*f[i];
            ans %= mod;
        }

        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}


LightOJ 1326 - Race(第二类斯特林数啊 )

标签:

原文地址:http://blog.csdn.net/u012860063/article/details/44724495

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!