题目传送门: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): 22685 Accepted Submission(s): 10091
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!
“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!
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
42种做法第一种:母函数 31ms//母函数的稍微变形 //G(x) = (1+x+x^2+...)(1+x^2+x^4+...)(1+x^5+x^10+...) //每个表达式的元素个数取决于输入的数 //因为只有1,2,5,所以只需要初始化第一个表达式,然后2次循环分别计算第二个表达式和第三个表达式 //maxn的取值来源于(1+2+5)*1000 #include <stdio.h> #include <string.h> #include <algorithm> #define maxn 8005 using namespace std; int c1[maxn]; int c2[maxn]; int main() { int num1, num2, num3; while(~scanf("%d%d%d", &num1, &num2, &num3)) { if(num1+num2+num3<=0) break; memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); //初始化第一个表达式 for(int i=0; i<=num1; i++) { c1[i] = 1; } //计算第一个表达式与第二个表达式相乘后的结果 for(int j=0; j<=num1; j++) { for(int k=0; k<=2*num2; k+=2) { c2[j+k] += c1[j]; } } for(int k=0; k<=num1+2*num2; k++) { c1[k] = c2[k]; c2[k] = 0; } //计算三个表达式相乘后的结果 for(int j=0; j<=num1+2*num2; j++) { for(int k=0; k<=5*num3; k+=5) { c2[j+k] += c1[j]; } } //此处无需再将数组c2赋给c1,结果可直接由c2输出 for(int i=1; i<=num1+2*num2+5*num3+1; i++) { if(c2[i]==0) { printf("%d\n", i); break; } } } return 0; }
第二种:写几组数据,然后找规律,15ms//将所有的1,2,5存入一个数组中,并增加一个元素0x3f3f3f3f,然后进行排序 //如果出现一个s[i]大于s[0]到s[i-1]的总和+1, 说明组合不了s[0]到s[i-1]的总和+1,即结果 #include <stdio.h> #include <algorithm> #define maxn 3500 using namespace std; int s[maxn]; int main() { int num1, num2, num3; while(scanf("%d%d%d", &num1, &num2, &num3)) { int n = num1+num2+num3; if(num1<0||num2<0||num3<0 || (n==0)) break; int k=0; while(num1--) s[k++] = 1; while(num2--) s[k++] = 2; while(num3--) s[k++] = 5; sort(s, s+k); int t=0; int ans=0; s[k] = 0x3f3f3f3f; for(int i=0; i<=k; i++) { if(s[i]>t+1) { ans = t+1; break; } else { t += s[i]; } } printf("%d\n", ans); } return 0; }