Holding Bin-Laden Captive!(母函数)

来源:互联网 发布:php微信授权获取code 编辑:程序博客网 时间:2024/05/17 04:57

Holding Bin-Laden Captive!

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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 30 0 0

Sample Output
4

Author
lcy


http://www.loveqiuqiu.com/?p=362

思路:
1。首先考虑5毛以下的。

如果这些都不能表示的话 那么最小不能表示的一定是这些数
2。如果没有1毛 那么最小不能表示的是1毛
然后只要有2毛了 然后就可以表示2+1和2-1
如果有1毛 但和2毛组合出来的数小于4的话
比如1+2=3小于4 那么最小不能表示的数是(1+2)+1
3。如果可以表示出4 那么只要有5毛存在了 5毛就可以表示了
然后每多个5毛就可以多表示5毛的数字
感觉跟平时买东西搞零钱一样
于是最小不能表示的就是所有的硬币加起来所表示的最大数+1


#include<cstdio>using namespace std;int main(){    int n1,n2,n5;    while(scanf("%d%d%d",&n1,&n2,&n5),n1+n2+n5)    {        if(!n1)            printf("1\n");        else if(n1+2*n2<4)            printf("%d\n",n1+2*n2+1);        else            printf("%d\n",n1+n2*2+n5*5+1);    }    return 0;}

母函数:
#include <iostream>using namespace std;int c1[8002], c2[8002];int t[3], d[4];int main(){    int i, j, k, n, s, a, b, c, q;    while(cin >> a >> b >> c, a|b|c) {        n = a + 2 * b + 5 * c;        for(i = 0; i <= n+1; i++)            c1[i] = c2[i] = 0;        for(i = 0; i <= a; i++) //一定要有底层            c1[i] = 1;        t[1] = 2; t[2] = 5;        d[0] = a; d[1] = b; d[2] = c;        for(i = 1; i <= 2; i++) {            for(j = 0; j <= n; j++) {                if(c1[j] == 0) continue; /**这里的continue是很重要的,一定要懂**/                for(k = 0, s = 0; k+j <= n && s <= d[i]; k+=t[i], s++) {                    c2[k+j] += c1[j];                }            }            for(j = 0; j <= n; j++) {                c1[j] = c2[j];                c2[j] = 0;            }        }        for(q = 0; q <= n+1; q++) {            if(c1[q] == 0) break;        }        cout << q << endl;    }    return 0;}

原创粉丝点击