Milk Measuring
Hal Burch

Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest milk and deliver it in one big bottle to a customer. He fills that bottle with exactly the number of quarts that the customer orders.

Farmer John has always been frugal. He is at the cow hardware store where he must purchase a set of pails with which to measure out Q quarts of milk from his giant milk tank. Since the pails each cost the same amount, your task is to figure out a minimal set of pails Farmer John can purchase in order to fill a bottle with exactly Q quarts of milk. Additionally, since Farmer John has to carry the pails home, given two minimal sets of pails he should choose the "smaller" one as follows: Sort the sets in ascending order. Compare the first pail in each set and choose the set with the smallest pail. If the first pails match, compare the second pails and choose from among those, else continue until the two sets differ. Thus the set {3, 5, 7, 100} should be chosen over {3, 6, 7, 8}.

To measure out milk, FJ may completely fill a pail from the tank and pour it into the bottle. He can never remove milk from the bottle or pour milk anywhere except into the bottle. With a one-quart pail, FJ would need only one pail to create any number of quarts in a bottle. Other pail combinations are not so convenient.

Determine the optimally small number of pails to purchase, given the guarantee that at least one solution is possible for all contest input data.

PROGRAM NAME: milk4

INPUT FORMAT

Line 1: The single integer Q
Line 2: A single integer P (1 <= P <= 100) which is the number of pails in the store
Lines 3..P+2: Each line contains a single integer pail_value (1 <= pail_value <= 10000), the number of quarts a pail holds

SAMPLE INPUT (file milk4.in)

16
3
3
5
7

OUTPUT FORMAT

The output is a single line of space separated integers that contains:

  • the minimum number of pails required to measure out the desired number of quarts, followed by:
  • a sorted list (from smallest to largest) of the capacity of each of the required pails

SAMPLE OUTPUT (file milk4.out)

2 3 5

——————————————————————————————————————————————————————题解
这道题是迭代深搜,但是以为是个记录状态的背包,死活没写出来
这道题记忆化搜索判断可行比纯dp要快,因为要用的状态都是跳跃幅度较大的,记忆化搜索反而用的状态比较少
思路和ax+by=1方程解然后不断得到新的差值为1的序列如果这个序列长度==最小的数,那么就回得到后面所有的数
http://www.cnblogs.com/ivorysi/p/6279637.html
所以这道题的个数不会太多,用DFSID+记搜
 /*
ID: ivorysi
LANG: C++
PROG: milk4
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
//#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int q,p;
int f[];
int ld,used[],w[];
int flag=,cnt;
bool calc(int tmp) {
if(f[tmp]!=-) return f[tmp];
f[tmp]=;
siji(i,,p) {
if(used[i] && tmp%w[i]==) return f[tmp]=;
}
siji(i,,p) {
if(used[i] && tmp>=w[i]) {
f[tmp]=(f[tmp] || calc(tmp-w[i]));
if(f[tmp]==) return f[tmp];
}
}
return f[tmp];
}
void dfs(int d,int pr) {
if(flag) return;
if(d==ld){
siji(i,,q) f[i]=-;
f[]=;
if(calc(q)) {
flag=;
printf("%d ",ld);
siji(i,,p) {
if(used[i]) {
++cnt;
printf("%d%c",w[i]," \n"[cnt==ld]);
}
}
}
return;
}
siji(i,pr+,p) {
used[i]=;
dfs(d+,i);
if(flag) return;
used[i]=;
}
}
void solve() {
scanf("%d%d",&q,&p);
siji(i,,p) {
scanf("%d",&w[i]);
}
sort(w+,w+p+);
f[]=;
siji(i,,p) {
ld=i;
dfs(,);
if(flag) break;
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("milk4.in","r",stdin);
freopen("milk4.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
 

最新文章

  1. centos 安装依赖错误
  2. jquery 设置焦点
  3. 添加Bootstrap 到MVC项目中(vs2010)
  4. 让层遮挡select(ie6下的问题)
  5. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)【转】
  6. RASP 完爆 WAF 的5大理由!
  7. js的引用顺序
  8. Java调用R(一)_Rserve
  9. 如何分析apache日志[access_log(访问日志)和error_log(错误日志)]
  10. zookeeper watch 节点
  11. new关键字
  12. vue零基础学习--搭建项目
  13. 焦点轮播图(tab轮播)
  14. SpringCloud学习笔记:SpringCloud简介(1)
  15. pandas(三)
  16. (set)MG loves gold hdu6019
  17. as2 shareObject本地缓存存储位置:
  18. iOS用全局宏的概念理解xcode中的设置 preprocessor&#160;macros
  19. SQL Server查询已锁的表及解锁
  20. SQL查询入门(下篇)

热门文章

  1. Linux入门,这七大习惯得有!
  2. Shell记录-Shell命令(定时任务)
  3. mongodb3.6集群搭建:分片集群认证
  4. ConcurrentHashMap 产生NullPointerException
  5. 【转】一个简单的WCF回调实例
  6. 利用反射型XSS二次注入绕过CSP form-action限制
  7. 7 SQL优化技术
  8. elasticsearch-head插件安装的一些坑!es6.5.4版本
  9. [MySQL FAQ]系列 — EXPLAIN结果中哪些信息要引起关注
  10. linq和ef关于group by取最大值的两种写法