母函數 學習筆記

組合數學——生成函數

參考:

1. http://www.wutianqi.com/blog/596.html

2. https://www.cnblogs.com/linyujun/p/5207730.html 

個人理解:

用多項式乘法表示某種組合的可能的次數。

 

 

模版1:

#include 
using namespace std;
// Author: Tanky Woo
// www.wutianqi.com
const int _max = 10001; 
// c1是保存各項質量砝碼可以組合的數目
// c2是中間量,保存沒一次的情況
int c1[_max], c2[_max];   
int main()
{   //int n,i,j,k;
    int nNum;   // 
    int i, j, k;

    while(cin >> nNum)
    {
        for(i=0; i<=nNum; ++i)   // ---- ①
        {
            c1[i] = 1;
            c2[i] = 0;
        }
        for(i=2; i<=nNum; ++i)   // ----- ②
        {

            for(j=0; j<=nNum; ++j)   // ----- ③
                for(k=0; k+j<=nNum; k+=i)  // ---- ④
                {
                    c2[j+k] += c1[j];
                }
            for(j=0; j<=nNum; ++j)     // ---- ⑤
            {
                c1[j] = c2[j];
                c2[j] = 0;
            }
        }
        cout << c1[nNum] << endl;
    }
    return 0;
}

/*
> > ① 、首先對c1初始化,由第一個表達式(1+x+x^2+..x^n)初始化,把質量從0到n的所有砝碼都初始化爲1. > > > > ② 、 i從2到n遍歷,這裏i就是指第i個表達式,上面給出的第二種母函數關係式裏,每一個括號括起來的就是一個表達式。 > > > > ③、j 從0到n遍歷,這裏j就是(前面i個表達式累乘的表達式)裏第j個變量,(這裏感謝一下seagg朋友給我指出的錯誤,大家可以看下留言處的討論)。如(1+x)(1+x^2)(1+x^3),j先指示的是1和x的係數,i=2執行完之後變爲 (1+x+x^2+x^3)(1+x^3),這時候j應該指示的是合併後的第一個括號的四個變量的係數。 > > > > ④ 、 k表示的是第j個指數,所以k每次增i(因爲第i個表達式的增量是i)。 > > > > ⑤ 、把c2的值賦給c1,而把c2初始化爲0,因爲c2每次是從一個表達式中開始的。 > >
*/

模版2:

#include<cstdio>
typedef long long LL;
const int N = 100 + 5;//假如題目只問到100爲止 
const int MAX = 3;//題目只有1,2,3這3種郵票 
LL c1[N], c2[N];//c2是臨時合併的多項式,c1是最終合併的多項式 
int n;
void init(){
    c1[0] = 1;//一開始0的情況算一種 
    for(int i = 1; i <= MAX; i ++){//把1分到MAXN的郵票合併,變成一個多項式 
        for(int j = 0; j < N; j += i){//i分的郵票,步長是i
            for(int k = 0; j + k < N; k ++){//從x^0到x^N遍歷一遍 
                c2[j + k] += c1[k];//因爲j的所有項係數爲1,所以c1[k]可以看成c1[k]*1; 
            }
        } 
        for(int j = 0; j < N; j ++){//把c2的數據抄到c1,清空c2 
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
} 
int main(){
    init();
    while(scanf("%d", &n) != EOF){
        printf("%I64d\n", c1[n]);
    }
}

 

 

題目1:

HDU1028 Ignatius and the Princess III

http://acm.hdu.edu.cn/showproblem.php?pid=1028

#include <iostream>
#define maxn 125
using namespace std;
typedef long long ll;
ll c1[maxn], c2[maxn];
void init() {
    for(int i = 0; i <= maxn; i++) {
        c1[i] = 1;
        c2[i] = 0;
    }
    for(int i = 2; i <= maxn; i++) {
        for(int j = 0; j <= maxn; j++) {
            for(int k = 0; k+j < maxn; k+=i) {
                c2[j + k] += c1[j];
            }
        }
        for(int j = 0; j <= maxn; j++) {
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
}
int main() {
    int N;
    while(cin >> N) {
        init();
        cout << c1[N] << endl;
    }
    
    return 0;
}

 

題目2:

HDU 1398 Square Coins

http://acm.hdu.edu.cn/showproblem.php?pid=1398

AC代碼:

#include <iostream>
#define maxn 300 + 50
using namespace std;
typedef long long ll;
ll c1[maxn], c2[maxn];
void init() {
    c1[0] = 1;
    for(int i = 1; i <= 17; i++) {
        for(int j = 0; j < maxn; j += i * i) {
            for(int k = 0; j + k < maxn; k++) {
                c2[j + k] += c1[k];
            }
        }
        for(int j = 0; j < maxn; j++) {
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
}
int main() {
    int N;
    init();
    while(cin >> N) {
        if(N == 0) return 0;
        cout << c1[N] << endl;
    }

    return 0;
}

 

題目3:

HDU 1085 Holding Bin-Laden Captive!

http://acm.hdu.edu.cn/showproblem.php?pid=1085

 

AC代碼:

#include <iostream>
#include <cstring>
#define maxn 8000 + 50
using namespace std;
typedef long long ll;
ll c1[maxn], c2[maxn];
int cost[3] = {1, 2, 5};
int num[3];
void init(int a, int b, int c) {
    memset(c1, 0, sizeof c1);
    memset(c2, 0, sizeof c2);
    num[0] = a, num[1] = b, num[2] = c;
    int max = a + 2 * b + 5 * c;
    c1[0] = 1;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j <= num[i] * cost[i]; j += cost[i]) {
            for(int k = 0; j + k < maxn; k++) {
                c2[j + k] += c1[k];
            }
        }
        for(int j = 0; j < maxn; j++) {
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
}
int main() {
    int a, b, c;
    while(cin >> a >> b >> c) {
        if(a == 0 && b == 0 && c == 0) return 0;
        else {
            init(a, b, c);
            for (int i = 1; i < maxn; i++) {
                if (!c1[i]) {
                    cout << i << endl;
                    break;
                }
            }
        }
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章