hdu 1085 Holding Bin-Laden Captive!(母函数/背包)

来源:互联网 发布:unity3d做建筑 编辑:程序博客网 时间:2024/05/21 08:49

Holding Bin-Laden Captive!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20721    Accepted Submission(s): 9207


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
 

题意:给出1元,2元,5元硬币的个数,求最小的不能组成的数

思路:很明显可以用三次多重背包做,每次的硬币价值为1,2,5

也可以用母函数,在整数分解的基础上改一改就好。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int main(){    int numa,numb,numc;    while(~scanf("%d %d %d",&numa,&numb,&numc)&&(numa+numb+numc))    {        int a[8005],b[8005];        int sum=numa+numb*2+numc*5;        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        for(int i=0; i<=numa; i++)            a[i]=1;        for(int j=0; j<=sum; j++)            for(int k=0,num=0; k+j<=sum&&num<=numb; k+=2,num++)                b[k+j]+=a[j];        for(int i=0; i<=sum; i++)        {            a[i]=b[i];            b[i]=0;        }        for(int j=0; j<=sum; j++)            for(int k=0,num=0; k+j<=sum&&num<=numc; k+=5,num++)                b[k+j]+=a[j];        for(int i=0; i<=sum; i++)            a[i]=b[i];        int flag=0;        for(int i=1; i<=sum; i++)        {            if(!a[i])            {                flag=1;                printf("%d\n",i);                break;            }        }        if(!flag) printf("%d\n",sum+1);           /* int dp[8005];            int sum=numa+numb*2+numc*5;        memset(dp,0,sizeof(dp));        for(int i=0; i<=numa; i++)            dp[i]=1;        for(int i=1;i<=numb;i++)            for(int j=sum;j>=2;j--)                dp[j]=dp[j-2];        for(int i=1;i<=numc;i++)            for(int j=sum;j>=5;j--)                dp[j]=dp[j-5];        int flag=0;        for(int i=1; i<=sum; i++)        {            if(!dp[i])            {                flag=1;                printf("%d\n",i);                break;            }        }        if(!flag) printf("%d\n",sum+1);*/    }    return 0;}




0 0
原创粉丝点击