Lightoj 1326-race (second class Stirling number AH)

Source: Internet
Author: User

Title Link: http://lightoj.com/volume_showproblem.php?problem=1326


Baidu Encyclopedia: Sterling Number


Acdreamer: The first class of Stirling and the second class Stirling


Disky and Sooma, and both 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 the this. But everything have an end. A Holy person, Munsiji came to 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 the is their romance!

In a race there is 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 is 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:

Second Class Stirling S (P,k)

the recursive formula for S (P,K) is: S (p,k) =k*s (p-1,k) +s (p-1,k-1), 1<= k<=p-1

Boundary conditions: S (p,p) =1, p>=0 S (p,0) =0, P>=1


The problem is to find the second class of Stirling numbers and multiply the factorial!

The code is as follows:

#include <cstdio> #include <cstring> #define MOD 10056int sti[1017][1017];int f[1017];void init () {    sti[ 1][1]=1;    for (int i = 2; I <=; i++)   //Initialize the second class of Stirling number    {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 <=, i++)//factorial    {        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 (second class Stirling number AH)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.