Holding Bin-Laden Captive! (HDU-1085)

博客围绕一个数学问题展开,已知三种硬币(1角、2角、5角)的个数,求最小不能组成的钱数。输入包含多组测试用例,输出每组对应的结果。解题思路是使用普通母函数,跑完模版后枚举幂次寻找不能组成的钱数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China!

 “Oh, God! How terrible! ” 

Don’t be so afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out. Laden is so bored recent years that he fling himself into some math problems, and he said that if anyone can solve his problem, he will give himself up!

Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his problem? 

“Given some Chinese Coins (硬币) (three kinds-- 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.” 

You, super ACMer, should solve the problem easily, and don’t forget to take $25000000 from Bush! 

Input

Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed. 

Output

Output the minimum positive value that one cannot pay with given coins, one line for one case. 

Sample Input

1 1 3
0 0 0

Sample Output

4

题意:每组给出 1 角、2 角、5 角硬币的个数 num1、num2、num5,问最小的不能组成的钱数

思路:普通母函数

根据题意,使用模版 2 较为方便,只需考虑 n2 数组的影响即可

首先 k=3,v[0]=1,v[1]=2,v[2]=3,n2[0]=num1,n2[1]=num2,n2[2]=num5,n1[0]=n1[1]=n1[2]=0

其次 P 可忽略,那么 last2 改为 last2=last+n[i]*v[i]

由于要求最小的不能组成钱数,那么在跑完模版后,枚举所有的幂次从 0 到 last,寻找 a[i]=0 的序号 i 即可。

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 10000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

int a[N];//权重为i的组合数
int b[N];//临时数组
int v[N],n1[N],n2[N];
int main(){
    v[0]=1;
    v[1]=2;
    v[2]=5;
    while(scanf("%d%d%d",&n2[0],&n2[1],&n2[2])!=EOF&&(n2[0]+n2[1]+n2[2])){
        a[0]=1;
        memset(n1,0,sizeof(n1));
        int last=0;
        for(int i=0; i<3; i++) {
//          int last2=min(last+n2[i]*v[i],P);//计算下一次的last
            int last2=last+n2[i]*v[i];
            memset(b,0,sizeof(int)*(last2+1));//只清空b[0..last2]
            for(int j=n1[i]; j<=n2[i]&&j*v[i]<=last2; j++) //last2
                for(int k=0; k<=last&&k+j*v[i]<=last2; k++) //一个是last,一个是last2
                    b[k+j*v[i]]+=a[k];
            memcpy(a,b,sizeof(int)*(last2+1));//b赋值给a,只赋值0..last2
            last=last2;//更新last
        }

        int i;
        for(i=0;i<=last;i++)
            if(a[i]==0)
                break;
        printf("%d\n",i);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值