“Oh, God! How terrible! ”
![](https://i-blog.csdnimg.cn/blog_migrate/b858f96d99fe58f794dd2613cb15429b.jpeg)
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!
1 1 3 0 0 0
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; }