Holding Bin-Laden Captive!(hdu1085)——母函数

来源:互联网 发布:javascript input赋值 编辑:程序博客网 时间:2024/05/16 16:03
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 30 0 0
 

Sample Output

4

给出3个数,分别代表价值为1,2,5的数量,找出这些硬币不能组成的最小的数

这也是一个母函数模板的题目,只不过我们最好要更新当先cmax的值,然后最后找最小不能组成的值时要找到sum+1,因为有可能1-sum都能组成

#include <iostream>#include <cstring>#include <cstdio>using namespace std;int s[4],ans[8005],tmp[8005],cmax;int num[4];int mu(){    s[1]=1;    s[2]=2;    s[3]=5;    memset(ans,0,sizeof(ans));    memset(tmp,0,sizeof(tmp));    ans[0]=1;    cmax=0;    for(int i=1;i<=3;i++)    {        cmax+=num[i]*s[i];//更新j当前的最大值        for(int j=0;j<=cmax;j++)        for(int k=0;k<=num[i]&&k*s[i]+j<=8000;k++)        {            tmp[j+k*s[i]]+=ans[j];        }        memcpy(ans,tmp,sizeof(tmp));        memset(tmp,0,sizeof(tmp));    }    return 0;}int main(){    while(cin>>num[1]>>num[2]>>num[3])    {        if(num[1]==0&&num[2]==0&&num[3]==0)        break;        mu();        int sum=num[1]+num[2]*2+num[3]*5;        for(int i=1;i<=sum+1;i++)        if(ans[i]==0)        {            cout<<i<<endl;            break;        }    }    return 0;}

原创粉丝点击