[HDU 1085] Holding Bin-Laden Captive! 母函数或多重背包

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

题意:有三个硬币面值分别是1,2,5。输入三个硬币的个数,要求出这三个硬币不能组成的最小的面值。

思路:可以用母函数或者多重背包。

母函数代码(171ms):

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int coin[3] = {1, 2, 5};

int nex[8005],temp[8005];

int main()
{
    int num[3];
    while(cin>>num[0]>>num[1]>>num[2] && (num[0] || num[1] || num[2]))
    {
        int maxn = num[0] * 1 + num[1] * 2 + num[2] * 5; //最高次幂
        memset(nex, 0, sizeof(nex));
        memset(temp, 0, sizeof(temp));
        for(int i = 0; i <= num[0]; i++){
            nex[i] = 1;
        }
        //母函数为(1+x+x^2+...x^num[0])(1+x^2+x^4+....x^2*num[1])(1+x^5+x^10+...+x^5*num[2]) 
        for(int i = 1; i < 3; i++) //一共三项
        {
            for(int j = 0; j <= maxn; j++)  //x^j
            {
                for(int k = 0; k + j <= maxn && k <= coin[i] * num[i]; k += coin[i]){    //x^k
                    temp[k+j] += nex[j];  //相乘后x^(k+j)的系数加上x^k的系数乘以x^j的系数
                }
            }
           //更新
            for(int j = 0; j <= maxn; j++){
                nex[j] = temp[j];
                temp[j] = 0;
            }
        }
        for(int i = 0; i <= maxn+1; i++){
            if(nex[i] == 0)
            {
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

多重背包(0ms):

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

int dp[8005];

void ZeroOnePack(int volume, int val, int vol) //01背包
{
    for(int i = volume; i >= vol; i--){
        dp[i] = dp[i-vol] + val;
    }
}

void CompletelyPack(int volume, int val, int vol) //完全背包
{
    for(int i = vol; i <= volume; i++){
        dp[i] = dp[i-vol] + val;
    }
}

void MultiplePack(int volume, int val, int vol, int counts)//多重背包
{
    if(vol * counts > volume){  //装不下当前物体,看作完全背包
        CompletelyPack(volume, val, vol);
    } else{
        int mid = 1;
        while(mid <= counts){  //将物体拆成1,2,4,.....  然后当成01背包
            ZeroOnePack(volume, val*mid, vol*mid);
            counts = counts - mid;
            mid = mid << 1;
        }
        if(counts){
            ZeroOnePack(volume, val*counts, vol*counts);  //剩下的
        }
    }
}

int main()
{
    int x, y, z;
    while(cin>>x>>y>>z && (x || y || z))
    {
        memset(dp, 0, sizeof(dp));
        int volume = x + 2 * y + 5 * z;
        MultiplePack(volume, 1, 1, x);
        MultiplePack(volume, 2, 2, y);
        MultiplePack(volume, 5, 5, z);
        for(int i = 1; i <= volume+1; i++){
            if(dp[i] != i){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

achonor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值