HDU 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): 13861 Accepted Submission(s): 6230
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
这就是一道母函数的问题,可是这里并非求组合数。所以,不须要int行的数组来记录组合数,仅仅须要true和false来标示能否表示该面值就能够了。当然用int记录组合数也能够,但要注意。由于组合量很大,要注意溢出的情况,我试了一下能够,推断大于0的时候置1就能过。
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> #include <algorithm> using namespace std; const int MAX = 8003; const int type[3] = {1,2,5}; bool ans[MAX],tmp[MAX]; int cnt[3]; void work(){ int i,j,k; memset(ans,0,sizeof(ans)); memset(tmp,0,sizeof(tmp)); for(i=0;i<=cnt[0];++i){ ans[i] = true; } //(author : CSDN iaccepted) for(i=1;i<3;++i){ for(j=0;j<=MAX;++j){ for(k=0;k<=cnt[i] && j+k*type[i]<=MAX;++k){ //tmp[j+k*type[i]] += ans[j]; if(tmp[j+k*type[i]] || ans[j])tmp[j+k*type[i]] = true; } } for(j=0;j<=MAX;++j){ ans[j] = tmp[j]; tmp[j] = false; } } } int main(){ //freopen("in.txt","r",stdin); //(author : CSDN iaccepted) int res,i; while(scanf("%d %d %d",&cnt[0],&cnt[1],&cnt[2])!=EOF){ if(cnt[0]==0 && cnt[1]==0 && cnt[2]==0)break; work(); for(i=0;i<=MAX;++i){ if(!ans[i]){ res = i; break; } } printf("%d\n",res); } return 0; }