【母函数初学,其他简便方法】Holding Bin-Laden Captive! hdoj 1085

Holding Bin-Laden Captive! hdoj 1085 (母函数初学,其他简便方法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085
题目大意:给你1,2,5元的硬币数量,求没办法组成的最小数。
非母函数求解,可见如果可以分成三种情况:1.没有1元的就最小为1;
2.如果1,2没办法组成1-4所有的数,那轮不到用5就结束了,此时最小为a+b*2+1;
3.另外可以组成1-4则,在最大数a+b*2+c*5的数均可得到。
ac代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int a,b,c;
    while(cin >> a >> b >> c)
    {
        if(a==0 && b==0 && c==0)
            break;
        if(a == 0)
            printf("1\n");
        else if(a+b*2 < 4)
            printf("%d\n",a+b*2+1);
        else
            printf("%d\n",a+b*2+c*5+1);
    }
    return 0;
}

用母函数的话,既然是初学,我们先了解下母函数
可以看下我转载的一篇关于母函数的文章。
http://blog.csdn.net/qq_33199236/article/details/51253524
关于这一题的话:其母函数应该为(1+x^1+x^2+….x^a) * (1+x^2+x^4+….x^(2b)) * (1+x^5+x^10+…+x^(5c));

ac代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int c1[10000],c2[10000];
int main()
{
    int a,b,c;
    while(cin >> a >> b >> c)
    {
        if(a==0 && b==0 && c==0)
            break;
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        for(int i=0;i<=a;i++)
            c1[i] = 1;
        // 模拟前两个式子
        for(int i=0;i<=a;i++)
            for(int j=0;j<=2*b;j+=2)
                c2[i+j]+=c1[i];
        for(int i=0;i<=a+2*b;i++)
            c1[i]=c2[i];
        memset(c2,0,sizeof(c2));
        for(int i=0;i<=a+2*b;i++)
            for(int j=0;j<=5*c;j+=5)
                c2[i+j]+=c1[i];
        int i;
        for(i=0;i<=a+2*b+5*c;i++)
        {
            if(c2[i] == 0)
                break;
        }
        printf("%d\n",i);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值