原题链接:http://codevs.cn/problem/2549/
自然数和分解
题目描述 Description
把自然数N分解为若干个自然数之和,输出方案数。
输入描述 Input Description
N,(1≤n≤50)
输出描述 Output Description
方案数
样例输入 Sample Input
5
样例输出 Sample Output
7
数据范围及提示 Data Size & Hint
5 可分为
1 1 1 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3
5
题解
显然这是一道完全背包
2020.09.07更新:回坑的我看的想给自己一个大耳巴子,欲知详细原理请移步A+B Problem。
代码
#include<bits/stdc++.h>
using namespace std;
int n,dp[55];
void in()
{scanf("%d",&n);}
void ac()
{
dp[0]=1;
for(int i=1;i<=n;++i)
for(int j=0;j<=n;++j)
if(dp[j]&&j+i<=n)dp[i+j]+=dp[j];
printf("%d",dp[n]);
}
int main()
{
in();ac();
return 0;
}