每日一题之 hiho221 Push Button II (计数dp)

描述
There are N buttons on the console. Each button needs to be pushed exactly once. Each time you may push several buttons simultaneously.

Assume there are 4 buttons. You can first push button 1 and button 3 at one time, then push button 2 and button 4 at one time. It can be represented as a string “13-24”. Other pushing way may be “1-2-4-3”, “23-14” or “1234”. Note that “23-41” is the same as “23-14”.

Given the number N your task is to find the number of different valid pushing ways.

输入
An integer N. (1 <= N <= 1000)

输出
Output the number of different pushing ways. The answer would be very large so you only need to output the answer modulo 1000000007.

样例输入
3
样例输出
13

思路:

统计方案数的题目可以用dp来做,不妨我们设dp[i][j]表示 数字 1~i 分成 j 组一共有多少种方案。那么考虑 dp[i+1][j+1]等于多少? 在之前由于递推求得了 dp[i][j] 了,那么

  1. 一种情况是1~i 的数分成 j 组,然后第 i+1个数 独立成为一组 ,相当于在分好的 j 组中 插空就行了,j 个组 一共 j+1 个空,所以这种情况的方案数是dp[i][j]∗(j+1)dp[i][j]*(j+1)dp[i][j](j+1)
  2. 另外一种情况是 1~i的数已经被分成了 j+1 个组,那么第 i+1 这个数,随便放到这 j+1中的任意一个组就行了,那么这种情况的方案数是 d[i][j+1]∗(j+1)d[i][j+1]*(j+1)d[i][j+1](j+1)

综上就可以得出递推式:dp[i][j]=dp[i−1][j−1]∗j+dp[i−1][j]∗jdp[i][j] = dp[i-1][j-1]*j + dp[i-1][j]*jdp[i][j]=dp[i1][j1]j+dp[i1][j]j

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

const int maxn = 1e3+5;
const int mod = 1e9+7;

typedef long long ll;

ll dp[maxn][maxn];

void solve(int n)
{
	memset(dp,0,sizeof(dp));
	for (int i = 1; i <= n; ++i)
		dp[i][1] = 1;

	for (int i = 2; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			dp[i][j] = (dp[i-1][j-1]*j)%mod + (dp[i-1][j]*j)%mod;
			dp[i][j] %= mod;
		}
	}
	ll res = 0;
	for (int i = 1; i <= n; ++i) {
		res = (res + dp[n][i])%mod;
	}
	cout << res%mod << endl;
}

int main()
{

	int n;
	cin >> n;
	solve(n);

	return 0;
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值