Holding Bin-Laden Captive! 母函数

题目链接

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! ”
img
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 的金币,给你这三种金币的数量,求出它们不能构成最小的价值

如 1 1 3 可以 有1(1) 2(1个2)3 (1个2 + 1个1)5 (一个5)。。。由此发现没有4,所以不能构成最小的价值是4.

分析

母函数问题,它们所能构成的最大的价值(max)是每种金币的价值乘以它们的数量 之和,得出最终的表达式之后找出第一个系数为零的值,还有另一种情况,从0到max都能构成,这是答案就是max+1。

母函数释疑

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, c;
    while (scanf("%d%d%d", &a, &b, &c))
    {
        if(a==0&&b==0&&c==0)break;
        int sum[3], value[3];
        sum[0] = a;
        sum[1] = b;
        sum[2] = c;
        value[0] = 1;
        value[1] = 2;
        value[2] = 5;
        int c1[10000] = {0}, c2[10000] = {0};
        int n = sum[0] * value[0] + sum[1] * value[1] + sum[2] * value[2];
        for (int i = 0; i <= sum[0]*value[0]; i += value[0])
            c1[i] = 1;
        for (int i = 1; i < 3; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                for (int k = 0; k+j <= n && k<=sum[i]*value[i]; k += value[i])
                {
                    c2[k + j] += c1[j];
                }
            }
            for (int k = 0; k <= n; k++)
            {
                c1[k] = c2[k];
                c2[k] = 0;
            }
        }
        int i=0;
        for ( i = 0; i <= n; i++)
        {

            if (c1[i] == 0)
            {
                printf("%d\n", i);
                break;
            }
        }
        if(i==n+1)
            printf("%d\n",i);
    }
    return 0;
}

posted @ 2017-04-14 12:34  -梦里不知身是客  阅读(191)  评论(0编辑  收藏  举报