天天看點

HDU 1085-Holding Bin-Laden Captive!(簡單的母函數) 題目傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=1085 Holding Bin-Laden Captive!

題目傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=1085

Holding Bin-Laden Captive!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 22685    Accepted Submission(s): 10091

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

HDU 1085-Holding Bin-Laden Captive!(簡單的母函數) 題目傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=1085 Holding Bin-Laden Captive!

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
  
  

  
  

  
  
   2種做法
  
  
   第一種:母函數 31ms
  
  
          
//母函數的稍微變形
//G(x) = (1+x+x^2+...)(1+x^2+x^4+...)(1+x^5+x^10+...)
//每個表達式的元素個數取決于輸入的數
//因為隻有1,2,5,是以隻需要初始化第一個表達式,然後2次循環分别計算第二個表達式和第三個表達式
//maxn的取值來源于(1+2+5)*1000
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 8005
using namespace std;

int c1[maxn];
int c2[maxn];

int main()
{
	int num1, num2, num3;
	while(~scanf("%d%d%d", &num1, &num2, &num3))
	{
		if(num1+num2+num3<=0)
			break;
		memset(c1, 0, sizeof(c1));
		memset(c2, 0, sizeof(c2));
		//初始化第一個表達式
		for(int i=0; i<=num1; i++)
		{
			c1[i] = 1;
		}
		//計算第一個表達式與第二個表達式相乘後的結果
		for(int j=0; j<=num1; j++)
		{
			for(int k=0; k<=2*num2; k+=2)
			{
				c2[j+k] += c1[j];
			}
		}
		for(int k=0; k<=num1+2*num2; k++)
			{
				c1[k] = c2[k];
				c2[k] = 0;
			}
		//計算三個表達式相乘後的結果
		for(int j=0; j<=num1+2*num2; j++)
		{
			for(int k=0; k<=5*num3; k+=5)
			{
				c2[j+k] += c1[j];
			}
		}
		//此處無需再将數組c2賦給c1,結果可直接由c2輸出
		for(int i=1; i<=num1+2*num2+5*num3+1; i++)
		{
			if(c2[i]==0)
			{
				printf("%d\n", i);
				break;
			}
		}
	}
	return 0;
}
           
第二種:寫幾組資料,然後找規律,15ms
//将所有的1,2,5存入一個數組中,并增加一個元素0x3f3f3f3f,然後進行排序
//如果出現一個s[i]大于s[0]到s[i-1]的總和+1, 說明組合不了s[0]到s[i-1]的總和+1,即結果
#include <stdio.h>
#include <algorithm>
#define maxn 3500
using namespace std;

int s[maxn];
int main()
{
	int num1, num2, num3;
	while(scanf("%d%d%d", &num1, &num2, &num3))
	{
		int n = num1+num2+num3;
		if(num1<0||num2<0||num3<0 || (n==0))
            break;
		int k=0;
		while(num1--)
			s[k++] = 1;
		while(num2--)
			s[k++] = 2;
		while(num3--)
			s[k++] = 5;
		sort(s, s+k);
		int t=0;
		int ans=0;
		s[k] = 0x3f3f3f3f;
		for(int i=0; i<=k; i++)
		{
			if(s[i]>t+1)
			{
				ans = t+1;
				break;
			}
			else
			{
				t += s[i];
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}