1028 demarcation integer hdu

"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

10 
20

Sample Output

42 
627
 
 

Thinking

Suppose partition (n, m): n is a positive integer division in all addend division number less than or equal to m.
Under normal circumstances, there are:
partion(n, m) = partition(n, m-1) + partition(n-m, m);
 
E.g:
For example partition (7,4) = partition (7,3) + partition (3,4), why would add partition (3,4) it?
4 + 3, + 4 + 4 + 2 + 1 + 1 total number of this line is 1 partition (3,4),
4 is equivalent to a fixed top removed, the remaining entries will equal the total number of 7-4 = 3, but the remaining items addend less than 4, such as the number of deleted rows 4 at the back.
 
Special circumstances (recursive termination condition):
(1)partition(n,m) = partition(n,n-1) + 1
         When n <= m, with a partition (n, m) = partition (n, n-1) + 1, n is not divided as there is greater than n addend
(2)partition(1,n) = 1
         When n = 1, no matter how big the m, have only an integer division 1
(3)partition(n,1) = 1
         For any integer n, less than or equal addend 1 only one division, i.e. 1 + 1 + 1 + ....
 
Memory search optimization using recursion, to give the following code:
 1 #include <iostream>
 2 #include <vector>
 3 #include <stdio.h>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 #define MAXN 150
 9 
10 vector<vector<int>> memo;
11 
12 int partition(int n, int m)
13 {
14     if(n < 1 || m < 1)
15         return 0;
16     
17     if(n == 1 || m == 1)
18         return 1;
19     
20     if(memo[n][m] != -1)
21         return memo[n][m];
22         
23     if(n <= m)
24         memo[n][m] = 1+partition(n,n-1);
25     else
26         memo[n][m] = partition(n,m-1) + partition(n-m,m);
27     
28     return memo[n][m];
29     
30 } 
31 
32 int main()
33 {
34     int n;
35     while(scanf("%d", &n) != EOF)
36     {
37         memo = vector<vector<int>>(MAXN, vector<int>(MAXN, -1));
38         printf("%d\n", partition(n,n));
39     }
40     
41     return 0;
42 }

 


 
 

Guess you like

Origin www.cnblogs.com/FengZeng666/p/12663187.html