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

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,2,5钞票的数量(num_1,num_2,num_3)求不能凑出的最小面值

题解

母函数

母函数:1+x+x^2+x^3+.....+x^num_1)*(1+x^2+x^4+....+x^2*num_2)*(1+x^5+x^10+...+x^5*num_5)
写母函数的时候一定先把母函数写出来 我们再写程序!

AC代码

其中注释的地方也是核心部分的写法

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
ll c1[200000],c2[200000];
int main() {
    ios::sync_with_stdio(false);
    int p[3] = {1,2,5};
    int nums[3];
    while (cin >> nums[0] >> nums[1] >> nums[2])
    {
        if(nums[0] == 0 && nums[1] == 0 && nums[2] == 0)break;
        int  s = 0;
        for (int i = 0; i < 3; ++i) {
            s += (nums[i]*p[i]);
        }
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        for (int i = 0; i <= nums[0]; ++i)
            c1[i] = 1;
//        for (int i = 1; i < 3; ++i) {
//            for (int j = 0; j <= s; ++j) {
//                for (int k = 0; k <= nums[i] && j+k*p[i] <= s; ++k) {
//                    c2[j+k*p[i]] += c1[j];
//                }
//            }
//------------start 这部分也可以替换成上面那样-------
            for (int i = 1; i < 3; ++i) {
                for (int j = 0; j <= s; ++j) {
                    for (int k = 0; k <= nums[i]*p[i] && j+k <= s; k += p[i]) {
                        c2[j+k] += c1[j];
                    }
                }
            for (int l = 0; l <= s; ++l) {
                c1[l] = c2[l];
                c2[l] = 0;
            }
        }
//-------------end-------------------------------------------------
        for (int i = 1; i <= s + 1; ++i) {
            if(!c1[i])
            {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}

完全背包 & 多重背包

因为不久前学过背包 这题用背包同样好解 不过这题背包比母函数慢 可能是我不会优化

#include <iostream>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
int nums[3],dp[200000];
int p[3] = {1,2,5};
int main()
{
    while (cin >> nums[0] >> nums[1] >> nums[2])
    {
        int s = 0;
        for (int i = 0; i < 3; ++i) {
            s += nums[i] * p[i];
        }
        if(!s)break;
        memset(dp,-INF,sizeof(dp));
        dp[0] = 0;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < nums[i]; ++j) {
                for (int k = s; k >= p[i] ; --k) {
                    dp[k] = max(dp[k],dp[k - p[i]] + p[i]);
                }
            }
        }
        for (int i = 1; i <= s + 1; ++i) {
            if(dp[i] < 0)
            {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}
Last modification:April 12th, 2020 at 02:20 am
如果觉得我的文章对你有用,请随意赞赏