hdu 1028 整数的划分问题

"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

思路

假设partition(n,m):正整数n的划分中加数小于等于m的所有划分数。

一般情况下,有:

partion(n, m) = partition(n, m-1) + partition(n-m, m);

例如:

比如partition(7,4) = partition(7,3)+partition(3,4),为什么会加上partition(3,4)呢?

4+3, 4+2+1,4+1+1+1这一行的总个数就是partition(3,4),

相当于把最前面固定的4去掉,剩余项的和等于7-4=3的总个数,但同时剩余项的加数要小于4,因为这些数排在删掉的4的后面。

特殊情况(递归终止条件):

(1)partition(n,m) = partition(n,n-1) + 1

n<=m时,有 partition(n,m) = partition(n,n-1) + 1,因为n的划分不能有大于n的加数

(2)partition(1,n) = 1

n = 1时,不管m有多大,整数1都只有1个划分

(3)partition(n,1) = 1

对任意整数n,加数小于等于1的划分只有1个,即1+1+....+1

使用记忆化搜索优化递归次数,得到如下代码:

 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 }

原文地址:https://www.cnblogs.com/FengZeng666/p/12663187.html

时间: 2024-11-13 09:35:12

hdu 1028 整数的划分问题的相关文章

HDU 1028 整数划分问题 DP

http://acm.hdu.edu.cn/showproblem.php?pid=1028 将一个正整数n划分成多个正整数的和,例如4可以划分为4,1 + 3,2 + 2,1 + 1 + 2, 1 + 1 + 1(1 + 3和3 + 1算作一种划分). 在网上找到了两种做法 方法1: dp[i][j] 的含义是将一个正整数i划分为j个整数的和有几种分法.转移方程很好写但是不太好想. 将此划分分为两种情况,划分中包括1和不包括1.若划分中包括1,则只需将剩余的整数i - 1划分成j - 1个整数

hdu,1028,整数拆分的理解

#include"iostream"using namespace std;int main() { int n,i,j,k; int c[122],temp[122]; //c[] 数组用于储存当前多项式各项系数 //temp[]数组用于暂时储存在运算时的两多项式相加的系数和 while(cin>>n&&n!=0) { for(i=0;i<122;i++) //系数初始化,当前c[]所指的多项式是第一个多项式 {c[i]=1; temp[i]=0;}

hdu 1028 整数划分 (母函数)

假如输入44 = 4;4 = 3 + 1;4 = 2 + 2;4 = 2 + 1 + 1;4 = 1 + 1 + 1 + 1;一共5种 假如输入3 用母函数的方法就是写成(1+X+X2+X3)(1+X2)(1+X3) 展开后 求X3的系数 假如输入n就是(1+X+X2+X3+X4....)(1+X2+X4+X6..)(1+X3+X6...)(....) Sample Input41020 Sample Output542627 1 # include <iostream> 2 # includ

hdu 1028 Ignatius and the Princess III(整数划分)

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12731    Accepted Submission(s): 8999 Problem Description "Well, it seems the first problem is too easy. I will let

hdu 1028 Ignatius and the Princess III 【整数划分】

Ignatius and the Princess III                                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15730    Accepted Submission(

hdu 1028 Ignatius and the Princess III —— 整数划分(生成函数)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分,每个数可以用无限次: 所以构造 f(x) = (1+x+x2+x3+...)(1+x2+x4+...)(1+x3+x6+...)...(1+xn) 乘起来后的 xn 的系数就是方案数: 用两个数组做即可,从第一个括号开始一个一个乘,f[i] 表示此时 xi 项的系数,后面每乘过来一个括号,相当于多了一种转移,所以加上. 代码如下: #include<iostream> #include

hdu 1028 Ignatius and the Princess III(母函数,完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分问题. 第一道母函数...母函数入门 小于等于n的整数共有n个,1,2......n,每个数都有无限多个,对于整数1,它所对应的母函数为(1+x+x^2+...+x^k+...),整数2对应的母函数为(1+x^2+X^4+...+x^(2*k)+...),整数3对应的母函数为(1+x^3+x^6+...+x^(3*k)+...),以此类推,直到整数n. 那么n的整数划分的个数就是这n个母函数乘积

hdu 1028 Ignatius and the Princess III 简单dp

题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是dp[i][i].那么dp[i][j]=dp[i][j-1]+dp[i-j][i-j],dp[i][j-1]是累加1到j-1的结果,dp[i-j][i-j]表示的就是最大为j,然后i-j有多少种表达方式啦.因为i-j可能大于j,这与我们定义的j为最大值矛盾,所以要去掉大于j的那些值 /*******

ACM: HDU 1028 Ignatius and the Princess III-DP

HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description "Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says. &qu