Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18962 Accepted Submission(s): 13331
Problem Description
“Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says.
“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!”
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
Author
Ignatius.L
Recommend
We have carefully selected several similar problems for you: 1171 1085 1398 2152 1709
思路:第一次不会做,看的前辈们写的才懂了一些,太菜了~多积累就好了~
record[n][m]是把自然数n划分成所有元素不大于m的方法,例如:
当n=4,m=1时,要求所有的元素都比1小,所以划分法只有1种:{1,1,1,1};
当n=4,m=2时…..只有3种{1,1,1,1},{2,1,1},{2,2};
当n=4,m=3时…..只有4种{1,1,1,1},{2,1,1},{2,2},{3,1};
当n=4,m=4时…..只有5种{1,1,1,1},{2,1,1},{2,2},{3,1} , {4};
当n=4,m=5时…..只有5种{1,1,1,1},{2,1,1},{2,2},{3,1} , {4};
我们分析一下:
1.当n=1或者m=1时,这时候只有1种划分,不解释了~
2.当n==m时,reord[n][m]=record[n][m-1]+1,这个1就是划分的 n,例n=4,m=4这种情况实际上是由n=4,m=3 + 4本身得来的。
3.当n< m时,record[n][m]=record[n][n],因为n< m,n又怎么能划分出来一个比它大的数呢?
4.当n > m时,这时候有两种情况:①对m进行划分,即{m, {x1,x2,…xi}}, record[n][m]=record[n-m][m];②如果不对m划分,即record[n][m]=record[n][m-1]。所以,综合来说record[n][m]=record[n-m][m]+record[n][m-1]。
综上:
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
int record[130][130];
//打个表
void Creat_table(){
for(int i=1;i<130;i++)
record[1][i]=record[i][1]=1;
for(int i=2;i<130;i++)
for(int j=1;j<130;j++){
if(i==j)
record[i][j]=record[i][j-1]+1;
else if(i>j)
record[i][j]=record[i][j-1]+record[i-j][j];
else
record[i][j]=record[i][i];
}
}
int main(){
Creat_table();
int n;
while(scanf("%d",&n)!=EOF){
printf("%d\n",record[n][n]);
}
}