HDOJ 1085 Holding Bin-Laden Captive!
Holding Bin-Laden Captive!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21379 Accepted Submission(s): 9486
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! ”
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
题意:
给出若干枚1元2元和5元硬币,求问最小的无法组成的面值...
分析:
我们可以把它写成生成函数的形式:$f(x)=(1+x+x^{2}+……+x^{a})(1+x^{2}+x^{4}+……+x^{2b})(1+x^{5}+x^{10}+……+x^{5c})$...
对于每一个x项,它的指数代表可以组成的硬币的面值,系数代表方案数...乘起来之后的所有x项的指数就是可以组成的面值...
然后我们可以暴力$O(n^{2})$的计算多项式乘法...因为我们只需要知道指数为x的那一位系数是否为0,所以可以用bitset优化...
但是对于此题来说有一个很机智的做法:感谢@YouSiki...
http://www.cnblogs.com/yousiki/p/6422036.html
代码:
bitset暴力:
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<bitset> //by NeighThorn using namespace std; const int maxn=1000+5; bitset<8005> s; int a,b,c,ans; signed main(void){ while(scanf("%d%d%d",&a,&b,&c)){ if(a==0&&b==0&&c==0) break; s.reset(); for(int i=0;i<=a;i++) for(int j=0;j<=b;j++) s.set(i+j*2); for(int i=a+b*2;i>=0;i--) if(s[i]) for(int j=c;j>=0;j--) s.set(i+j*5); int ans=1; while(s[ans]) ans++; printf("%d\n",ans); } return 0; }
机智做法:
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> //by NeighThorn using namespace std; int a[3],s[3]={1,2,5}; signed main(void){ while(scanf("%d%d%d",&a[0],&a[1],&a[2])){ if(a[0]==0&&a[1]==0&&a[2]==0) break; int ans=1; while(13){ int sum=0; for(int i=0;i<3;i++) if(s[i]<=ans) sum+=s[i]*a[i]; if(sum<ans){ printf("%d\n",ans); break; } else ans=sum+1; } } return 0; }
By NeighThorn