杭电 OJ 1028 整数划分




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




以下文章便于对整数划分的算法大体有一个了解



-------------------------------------------------------------------------------------------------------------------------------------

整数划分问题是将一个正整数n拆成一组数连加并等于n的形式,且这组数中的最大加数不大于n。
    如6的整数划分为
    最大数  
    6         6
    5        5 + 1
    4         4 + 2, 4 + 1 + 1
    3         3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
    2        2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
    1         1 + 1 + 1 + 1 + 1 + 1
    
    共11种。下面介绍一种通过递归方法得到一个正整数的划分数。
    
    递归函数的声明为 int split(int n, int m);其中n为要划分的正整数,m是划分中的最大加数(当m > n时,最大加数为n),
    1 当n = 1或m = 1时,split的值为1,可根据上例看出,只有一个划分1 或 1 + 1 + 1 + 1 + 1 + 1
    可用程序表示为if(n == 1 || m == 1) return 1;
    
    2 下面看一看m 和 n的关系。它们有三种关系
    (1) m > n
    在整数划分中实际上最大加数不能大于n,因此在这种情况可以等价为split(n, n);
    可用程序表示为if(m > n) return split(n, n);    
    (2) m = n
    这种情况可用递归表示为split(n, m - 1) + 1,从以上例子中可以看出,就是最大加
    数为6和小于6的划分之和
    用程序表示为if(m == n) return (split(n, m - 1) + 1);
    (3) m < n
    这是最一般的情况,在划分的大多数时都是这种情况。
    从上例可以看出,设m = 4,那split(6, 4)的值是最大加数小于4划分数和整数2的划分数的和。
    因此,split(n, m)可表示为split(n, m - 1) + split(n - m, m)
    
    根据以上描述,可得源程序如下:
  

#include 

   int split(int n, int m)
    {
      if(n < 1 || m < 1) return 0;
      if(n == 1 || m == 1) return 1;
      if(n < m) return split(n, n);
      if(n == m) return (split(n, m - 1) + 1);
      if(n > m) return (split(n, m - 1) + split((n - m), m));
   }

int main()
{
      printf("12的划分数: %d", split(12, 12));
    return 0;
}


--------------------------------------------------------------------------


然而递归在本题会超时,不过上述的叙述加深了我对整数划分的理解。


非递归的的代码:


#include <iostream>
using namespace std;

int main()
{
	freopen("D:\\input.txt","r",stdin);




	int n;
	int A[121][121];
	for(int i=0;i<121;i++)
	{
		for(int j=0;j<121;j++)
		{
			A[i][j]=0;
			A[i][1]=1;
		}
	}
	A[0][0]=1;
	A[1][0]=1;
    A[1][1]=1;
    /*A[2][1]=1;
    A[2][2]=2;
    A[3][1]=1;
    A[3][2]=2;
    A[3][3]=3;*/



	for(int i=2;i<121;i++)
	{
		for(int j=1;j<=i;j++)
		{
			if(i-j<=j)
			{
				A[i][j]=A[i][j-1]+A[i-j][i-j];
			}
			else
				A[i][j]=A[i][j-1]+A[i-j][j];
		}
	}
	while(cin>>n)
		cout<<A[n][n]<<endl;
}


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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值