Stirling numbers and Bell numbers

Bell Number:

Note B n is the number of division method set comprises n elements.

For example: B 0 =. 1, B . 1 =. 1, B 2 = 2, B . 3 =. 5, B . 4 = 15, B . 5 = 52 is.

Bell numbers recursion formulas: B 0 =. 1, B n-+. 1 = [Sigma (K = 0-> = n-K) C (n-, K) B * K.

Stirling number divided into two in the number of combinations.

Stirling Number of the first type:

Containing positive and negative values, the absolute value of n elements is divided into a set of k number of methods ring arrangement.

Recursion formulas: S (n, 0) = 0, S (1,1) = 1, S (n, k) = S (n-1, k-1) + S (n-1, k) * ( n-1).

prove:

1. The set {an} is a set of k sub-subset, then the {a1, a2, a3 ...... an-1} into the k-1 has a set S (n-1, k-1 ) a number of divisions.

2, if k is not a {an} of subsets, and then the other must be an element constituting a subset of the first {a1, a2, a3 ...... an-1} is divided into k subsets, a total of S (n-1, k) dividing the number of species, then such an insert with a number of divisions in the (n-1) kinds of programs, it is the (n-1) * S (n-1, k).

Application of 1,2 addition theorem: to give S (n, k) = S (n-1, k-1) + S (n-1, k) * (n-1).

Stirling number of second kind:

Containing the set of n elements is divided into k non-empty sets the number of programs.

Recurrence formula:

S(n,n)=S(n,1)=1. S(n,k)=S(n-1,k-1)+k*S(n-1,k)。(n>1,k>=1)

The proof of the first type and the Stirling number of methods.

Bell binding and defining a second number of the class number Stirling, obviously: Bn = Σ (k = 1 -> k = n) S (n, k)

Bell Stirling number of second kind and the number can be solved by constructing a triangle Bell, constructed as follows:

1. The first line is the first 1, a [1,1] = 1.

2. For n> 1, satisfies: a [n, 1] = a [n-1, n-1].

3. For m, n> 1, m n-th row satisfy the item: a [n, m] = a [n, m-1] + a [n-1, m-1].

The results are as follows:

1  

1    2

2    3    5

5    7    10  15

15  20  27  37  52

.................................................

The first number of each line item is Bell, the sum of each row is the second kind Stirling numbers.

~~~~

Bell Number of template question:

UVA 10844

Little Joan has N blocks,all of them of different sizes. He is playing to build cities in the beach. A city
is just a collection of buildings.
A single block over the sand can be considered as a building. Then he can construct higher buildings
by putting a block above any other block. At most one block can be put immediately above any other
block. However he can stack several blocks together to construct a building. However, its not allowed
to put bigger blocks on top of smaller ones, since the stack of blocks may fall. A block can be speci ed
by a natural number that represents its size.
It doesn't matter the order among buildings. That is:
1 3
2 4
is the same con guration as:
3 1
4 2
Your problem is to compute the number of possible different cities using N blocks. We say that
#(N) gives the number of different cities of size N. If N = 2, for instance, there are only two possible
cities:
City #1:
1 2
In this city both blocks of size 1 and 2 are put over the sand.
City #2:
1
2
In this city block of size 1 is over block is size 2, and block of size 2 is over the sand.
So, #(2) = 2.
Input
A sequence of non-negative integer numbers, each of one in different line. All of them but the last one
are natural numbers. The last one is 0 and means the end. Each natural number is less than 900.
Output
For each natural number I in the input, you must write a line with the pair of numbers I, #(I).
Sample Input
2
3
0
Sample Output
2, 2
3, 5

analysis:

With N building block to cover the buildings can be understood as a size into a plurality of disjoint subsets of the set of N. This is consistent with definition of the Bell, then this problem can be pretreated Bell triangle to O (1) outputs the answer.

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int64;
int64 m=1e10;
struct Bigint{
    int l;
    int64 s[200];
    void read(int64 x)
    {
        l=-1;
        memset(s,0,sizeof(s));
        do{
            s[++l]=x%m;
            x/=m;
        }while(x);
    }
    void print()
    {
        printf("%llu",s[l]);
        for(int i=l-1;i>=0;i--)
        {
            printf("%010llu",s[i]);
        }
    }
}dp[2][1000],ans[1000];
Bigint operator+(Bigint a,Bigint b)
{
    a.l=max(a.l,b.l);int64 d=0;
    for(int i=0;i<=a.l;i++)
    {
        a.s[i]+=d+b.s[i];
        d=a.s[i]/m;
        a.s[i]%=m;
    }
    if(d)    a.s[++a.l]=d;
    return a;
}
int n;
void getans(int id,int n)
{
    int i=id^1;
    for(int j=1;j<=n;j++)
    {
        dp[id][j+1]=dp[i][j]+dp[id][j];
    }
}
void work()
{
    dp[1][1].read(1);
    ans[2]=dp[0][1]=ans[1]=dp[1][1];
    for(int i=2;i<=900;i++)
    {
        getans(i&1,i);
        dp[(i&1)^1][1]=ans[i+1]=dp[i&1][i];
    }
}
int main()
{
    //freopen("input.txt","r",stdin);
    work();
    while(~scanf("%d",&n)&&n)
    {
        printf("%d, ",n);
        ans[n+1].print();
        printf("\n");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/cautx/p/11413680.html