HDU 1085 母函数 硬币组合

题目如下:

Holding Bin-Laden Captive!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13245    Accepted Submission(s): 5931


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
 

先来一段暴力的做法:

#include<iostream>
using namespace std;
#define M 8000
void main()
{
	int a,b,c,x[M+10],i,j,k;
	while(cin>>a>>b>>c,a+b+c)
	{
		memset(x,0,sizeof(x));
		for(i=0;i<=c;i++)
			for(j=0;j<=b;j++)
				for(k=0;k<=a;k++)
					x[5*i+2*j+k]=1;
		i=0;
		while(++i)
			if(x[i]==0)
				break;
		cout<<i<<endl;
	}
}

这题目还可以尝试母函数的做法:

#include <iostream>
using namespace std;
int c1[10000], c2[10000];
int num[4];
int main()
{
    int nNum;
    while(scanf("%d %d %d", &num[1], &num[2], &num[3]) && (num[1]||num[2]||num[3]))
    {
        int _max = num[1]*1+num[2]*2+num[3]*5;
        for(int i=0; i<=_max; ++i)
        {
            c1[i] = 0;
            c2[i] = 0;
        }
        for(int i=0; i<=num[1]; ++i)
            c1[i] = 1;
        for(int i=0; i<=num[1]; ++i)
            for(int j=0; j<=num[2]*2; j+=2) 
                c2[j+i] += c1[i];
        for(int i=0; i<=num[2]*2+num[1]*1; ++i)
        {
            c1[i] = c2[i];
            c2[i] = 0;
        }
        for(int i=0; i<=num[1]*1+num[2]*2; ++i)
            for(int j=0; j<=num[3]*5; j+=5)
                c2[j+i] += c1[i];
        for(int i=0; i<=num[2]*2+num[1]*1+num[3]*5; ++i)
        {
            c1[i] = c2[i];
            c2[i] = 0;
        }
        int i;
 
        for(i=0; i<=_max; ++i)
            if(c1[i] == 0)
            {
                printf("%d\n", i);
                break;
            }
        if(i == _max+1)
            printf("%d\n", i);
    }
    return 0;
}

附母函数模板:

#include <iostream>
using namespace std;
const int _max = 10001; 
// c1是保存各项质量砝码可以组合的数目
// c2是中间量,保存每一次的情况
int c1[_max], c2[_max];   
int main()
{	 
	int nNum;
	int i, j, k;
	while(cin >> nNum)
	{
		for(i=0; i<=nNum; ++i)   // ---- ①
		{
			c1[i] = 1;
			c2[i] = 0;
		}
		for(i=2; i<=nNum; ++i)   // ----- ②
		{
			for(j=0; j<=nNum; ++j)   // ----- ③
				for(k=0; k+j<=nNum; k+=i)  // ---- ④
				{
					c2[j+k] += c1[j];
				}
			for(j=0; j<=nNum; ++j)     // ---- ⑤
			{
				c1[j] = c2[j];
				c2[j] = 0;
			}
		}
		cout << c1[n] << endl;
	}
	return 0;
}

我们来解释下上面标志的各个地方:

① 、首先对c1初始化,由第一个表达式(1+x+x2+..xn)初始化,把质量从0到n的所有砝码都初始化为1.

② 、 i从2到n遍历,这里i就是指第i个表达式,上面给出的第二种母函数关系式里,每一个括号括起来的就是一个表达式。

③、j 从0到n遍历,这里j就是只一个表达式里第j个变量,比如在第二个表达式里:(1+x2+x4....)里,第j个就是x2*j.

③ 、k表示的是第j个指数,所以k每次增i(因为第i个表达式的增量是i)。

④ 、把c2的值赋给c1,而把c2初始化为0,因为c2每次是从一个表达式中开始的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值