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
Sample Output
5 42 627【題目分析】
剛開始看感覺像是一個遞推,可是推半天沒找到什么明顯的關系。知道是要用到組合數學里面母函數的知識后就去學習了一下母函數,還是挺好理解的,關鍵在于怎么進行靈活的運用。
我們在進行組合的時候多進行的是加法,而冪函數乘積就是系數之和的性質可以幫助我們進行組合的分析,而且這種組合是他們將相同系數合并后的,可以直接得到我們的結果。
關于母函數的學習,可以看看其他大佬的文章,總結起來就兩句話
在這個問題里面,我們想要選出能湊出這個數字的和的所有的數字,為了防止重復,我們從小往大選,選出來到最后的結果就是答案
其實我們進行的是模擬冪級數的乘法運算。
這里為了優化復雜度,我用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; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的HDU - 1028——母函数入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU - 1796——容斥原理+二进制
- 下一篇: 腾讯视频背景设置方法