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! ”

HDU 1085 Holding Bin-Laden Captive! (母函数)_第1张图片

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,并且给定三种硬币的数量分别为num_1,num_2,num_5,然后让求解这些硬币所不能组成的最小的面额是多少,例如给定的例子,面值为1的硬币为1枚,面值为2的硬币为1枚,面值为5的硬币为3枚,则可以组成的面值有1,2,3,5......所以,不能组成的最小面额为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;
}



你可能感兴趣的:(HDU,组合数,母函数)