Holding Bin-Laden Captive!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18700 Accepted Submission(s): 8327
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
4
题意:输入分别有多少个1、2、5,找x系数为0时,对应的最小指数。
G(x)=(1+x+x^2+..)(1+x^2...)(1+x^5+..) 由3个表达式构成,直接展开求解即可。
之前c1[1000]开小了,导致一直显示超时,其实不是超时的原因。如果你前面几个测试用例算的都是正确的,然后后面的算了半天还没算出结果就已经超过规定时间了,但其实就算再给你时间你算出来也是错的 这样的情形下,也是Time Limit Exceeded
再比如,你前面的测试用例算出来都是正确,后面的某个测试用例输入后你就死循环了,把时间全都耗光在某一个测试用例里了,那么你也是超时(Time Limit Exceeded),但实际上你的算法还是错的,给你无限的时间你也算不出结果
#include<stdio.h>
using namespace std;
int c1[10000],c2[10000];
int main(void)
{
int a,b,c;
while (scanf("%d %d %d",&a,&b,&c)!=EOF&&(a||b||c))
{
int _max=a+2*b+5*c;
for(int i=0;i<=_max;i++)
{
c1[i]=0;
c2[i]=0;
}
for(int i=0;i<=a;i++)//第一个表达式
{
c1[i]=1;
}
for(int j=0;j<=a;j++) //第一项乘第二项
{
for(int k=0;k<=b*2;k+=2)
{
c2[j+k]+=c1[j];
}
}
for(int q=0;q<=a+2*b;q++)//归并同类项
{
c1[q]=c2[q];
c2[q]=0;
}
for(int j=0;j<=a+2*b;j++) //接着乘第三项
{
for(int k=0;k<=c*5;k+=5)
{
c2[j+k]+=c1[j];
}
}
for(int q=0;q<=_max ;q++)//归并同类项
{
c1[q]=c2[q];
c2[q]=0;
}
int i;
for(i=0; i<=_max; ++i)
if(c1[i] == 0)
{
printf("%d\n", i);
break;
}
if(i == _max+1)
printf("%d\n", i);
}
return 0;
}