母函数初级运用 hdu 1028 、hdu 1398、hdu 1085

母函数即生成函数,有普通型生成函数和指数型生成函数两种,常用于组合数学中求某个问题的方法数。

例:G(x) = a0 + a1x + a2x*2 + a3x^3 +....+ anx^n

其中ai表示i的组合数。

 

例题1:hdu 1028 Ignatius and the Princess III

题目描述:输入正整数n(n<=120),输出由正整数组成的和为n的组合数。

//输入正整数n(n<=120),输出由正整数组成的和为n的组合数。
//G(x)=(x+x^2+x^3+...)*(x^2+x^4+...)*(x^3+x^6+...)*...
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=125;
int c1[maxn], c2[maxn];
int solve(int cnt){
    int i, j, k;
    for(i=0; i<=cnt; i++){
        c1[i]=1;
        c2[i]=0;
    }
    for(i=2; i<=cnt; i++){              //第i项
        for(j=0; j<=cnt; j++)           //枚举每一位
            for(k=0; k+j<=cnt; k+=i)    //枚举第i项的每一位
                c2[j+k]+=c1[j];         
        for(j=0; j<=cnt; j++){          
            c1[j]=c2[j];
            c2[j]=0;
        }
    }
    return c1[cnt];
}
int main(){
    //freopen("1.txt", "r", stdin);
    int n;
    solve(120);
    while(scanf("%d", &n)!=EOF){
        printf("%d\n", c1[n]);
    }
    return 0;
}


例题2:hdu 1398  Square Coins

 

//输入正整数n(n<=300),输出由完全平方数组成的和为n的组合数。
//G(x)=(x+x^2+x^3+...)*(x^4+x^8+...)*(x^9+x^18+...)*...
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=305;
int c1[maxn], c2[maxn];
int solve(int cnt){
    int i, j, k;
    for(i=0; i<=cnt; i++){
        c1[i]=1;
        c2[i]=0;
    }
    for(i=2; i*i<=cnt; i++){
        for(j=0; j<=cnt; j++)
            for(k=0; k+j<=cnt; k+=i*i)      
                c2[j+k]+=c1[j];
        for(j=0; j<=cnt; j++){
            c1[j]=c2[j];
            c2[j]=0;
        }
    }
    return c1[cnt];
}
int main(){
    //freopen("1.txt", "r", stdin);
    int n;
    while(scanf("%d", &n)&&n){
        printf("%d\n", solve(n));
    }
    return 0;
}

hdu1085 Holding Bin-Laden Captive!

//输入面值为1,2,5的硬币的数量,求第一个不能用他们组成的数。
//G(x)=(x+x^2+x^3+...)*(x^2+x^4+...)*(x^5+x^10+...)   (每组的长度有限,分别对应硬币数量)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=1000000;
int c1[maxn], c2[maxn], a[3], v[3]={1, 2, 5};
int solve(int cnt){
    int i, j, k, t;
    memset(c1, 0, sizeof(c1));
    memset(c2, 0, sizeof(c2));
    for(i=0; i<=cnt&&i<=a[0]; i++)
        c1[i]=1;
    for(i=1; i<=2; i++){
        for(j=0; j<=cnt; j++)
            for(k=0, t=0; k+j<=cnt&&t<=a[i]; k+=v[i], t++)
                c2[j+k]+=c1[j];
        for(j=0; j<=cnt; j++){
            c1[j]=c2[j];
            c2[j]=0;
        }
    }
    return c1[cnt];
}
int main(){
    //freopen("1.txt", "r", stdin);
    int n;
    while(scanf("%d%d%d", &a[0], &a[1], &a[2])&&(a[0]+a[1]+a[2])){
        n=a[0]+a[1]*2+a[2]*5;
        solve(n+1);
        n=1;
        while(n){
            if(!c1[n]){
                printf("%d\n", n);
                break;
            }
            n++;
        }
    }
    return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值