当前位置:网站首页>[NOIP2001 提高组] 数的划分
[NOIP2001 提高组] 数的划分
2022-08-07 06:56:00 【WDLieqi】
题目描述
将整数 n n n 分成 k k k 份,且每份不能为空,任意两个方案不相同(不考虑顺序)。
例如: n = 7 n=7 n=7, k = 3 k=3 k=3,下面三种分法被认为是相同的。
1 , 1 , 5 1,1,5 1,1,5;
1 , 5 , 1 1,5,1 1,5,1;
5 , 1 , 1 5,1,1 5,1,1.
问有多少种不同的分法。
输入格式
n , k n,k n,k ( 6 < n ≤ 200 6<n \le 200 6<n≤200, 2 ≤ k ≤ 6 2 \le k \le 6 2≤k≤6)
输出格式
1 1 1 个整数,即不同的分法。
样例 #1
样例输入 #1
7 3
样例输出 #1
4
提示
四种分法为:
1 , 1 , 5 1,1,5 1,1,5;
1 , 2 , 4 1,2,4 1,2,4;
1 , 3 , 3 1,3,3 1,3,3;
2 , 2 , 3 2,2,3 2,2,3.
一、dp解法
d p [ i ] [ j ] dp[i][j] dp[i][j] 代表把数字 i 划分成 j 个数。
当划分的数里面有 1 的时候 方案数为 d p [ i − 1 ] [ j − 1 ] dp[i - 1][j - 1] dp[i−1][j−1]
不含 1 的时候 d p [ i − x ] [ x ] dp[i - x][x] dp[i−x][x], 针对这个方案我们可以先往每一个划分的区域里面先放一个1,然后再进行划分。
最后可以得到动态转换方程:
那么代码如下
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
int dp[220][220];
int main()
{
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i ++ ) dp[i][1] = dp[i][0] = 1;
for (int i = 2; i <= k; i ++ ) dp[1][i] = dp[0][i] = 0;
for (int i = 2; i <= n; i ++ )
for (int j = 2; j <= k; j ++ )
if (i > j) dp[i][j] = dp[i - 1][j - 1] + dp[i - j][j];
else dp[i][j] = dp[i - 1][j - 1];
cout << dp[n][k] << '\n';
return 0;
}
边栏推荐
- 详解深拷贝,浅拷贝
- VoLTE basic self-study series | IP layer routing and addressing process in IMS network (implementation in registration process)
- 【C语言】内存函数
- ansible当中模块的使用
- LeetCode 剑指 Offer 06. 从尾到头打印链表
- MySQL - 索引优化
- VoLTE Basic Self-Learning Series | What is Silent Redial in VoLTE?How does it relate to CSFB?
- 程序员福音,关于如何使用Markdown写出一份漂亮的简历 —— 程序员简历 | md文档简历制作教程
- Are there MCUs with 5nm process technology?
- VoLTE基础自学系列 | 什么是VoLTE中的Silent Redial?它和CSFB什么关系?
猜你喜欢
随机推荐
VoLTE Basic Self-Learning Series | What is Silent Redial in VoLTE?How does it relate to CSFB?
Graphical LeetCode - 1408. String Matching in Arrays (Difficulty: Easy)
[Acwing Weekly Replay] The 63rd weekly match 20220806
LeetCode 1163. The last substring lexicographically
动手学深度学习--计算性能篇
OC-run loop
0-1背包问题
A Pursuit of Temporal Accuracy in General Activity Detection TAG论文阅读笔记
servlet tutorial 2: return to the jsp page
OC-run loop
Xcode13.1 real machine debugging
传输层(UDP协议,TCP协议三次握手、四次挥手)
inux安装软件命令yum,apt-get
netstat & firewall
Detailed explanation of fixture test fixture of pytest framework
大一大二拾光日记
grid网格布局
Actionformer: Localizing moments of actions with transformers 论文阅读笔记
LeetCode刷题笔记:899.有序队列
Vitalik详解5种类型的ZK-EVM








