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

    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
    ",res);
        }
        return 0;
    }



  • 相关阅读:
    跨域(cross-domain)访问 cookie (读取和设置)
    实用的PHP正则表达式
    Leetcode:find_minimum_in_rotated_sorted_array
    spring Jdbc自己主动获取主键。
    《学习opencv》笔记——矩阵和图像操作——cvSetIdentity,cvSolve,cvSplit,cvSub,cvSubS and cvSubRS
    HTML5 input placeholder 颜色 改动
    Java面试宝典2014版
    Go语言 关于go error处理风格的一些讨论和个人观点(上)
    动静结合学内核:linux idle进程和init进程浅析
    【Bootstrap3.0建站笔记二】button可下拉弹出层
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5177049.html
Copyright © 2020-2023  润新知