您的位置:首页 > 其它

(一题多解)Holding Bin-Laden Captive!(母函数模板变形、DP、多重背包)

2014-12-20 22:05 288 查看
Link: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): 15763 Accepted Submission(s): 7064



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



My code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
	int c1[8888],c2[8888];
	int i,j,k,n[4];
	while(scanf("%d%d%d",&n[1],&n[2],&n[3])!=EOF)
	{
		if(n[1]==0&&n[2]==0&&n[3]==0)
			break;
		memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
		for(i=0;i<=n[1];i+=1)
		{
			c1[i]=1;
		}
		for(i=0;i<=n[1]*1;i+=1)
		{
			for(j=0;j<=n[2]*2;j+=2)
			{
				c2[j+i]+=c1[i];
			}
		}
		for(i=0;i<=n[1]*1+n[2]*2;i++)
		{
			c1[i]=c2[i];
			c2[i]=0;
		}
		for(i=0;i<=n[2]*2+n[1]*1;i++)
		{
			for(j=0;j<=n[3]*5;j+=5)
			{
				c2[j+i]+=c1[i];
			}
		}
		for(i=0;i<=n[1]*1+n[2]*2+n[3]*5;i++)
		{
			c1[i]=c2[i];
			c2[i]=0;
		}
		for(i=1;;i++)
		{
			if(!c1[i])
			{
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}


code2:

以下参考:http://acm.hut.edu.cn/?p=277

/题意:给你面值是1,2,5的硬币的数量,要你求由这些硬币不能组成的最小的金额。。

下面给出3种方法;

//方法1:很明显母函数

//我这里一步一步的求。。

//下面是我的一点理解。。如果叫你写由面值1,2,5的硬币所组成的金额的母函数

//Y=(1+x+x^2+x^3+x^4…+x^n1*1)*(1+x^2+x^4+x^6…x^n2*2)*(1+x^5+x^10+…x^n3*5)

#include<stdio.h>

#include<string.h>

#define MAX 10010

int num[3];

int val[3]={1,2,5};

int n;

int c1[MAX],c2[MAX];

int main( )

{
	
	while(scanf("%d%d%d",&num[0],&num[1],&num[2]),num[0]||num[1]||num[2])
		
	{
		
		int i,j,k,t;
		
		n=num[0]*1+num[1]*2+num[2]*5;// num[0]*val[0]+num[1]*val[1]+num[2]*val[3] budui
		
		memset(c1,0,sizeof(c1));
		
		memset(c2,0,sizeof(c2));
		
		for( i=0;i<=num[0];i+=val[0])
			
		{
			
			c1[i]=1;
			
		}
		
		for( i=1;i<=2;i++)
			
		{
			
			
			
			for( j=0;j<=n;j++)
				
				for( k=0;k<=num[i]*val[i]&&k+j<=n;k+=val[i])
					
					c2[j+k]+=c1[j];
				
				for(t=0;t<=n;t++)
					
				{
					
					c1[t]=c2[t];
					
					c2[t]=0;
					
				}
				
		}
		
		for(i=0;i<=n+1;i++)
			
			if(c1[i]==0)
				
				
				
			{
				
				printf("%d\n",i);
				
				break;
				
				
				
			}
			
			
			
	}

	return 0;

	
}


上面代码的for循环可合并代码:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=8010;

int num[4],money[4]={0,1,2,5};
int c1
,c2
;

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d%d",&num[1],&num[2],&num[3])){
        if(num[1]==0 && num[2]==0 && num[3]==0)
            break;
        for(int i=0;i<N;i++){
            c1[i]=0;
            c2[i]=0;
        }
        c1[0]=1;
        int maxx=0;
        for(int i=1;i<=3;i++){
            maxx+=num[i]*money[i];
            for(int j=0;j<=maxx;j++)
                for(int k=0;k<=num[i] && j+k*money[i]<=maxx;k++)
                    c2[j+k*money[i]]+=c1[j];
            for(int j=0;j<=maxx;j++){
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        for(int i=1;;i++)
            if(c1[i]==0){
                printf("%d\n",i);
                break;
            }
    }
    return 0;
}


注:此代码可改为每种种类个数无限的情况,给出模板如下:

针对此题:http://acm.hdu.edu.cn/showproblem.php?pid=1028

#include<stdio.h>
#include<string.h>
#define MAX 10010
int num[3];
int val[3]={1,2,5};
int n,m;
int c1[MAX],c2[MAX];
int main()
{
	while(scanf("%d",&m)!=EOF)
	{
		int i,j,k,t;
		memset(c1,0,sizeof(c1));
		memset(c2,0,sizeof(c2));
		for( i=0;i<=m;i++)	
		{
			c1[i]=1;
			c2[i]=0;
		}
		for( i=2;i<=m;i++)	
		{
			for(j=0;j<=m;j++)
				for( k=0;k+j<=m;k+=i)
					c2[j+k]+=c1[j];
				for(t=0;t<=m;t++)	
				{
					c1[t]=c2[t];
					c2[t]=0;	
				}		
		}
				printf("%d\n",c1[m]);		
	}
	return 0;	
}


HDU 1085 HoldingBin-Laden Captive!

分类: 三、动态规划之--硬币系统2012-10-0216:39 89人阅读 评论(0) 收藏 举报
poj 1085
题目大意:
有一个币值系统,里面有 1元,2元,5元,其数量分别为
num1 张,num2
张,num3张,
现在需要求出最小不能表示的币值。
解题思路:
可以分为三类:(1)没有 1 元的,那么输出
1;
(2)没有5 元的或者(1,2
元的价值总和小于 4),那么输出值为 num1+2 * num2+1;
(3)1,2
元的价值总和〉= 4,那么就可以 4—〉5,即最大可表示数为:num1+2
*num2+5*num3;

code:

#include<stdio.h>
int main()
{
   int num1,num2,num3,max12,max;
   while(1)
    {
       scanf("%d%d%d",&num1,&num2,&num3);
       if(num1==0&&num2==0&&num3==0)break;
       if(num1==0){printf("1\n");continue;}
        max12=num2*2+num1;
       if(num3==0||max12<4)
       {printf("%d\n",max12+1);continue;}
       //只要有一个4,就可以变为5;即最大数为下面的max
       max=num1+num2*2+num3*5;
       printf("%d\n",max+1);
    }
   return 0;
}


3.多重背包方法:
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int num[3],money[3]={1,2,5};
int dp[8010];

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d%d",&num[0],&num[1],&num[2])){
        if(num[0]==0 && num[1]==0 && num[2]==0)
            break;
        memset(dp,0,sizeof(dp));
        int tot=num[0]*1+num[1]*2+num[2]*5;
        dp[0]=1;
        for(int i=0;i<3;i++)    //简单的多重背包
            for(int j=0;j<num[i];j++)
                for(int k=tot;k>=money[i];k--)
                    dp[k]+=dp[k-money[i]];
        int ans=tot+1;
        for(int i=1;i<=tot;i++)
            if(dp[i]==0){
                ans=i;
                break;
            }
        printf("%d\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: