题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085
Holding Bin-Laden Captive!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25635 Accepted Submission(s): 11323
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
Recommend
We have carefully selected several similar problems for you: 2152 1709 2069 1059 2566
这个题目想很久,想办法各种优化自己的蠢算法,还是太年轻了,看到别人家的博客,用母函数做的,模板敲上去,理解一下代码里面变量的意思,就基本上掌握了。
大佬关于母函数的讲解链接:https://blog.csdn.net/xiaofei_it/article/details/17042651
号称史上最强讲解,没错了,但更喜欢的是里面配套的hdu习题
一般模板(ac代码如下):
#include<iostream>
#include<cstring>
using namespace std;
int v[3]= {1,2,5}; //面值
int n[3];
int a[9050],b[9050];
int main() {
while(scanf("%d%d%d",&n[0],&n[1],&n[2])==3&&(n[0]||n[1]||n[2])) {
memset(a,0,sizeof(a));
a[0]=1;
int last=n[0]+2*n[1]+5*n[2]; //最大面值是多少
for(int i=0; i<3; i++) {
memset(b,0,sizeof(b));
for(int j=0; j<=n[i]; j++)
for(int k=0; k+j*v[i]<=last; k++)
b[k+j*v[i]]+=a[k];
memcpy(a,b,sizeof(b));
}
int idx;
for(idx=0; idx<=last; idx++)
if(a[idx]==0) {
break;
}
printf("%d\n",idx);
}
return 0;
}
优化模板的ac代码:
#include<iostream>
#include<cstring>
using namespace std;
int v[3]= {1,2,5};
int n[3];
int a[9050],b[9050];
int main() {
while(scanf("%d%d%d",&n[0],&n[1],&n[2])==3&&(n[0]||n[1]||n[2])) {
a[0]=1;
int last=0;
for(int i=0; i<3; i++) {
int last2=last+n[i]*v[i];
memset(b,0,sizeof(int)*(last2+1));
for(int j=0; j<=n[i]&&j*v[i]<=last2; j++)
for(int k=0; k<=last&&k+j*v[i]<=last2; k++)
b[k+j*v[i]]+=a[k];
memcpy(a,b,sizeof(int)*(last2+1));
last=last2;
}
int idx;
for(idx=0; idx<=last; idx++)
if(a[idx]==0) {
break;
}
printf("%d\n",idx);
}
return 0;
}