多種解法: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): 22033    Accepted Submission(s): 15385


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
“嗯,好像第一個問題太容易了,我會讓你知道你以後會多麼愚蠢。” feng5166說。

“第二個問題是,給定一個正整數N,我們定義一個這樣的公式:
  N = a [1] + a [2] + a [3] + ... + a [m]; 
  a [i] 0,1 <= m <= N; 
我的問題是給定N可以找到多少個不同的方程。
例如,假設N爲4,我們可以發現:
  4 = 4; 
  4 = 3 + 1; 
  4 = 2 + 2; 
  4 = 2 + 1 + 1; 
  4 = 1 + 1 + 1 + 1; 
所以當N爲4時,結果爲5。注意,“4 = 3 + 1”和“4 = 1 + 3”在這個問題上也是一樣的,現在,你做到了!
1,利用母函數,g(x)=(1+x+x^2+x^3+…)(1+x^2+x^4+…)(1+x^3+x^6+…)….
代碼如下:
#include<iostream>
using namespace  std;

int sol(int n)
{
	int a[130],b[130];
	for(int i=0;i<=n;i++)
	{
        a[i]=1;
        b[i]=0;
    }
	for(int i=2;i<=n;i++)
	{
		for(int j=0;j<=n;j++)
			for(int k=0;k*i+j<=n;k++)
				b[k*i+j] += a[j];
		for(int p=0;p<=n;p++)
			a[p]=b[p],b[p]=0;
	}
	return a[n];
}

int main()
{
	int n;
	while(cin >> n)
	{
		cout << sol(n) <<endl;
	}
	return 0;
}

2,整數劃分問題是將一個正整數n拆成一組數連加並等於n的形式,且這組數中的最大加數不大於n
   
 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
   
 
   
 11種。下面介紹一種通過遞歸方法得到一個正整數的劃分數。
遞歸函數的聲明爲int split(int n, int m);其中n爲要劃分的正整數,m是劃分中的最大加數(m > n時,最大加數爲n)
    1
 n = 1m = 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<iostream>
#include<cstdio>
using namespace std;

int s[130][130];

int split (int n,int m)
{
    if(s[n][m])
        return s[n][m];
    if(n<1||m<1)
        return s[n][m]=0;
    if(n==1||m==0)
        return 1;
    if(n<m)
        return s[n][m]=split(n,n);
    if(n==m)
        return s[n][m]=(split(n,m-1)+1);
    else
        return s[n][m]=(split(n,m-1)+split(n-m,m));
}

int main ()
{
    int n;
    split(120,120);
    while(scanf("%d",&n)!=EOF)
        printf("%d\n",split(n,n));
    return 0;
}


發佈了41 篇原創文章 · 獲贊 36 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章