HDU 1085 Holding Bin-Laden Captive! 活捉本拉登(AC代码)普通型母函数

题意:有面值分别为1、2、5的硬币,分别有num_1、num_2、num_5个,问不能组成的最小面值是多少?(0<=每种硬币个数<=1000,组成的面值>0)

思路:母函数解决。只有3个括号要作乘法,分别代表面值1、2、5所能组成的情况。需要两个数组,所能组成的最大值为num_1+2*num_2+5*num_5。如果在这个范围内都能组成,那么最小不能组成的面值为num_1+2*num_2+5*num_5+1。若没有1分钱的硬币,那么不能组成的肯定是1了。

数组的用法:ans[]保存第一个括号→sup保存前两个括号的结果→ans[]保存最后结果。

 1 #include <iostream>
 2 #define N 8100
 3 using namespace std;
 4 int num_1,num_2,num_5,ans[N],sup[N],tar;
 5 int cal_and_search()
 6 {//ans[]→sup[]→ans[]
 7     int i,j,k;
 8     num_2*=2;
 9     num_5*=5;
10     memset(ans,0,sizeof(ans));    //清零
11     memset(sup,0,sizeof(sup));
12     for(i=0;i<=num_1;i++)    //初始化num_1+1个喔
13         ans[i]=1;
14     for(j=0;j<=num_2;j+=2)//头两个括号相乘
15         for(k=0;k<=num_1;k++)
16             sup[j+k]+=ans[k];
17     memset(ans,0,sizeof(ans));    //ans置零
18     for(j=0;j<=num_5;j+=5)    //上一步结果*第3个括号
19         for(k=0;k<=num_1+num_2;k++)
20             ans[j+k]+=sup[k];
21     for(i=1;i<=N;i++)    //搜索
22         if(ans[i]==0)    return i;
23 }
24 int main()
25 {
26     while(scanf("%d%d%d",&num_1,&num_2,&num_5))
27     {
28         if(num_1==0&&num_2==0&&num_5==0)    return 0;    //结束
29         if(num_1==0){printf("1\n");continue;}
30         tar=cal_and_search();
31         printf("%d\n",tar);
32     }
33     return 0;
34 }

1085

时间: 2024-11-04 04:47:12

HDU 1085 Holding Bin-Laden Captive! 活捉本拉登(AC代码)普通型母函数的相关文章

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

http://acm.hdu.edu.cn/showproblem.php?pid=1085 题意:1元,2元,5元的硬币分别有num[1],num[2],num[3]个.问用这些硬币不能组合成的最小钱数. 继续母函数. 有两个注意的地方: 对c2[]初始化的同时也要对c1[]初始化. 最后枚举到sum+1,因为存在[1,sum]都可以凑成的可能,这时输出sum+1. #include <stdio.h> #include <iostream> #include <map&g

HDOJ/HDU 1085 Holding Bin-Laden Captive!(非母函数求解)

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 hi

HDU 1085 Holding Bin-Laden Captive!

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 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

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): 16063    Accepted Submission(s): 7206 Problem Description We all know that Bin-Laden is a notorious terrorist, and he

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): 17653    Accepted Submission(s): 7902 Problem Description We all know that Bin-Laden is a notorious terrorist, and he h

HDU 1085 Holding Bin-Laden Captive!(母函数或背包DP)

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 23245    Accepted Submission(s): 10350 Problem Description We all know that Bin-Laden is a notorious terrorist, and he

HDU 1085 Holding Bin-Laden Captive!(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085 解题报告:有1,2,5三种面值的硬币,这三种硬币的数量分别是num_1,num_2,num_5,问你不能凑的钱的最小值是多少. DP,开一个这样的数组dp[i][3],然后dp[i][0]表示凑成 i 元钱需要dp[i][0]张1块的,需要dp[i][1]张两块的,dp[i][2]张5块的,然后依次往后递推,得到 i 的途径一共有三种,第一种是i-1加一张一块的,第二种是i-2加上1张两块的,

HDU 1028 Ignatius and the Princess III伊格和公主III(AC代码)母函数

本题听说可用递推.DP等方法来做,但是此题是母函数的入门经典喔~所以我用了母函数 1 #include <iostream> 2 #define N 120 3 using namespace std; 4 int ans[N+1],sup[N+1];//ans保存答案,sup保存临时值 5 void main() 6 { 7 int num=0,i,j,k; 8 for(i=0;i<N+1;i++) //全部初始化为1 9 ans[i]=1; 10 for(i=2;i<=N;i+

HDU 1085

想来想去都有bug 最后简单粗暴的一个一个来处理了 #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #def