[ACM] 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): 13505    Accepted Submission(s): 6094


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
 


 

Author
lcy

解题思路:

本题的母函数为  (1+x +x^2+x^3+.........x^num1)  *(  1+x^2+x^4+x^6+.........x^num2) *( 1+x^5+x^10+x^15+............x^num5)

其中num1代表一分的硬币有多少个,num2代表2分的硬币多少个,num5代表5分的硬币有多少个。

模拟三个式子相乘,先前两个,合并后和第三个相乘。一定要注意指数的变化范围。给定了num1 num2 num5后,每一个式子的最大指数和计算后的总式子的最大指数都是确定的。

代码:

#include <iostream>
using namespace std;
int num1,num2,num5;
int c[9000],temp[9000];//注意范围,计算方法为 1000*1+1000*2+1000*5 ,8000为最大指数。

int main()
{
    while(cin>>num1>>num2>>num5&&num1||num2||num5)
    {
        int i,j;
        int max=num1*1+num2*2+num5*5;//最大的指数
        for(i=0;i<=max;i++)
        {
            c[i]=0;
            temp[i]=0;
        }
        for(i=0;i<=num1;i++)//先模拟前两个式子相乘
            c[i]=1;
        for(i=0;i<=num1;i++)
            for(j=0;j<=2*num2;j+=2)//因为没有给定要求的最大指数,所以不用 j+i<= 多少
        {
            temp[j+i]+=c[i];
        }
        for(i=0;i<=num1+2*num2;i++)
        {
            c[i]=temp[i];
            temp[i]=0;
        }
        for( i=0;i<=num1+2*num2;i++)//模拟前两个式子合并后的式子与第三个式子相乘,num1+2*num2是前两个式子相乘后的最大指数
            for( j=0;j<=5*num5;j+=5)
            temp[j+i]+=c[i];
            
        for(i=0;i<=max;i++)
            c[i]=temp[i];
        for(i=0;i<=max;i++)
        {
            if(c[i]==0)//系数为0说明,该指数不存在
            {
                cout<<i<<endl;
                break;
            }
        }
        if(i>max)//最大指数都存在,要求最小不存在的只能是max+1
            cout<<i<<endl;
    }
    return 0;
}


 

原文地址:https://www.cnblogs.com/sr1993/p/3697768.html