HDU 1085 Holding Bin-Laden Captive! 母函数③

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
 总结:此题前两道母函数题的不同之处在于  每种硬币不是唯一也不是无限,所以要适当改一些地方。
code:
View Code
#include<stdio.h>
int main()
{
int c1[10001],c2[10001];
int max,i,j,k,n1,n2,n3;;
while(scanf("%d%d%d",&n1,&n2,&n3)!=EOF)
{
if(n1==0&&n2==0&&n3==0)
break;
max=n1+n2*2+n3*5; //这里存在能够支付的最大值
for(i=0;i<=max;i++)
{c1[i]=0;c2[i]=0;}

for(i=0;i<=n1;i++)
c1[i]=1;

for(i=0;i<=n1;i++)
for(j=0;j<=n2*2;j+=2) //步长为二,n2*2 意为用2元支付的最大值
c2[j+i]+=c1[i];
for(i=0;i<=n1+n2*2;i++) //应为前两种硬币支付的最大值为n1+n2*2
{
c1[i]=c2[i];
c2[i]=0;
}
for(i=0;i<=n1+n2*2;i++)
for(j=0;j<=n3*5;j+=5)
c2[i+j]+=c1[i];

for(i=0;i<=n1+n2*2+n3*5;i++) //前三种硬币支付的最大值为n1+n2*2+n3*3
{
c1[i]=c2[i];
c2[i]=0;
}
for(i=0;i<=max;i++)
if(c1[i]==0)
{
printf("%d\n",i);
break;
}
if(i==max+1)//laden有可能买到从1到max之间任意价格的物品,那么max+1就是买不到的最小值。
printf("%d\n",i);
}
return 0;
}


posted @ 2012-04-06 16:47  'wind  阅读(156)  评论(0编辑  收藏  举报