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): 20918    Accepted Submission(s): 14599


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

母函数:生成函数即母函数,是组合数学中尤其是计数方面的一个重要理论和工具。生成函数有普通型生成函数和指数型生成函数两种,其中普通型用的比较多。形式上说,普通型生成函数用于解决多重集的组合问题,而指数型母函数用于解决多重集的排列问题。母函数还可以解决递归数列的通项问题(例如使用母函数解决斐波那契数列的通项公式)。

(百度百科的东西,大家估计也不想看,请看下面这个公式)

以展开后的x4为例,其系数为4,即4拆分成1、2、3之和的拆分数为4;

即 :4=1+1+1+1=1+1+2=1+3=2+2

再引出两个概念整数拆分和拆分数:

整数拆分即把整数分解成若干整数的和(相当于把n个无区别的球放到n个无标志的盒子,盒子允许空,也允许放多于一个球)。

整数拆分成若干整数的和,办法不一,不同拆分法的总数叫做拆分数

 1 /*
 2     Name: hdu--1028--Ignatius and the Princess III 
 3     Copyright: 2017 日天大帝
 4     Author: 日天大帝
 5     Date: 22/04/17 16:36
 6     Description: 母函数,对应上面那个公式,更容易做这道题
 7 */
 8 #include<iostream>
 9 using namespace std;
10 int c1[121],c2[121];
11 int main()
12 {
13     int n = 121,i;
14     for(i = 0;i <= n; i++){ 
15         c1[i] = 1;//母函数第一个因子,全为1,c1保存前面i-1个因子相乘的结果,首先对c1初始化,由第一个表达式(1+x+x2+..xn)初始化,把质量从0到n的所有砝码都初始化为1.
16         c2[i] = 0;
17     }
18     for(i =2;i<=n;i++){//操作第i个括号,i从2到n遍历,这里i就是指第i个表达式,上面给出的母函数关系式里,每一个括号括起来的就是一个表达式。
19         for(int j = 0; j<= n;j++){//对于指数为j的进行操作,j 从0到n遍历,这里j就是只一个表达式里第j个变量,比如在第二个表达式里:(1+x2+x4....)里,第j个就是x2*j. 
20             for(int k =0 ;k+j<=n;k+=i){//把第i个的每一个数与之前的结果相乘,k表示的是第j个指数,所以k每次增i(因为第i个表达式的增量是i)。 
21                 c2[j+k]+=c1[j];//j+k指数相加,他的值就是这个指数的系数, 
22             }
23         }
24         for(int j = 0;j<=n;j++){//系数保存在前面一个数组中
25             c1[j] = c2[j];//把c2的值赋给c1,而把c2初始化为0,因为c2每次是从一个表达式中开始的 
26             c2[j] = 0;
27         }
28     }
29     while(cin>>n)cout<<c1[n]<<endl;
30     return 0;
31 }
原文地址:https://www.cnblogs.com/slothrbk/p/6748364.html