Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16735 Accepted Submission(s): 11776
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
4 10 20
5 42 627
首先我们知道,1的划分方法只有一种:1.
2的划分方法有两种:2,1 1。
3的划分方法有3种:3 ,2 1,1 1,
4的划分方法有5种:4, 3 1,2 2,2 1 1 ,1 1 1 1 。
依次类推:
我们这里给出6的划分方法拿其举例:
6: 6; 5+1; 4+2; 4+1+1; 3+3; 3+2+1; 3+1+1+1; 2+2+2; 2+2+1+1; 2+1+1+1+1; 1+1+1+1+1+1.
首先我们知道,我们有六种拆分方法。
拆1的时候只能拆出一种方案:1+1+1+1+1+1.
当拆2的时候能够拆出三种方案:2+2+2,2+2+1+1,2+1+1+1+1.同时也相当于2+4,并且把4拆分成2,并且再全部拆分成1,2+2,2+1+1,1+1+1+1.因为这里4>2,所以不能保留2+4的情况,否则之后要有重复的问题。
当拆成3的时候能够拆出3种方案:3+3,3+2+1,3+1+1+1.同时也相当于3+3,并且把3拆分成2,并且全部拆分成1:3,2+1,1+1+1.
当拆成4的时候能够拆出两种方案:4+2,4+1+1,同时也相当于4+2,并且把2拆分:2,1+1;
当拆成5的时候能够拆出一种方案:5+1.
还有一种自己:6.
如果真的去推的话,是能够推出这样的动态方程的:dp【j】=dp【j】+dp【j-i】
AC代码如下:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int dp[150];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
dp[j]+=dp[j-i];
}
}
printf("%d\n",dp[n]);
}
}