【題目描述】
“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.“把組合問題的加法法則和冪級數的乘冪對應起來”2.“母函數的思想很簡單 — 就是把離散數列和冪級數一 一對應起來,把離散數列間的相互結合關系對應成為冪級數間的運算關系,最后由冪級數形式來確定離散數列的構造. “
在這個問題里面,我們想要選出能湊出這個數字的和的所有的數字,為了防止重復,我們從小往大選,選出來到最后的結果就是答案
其實我們進行的是模擬冪級數的乘法運算。
這里為了優化復雜度,我用i0和i1進行翻滾
【AC代碼】
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;typedef long long ll;
const int MAXN=125;
int n;
int ans[2][MAXN<<2];
int i0,i1;int main()
{while(~scanf("%d",&n)){memset(ans,0,sizeof(ans));i0=0; i1=1;for(int i=0;i<=n;i++){ans[i0][i]=1;}for(int i=2;i<=n;i++){for(int j=0;j<=n;j++){for(int k=0;k+j<=n;k+=i){ans[i1][j+k]+=ans[i0][j];}}swap(i0,i1);for(int i=0;i<=n;i++){ans[i1][i]=0;}}printf("%d\n",ans[i0][n]);}return 0;
}