贝尔(Bell)数

本文介绍Bell数及第二类Stirling数的概念,并通过C++代码实现Bell数的计算。Bell数代表了集合的划分数目,而每个Bell数又是第二类Stirling数之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bell数的定义:第n个Bell数表示集合{1,2,3,...,n}的划分方案数,即:B[0] = 1;
Bell(0) = 1,
Bell(n+1) = C(n,0)*Bell(0)+C(n,1)*Bell(1)+...+C(n,n)*Bell(n)
每一个Bell数都是第二类Stirling数的和,即:
Bell(n) = str2(n,1)+str2(n,2)+...+str2(n,n)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>
#include <set>

using namespace std;

const int maxn = 100;

int str2[maxn][maxn],be[maxn];

void stirling2(int n)
{
    for(int i = 1; i <= n; i++)
    {
        str2[i][i] = 1 ;
        for(int j = 1; j < i; j++)
        {
            str2[i][j]=str2[i-1][j-1]+j*str2[i-1][j];
        }
    }
}

void bell(int n)
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= i; j++)
        {
            be[i]=be[i]+str2[i][j] ;
        }
   }
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        memset(str2,0,sizeof(str2));
        stirling2(n);
        memset(be,0,sizeof(be));
        bell(n);
        printf("%d\n",be[n]);
    }
    return 0;
}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值