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
Author
lcy
這道題算是母函數當中比較簡單的一道題,只要你能夠求出所有的組合個數,就可以解出來,關於母函數的詳解, 發代碼吧!
//母函數問題,整數的拆分
//以前做過這一類的題,現在回顧一下
//我靠,這道題貌似比以前做的母函數的題目還要簡單
//只要求出有多少種組合方案就可以了
//以下的兩種方法皆可以做!
/*
#include<iostream>
using namespace std;
int main()
{
int n,m,j,i,k;
while(cin>>m>>n>>k &&( m!=0 || n!=0 || k!=0))
{
int flag[10001],temp[10001];//用於標志可不可以組合,如果可以組合出i,則flag[i]=1,否則為0
memset(flag,0,sizeof(flag));//初始化,全部標為0
memset(temp,0,sizeof(temp));
for(i=0;i<=m;i++) flag[i]=1;//先是1
for(i=0;i<=n*2;i=i+2)//再2
{
for(j=0;j<=m;j++)
temp[i+j]=1;
}
for(i=0;i<=m+2*n;i++)
if(temp[i]==1)
flag[i]=1;
memset(temp,0,sizeof(temp));
for(i=0;i<=k*5;i=i+5)//再5
{
for(j=0;j<=m+2*n;j++)
if(flag[j]==1)
temp[i+j]=1;
}
for(i=0;i<=m+2*n+5*k;i++)
if(temp[i]==1)
flag[i]=1;
for(i=0;i<=2*n+m+5*k;i++)
{
if(flag[i]==0)
break;
}
cout<<i<<endl;
}
return 0;
}
*/
#include<iostream>
using namespace std;
const int lmax=10000;
int c1[lmax+1],c2[lmax+1];
int main()
{
int n,m,k,i,j;
while(cin>>m>>n>>k && (m!=0 || n!=0 || k!=0))
{
int sum=m+2*n+5*k;
for(i=0;i<=sum;i++){c1[i]=0;c2[i]=0;}
for(i=0;i<=m;i++) c1[i]=1;
for(i=0;i<=m;i++)
for(j=0;j<=2*n;j=j+2)
c2[i+j]+=c1[i];
for(j=0;j<=sum;j++){c1[j]=c2[j];c2[j]=0;}
for(i=0;i<=m+2*n;i++)
for(j=0;j<=k*5;j=j+5)
c2[j+i]+=c1[i];
for(j=0;j<=sum;j++){c1[j]=c2[j];c2[j]=0;}
for(i=0;i<=sum;i++)
{
if(c1[i]==0)
{printf("%d\n",i);break;}
}
if(i==sum+1)
printf("%d\n",i);
}
return 0;
}