2021-06-14

本文探讨了如何利用斯特林数解决将n个不同球放入m个不同盒子的问题,要求每个盒子至少有1个球。通过数学公式和递归算法实现方案的计数与枚举。文章提供了C++程序实现,并展示了如何用容斥原理来理解这个问题。此外,还介绍了斯特林数在解决此类问题中的应用。

斯特林数

参考来源:Deadecho

n个不同的球(1-6)放在m个不同的盒子(A-C)里,要求每个盒子至少有1个球并且所有球都放盒子里。
请问有多少种方案?请输出这些方案?540
按如下格式输出(字典序从小到大,同一个盒子球的次序无所谓,统一按升序输出)
A1A2A3A4B5C6

A6B5C1C2C3C4
要求:
1)分计数和枚举2个问题
2)每个问题,给出手算和程序2种解法
3)给出数学证明。

斯特林算法:

是指将m个不同的球放入k个相同的盒内。要算出上面那题,只需要再乘k!即k ! S ( n , k ) 。

斯特林解法:

容斥原理:
在这里插入图片描述
这道题貌似可以用dp

#include<iostream>
using namespace std;
int n,m;
int a[10],b[10],c[10];
int z[10];
int aa,bb,cc;

void dfs(int u)
{
    if(u == n){
        for(int i=0;i<m;i++){
            for(int j = 1;j<=aa ;j++)cout<<"A"<<a[i];
            for(int j = 1;j<=bb; j++)cout<<"B"<<b[i];
            for(int j = 1;i<=cc; j++)cout<<"C"<<c[i];
            cout<<endl;
        }
        return;
    }
    for(int i = 0; i < m; i++){
        if(条件){
            if(i==0)a.push_back = u;
            if(i==1)b.push_back = u;
            if(i==2)c.push_back = u;
            z[i]=1;
            dfs(u+1);
            if(i==0)a.clear();
            if(i==1)b.clear();
            if(i==3)c.clear();
            z[i]=0;
        }
    }
}

int main()
{
    n = 6, m = 3;   //6个球,3个盒子
    dfs(1);
    return 0;
}

create table test01 ( id varchar2(20) ,start_date date ,end_date date ); insert into test01 values ('1001', to_date('2021-06-14 12:12:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 18:12:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1003', to_date('2021-06-14 13:12:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 16:12:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1004', to_date('2021-06-14 13:15:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 20:12:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1002', to_date('2021-06-14 15:12:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 16:12:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1005', to_date('2021-06-14 15:18:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 20:12:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1001', to_date('2021-06-14 20:12:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 23:12:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1006', to_date('2021-06-14 21:12:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 23:15:12', 'syyyy-mm-dd hh24:mi:ss')); insert into test01 values ('1007', to_date('2021-06-14 22:12:12', 'yyyy-mm-dd hh24:mi:ss'), to_date('2021-06-14 23:10:12', 'syyyy-mm-dd hh24:mi:ss')); select*from test01 with temp AS ( select start_date AS time1, 1 as change from test01 union select end_date, -1 from test01 ), temp1 AS ( select time1, sum(change) over (order by time1) as concurrent_users, lead(time1) over (order by time1) as next_event_time from temp ), temp2 as ( select time1 as period_start, next_event_time as period_end, concurrent_users, rank() over (order by concurrent_users desc) as rank_by_users from temp1 where next_event_time is not null ) select concurrent_users as "用户登录量",to_char(period_start,'HH:MI:SS') ,to_char(period_end,'HH:MI:SS') from temp2 where rank_by_users = 1 order by period_start; 分析这个代码
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值