Holding Bin-Laden Captive! ( HDU - 1085 ) (母函数)

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的金币,求:用这些金币不能组成的最小的数字。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <stack>
#define MAX 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
int a[11111],b[11111];
int main()
{
    int n1,n2,n3;
    while(cin >> n1 >> n2 >> n3&&(n1||n2||n3))
    {
        int e[3];
        e[0]=n1,e[1]=n2;e[2]=n3;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0;i<=e[0];i++)
        {
            a[i]=1;                              //把第一层括号的各项的系数初始化为 1 
            b[i]=0;
        }
        int t=e[0];
        for(int i=1;i<=2;i++)                    //剩下的两层括号
        {
            for(int j=0;j<=t;j++)                //新形成的括号的各项的指数
            {
                for(int k=0,p=0;p<=e[i];p++)//与后边的括号进行运算,k代表后边括号各项的指数
                {
                    b[k+j]+=a[j];             
                    if(i==1)
                        k+=2;
                    if(i==2)
                        k+=5;
                }

            }
            if(i==1)
               t+=e[i]*2;
            else
                t+=e[i]*5;
            for(int j=0;j<=t;j++)
            {
                a[j]=b[j];
                b[j]=0;
            }

        }
        int flag=0;
        for(int i=0;i<=t;i++)
        {
            if(a[i]==0)
            {
                flag=1;
                cout << i << endl;
                break;
            }

        }
        if(!flag)
            cout << t+1<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/M_Y_Y_/article/details/81230670
今日推荐