日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU - 1028——母函数入门

發(fā)布時間:2023/11/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU - 1028——母函数入门 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【題目描述】
“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

【題目分析】
剛開始看感覺像是一個遞推,可是推半天沒找到什么明顯的關(guān)系。知道是要用到組合數(shù)學(xué)里面母函數(shù)的知識后就去學(xué)習(xí)了一下母函數(shù),還是挺好理解的,關(guān)鍵在于怎么進行靈活的運用。
我們在進行組合的時候多進行的是加法,而冪函數(shù)乘積就是系數(shù)之和的性質(zhì)可以幫助我們進行組合的分析,而且這種組合是他們將相同系數(shù)合并后的,可以直接得到我們的結(jié)果。
關(guān)于母函數(shù)的學(xué)習(xí),可以看看其他大佬的文章,總結(jié)起來就兩句話

1.“把組合問題的加法法則和冪級數(shù)的乘冪對應(yīng)起來”2.“母函數(shù)的思想很簡單 — 就是把離散數(shù)列和冪級數(shù)一 一對應(yīng)起來,把離散數(shù)列間的相互結(jié)合關(guān)系對應(yīng)成為冪級數(shù)間的運算關(guān)系,最后由冪級數(shù)形式來確定離散數(shù)列的構(gòu)造. “

在這個問題里面,我們想要選出能湊出這個數(shù)字的和的所有的數(shù)字,為了防止重復(fù),我們從小往大選,選出來到最后的結(jié)果就是答案

其實我們進行的是模擬冪級數(shù)的乘法運算。

這里為了優(yōu)化復(fù)雜度,我用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; } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的HDU - 1028——母函数入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。