Stamps 

The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number of stamps that may be attached to it. The government wishes to know how many different stamps, and of what values, they need to print to allow the widest choice of values to be made up under these conditions. Stamps are always valued in units of $1.

This has been analysed by government mathematicians who have derived a formula for n(h,k), where h is the number of stamps that may be attached to a document, k is the number of denominations of stamps available, and n is the largest attainable value in a continuous sequence starting from $1. For instance, if h=3, k=2 and the denominations are $1 and $4, we can make all the values from $1 to $6 (as well as $8, $9 and $12). However with the same values of h and k, but using $1 and $3 stamps we can make all the values from $1 to $7 (as well as $9). This is maximal, so n(3,2) = 7.

Unfortunately the formula relating n(h,k) to hk and the values of the stamps has been lost--it was published in one of the government reports but no-one can remember which one, and of the three researchers who started to search for the formula, two died of boredom and the third took a job as a lighthouse keeper because it provided more social stimulation.

The task has now been passed on to you. You doubt the existence of a formula in the first place so you decide to write a program that, for given values of h and k, will determine an optimum set of stamps and the value of n(h,k).

Input

Input will consist of several lines, each containing a value for h and k. The file will be terminated by two zeroes (0 0). For technical reasons the sum of h and k is limited to 9. (The President lost his little finger in a shooting accident and cannot count past 9).

Output

Output will consist of a line for each value of h and k consisting of the k stamp values in ascending order right justified in fields 3 characters wide, followed by a space and an arrow (->) and the value of n(h,k) right justified in a field 3 characters wide.

Sample input

3 2
0 0

Sample output

  1  3 ->  7

题意:不好理解啊。。 还是看了别人题解上的题意才懂的。。给定h和k,h代表拥有的邮票张数,k代表有几种面值的邮票。。现在你要确定k个面值。来确保能组合成的连续邮票价值可以到最大。。不是很好理解

举下样例3 和 2.当邮票面值为1 3时。可以组成1 2 3 4 5 6 7 9.连续价值最大到7.所以输出7。假如取1 4.那么组成的为

1 2 3 4 5 6 8 9 12 连续最大到6.要输出较大,所以输出是 1 3-> 7。

思路:从题目中了看出必然有一个面值为1,不然1就组不成了。然后就可以以这个1作为起点。进行2层深搜。一层搜索当前价值能不能被组合成。。一层搜索当价值不能被组合成就添加一种面值。添加的面值区间为[当前能组成的最小面值+1,当前能组成的最大面值+1]。把这几种情况进行搜索。。知道面值数为k结束。

#include <stdio.h>
#include <string.h> int h, k;
int m[10];
int out[10];
int mnum;
int jud;
int maxx; void judge(int num, int mian, int money)
{
if (jud == 1)
return;
if (mian == money)
{
jud = 1;
return;
}
if (num == h)
return;
for (int i = 0; i < mnum; i ++)
{
judge(num + 1, mian + m[i], money);
}
} void f(int num, int money)
{
if (num == k)
{
if (maxx < money)
{
maxx = money;
for (int i = 0; i < k; i ++)
{
out[i] = m[i];
}
}
return;
}
jud = 0;
judge(0, 0, money);
if (jud)
{
f(num, money + 1);
}
else
{
for (int i = 2; i <= money; i ++)
{
m[mnum] = i;
mnum ++;
f(num + 1, money);
mnum --;
}
}
}
int main()
{
while (scanf("%d%d", &h, &k) != EOF && h && k)
{
memset(out, 0, sizeof(out));
memset(m, 0, sizeof(m));
maxx = 0;
m[0] = 1;
mnum = 1;
f(0, 1);
for (int i = 0; i < k; i ++)
printf("%3d", out[i]);
printf(" ->%3d\n", maxx - 1);
}
return 0;
}

UVA 165 Stamps (DFS深搜回溯)的更多相关文章

  1. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

    题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...

  2. HDU5723 Abandoned country (最小生成树+深搜回溯法)

    Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...

  3. DFS深搜——Red and Black——A Knight&#39;s Journey

    深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...

  4. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

  5. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  6. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  7. ****Curling 2.0(深搜+回溯)

    Curling 2.0 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  8. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  9. Red and Black(DFS深搜实现)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

随机推荐

  1. FPGA In/Out Delay Timing Constaint

    先简单说说这段时间遇到的问题.FPGA采集前端scaler的视频数据.像素时钟(随路时钟),视频数据,行场同步,DE.这些信号进入FPGA后.通过CSC(颜色空间转换).输出后的图像有噪点.通过查看时 ...

  2. nc工具学习

    0x00.命令详解 基本使用 想要连接到某处:nc  [-options] ip port 绑定端口等待连接:nc -l -p port ip 参数: -e prog 程序重定向,一旦连接,就执行 [ ...

  3. [leetcode DP]63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. 怎么处理stdClass::__set_state

    处理后 处理方法 function object2array_pre(&$object) { if (is_object($object)) { $arr = (array)($object) ...

  5. QT学习笔记5:QMouseEvent鼠标事件简介

    一.QMouseEvent的详细描述 首先请注意,Qt中的QMouseEvent一般只涉及鼠标左键或右键的单击.释放等操作,而对鼠标滚轮的响应则通过QWheeEvent来处理. QMouseEvent ...

  6. Java设计模式GOF之工厂模式

    一.工厂模式(Factory) 1.实现了创建者和调用者的分离 2.应用场景 ①JDK中 Calendar 的 getInstance(): ②JDBC 的 Connection 对象的获取: ③Hi ...

  7. [BZOJ4881][Lydsy1705月赛]线段游戏

    首先冷静一下看清问题的本质,是将整个数列分成两个递增子序列. 那么由Dilworth定理得,无解当且仅当数列的最长下降子序列的长度>2,先特判掉. 然后就有一些比较厉害的做法:http://ww ...

  8. 方程式0day图形化利用工具

    最近方程式的漏洞着实活了一把,分析了下githup上面的文件目录,找到了利用文件,主要是针对windows主机的SMB.RDP协议进行攻击,因为我主要根据他们提供的payload的程序,利用这两个模块 ...

  9. 【洛谷】3469:[POI2008]BLO-Blockade【割点统计size】

    P3469 [POI2008]BLO-Blockade 题意翻译 在Byteotia有n个城镇. 一些城镇之间由无向边连接. 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些).每 ...

  10. 【BZOJ】2760: [JLOI2011]小A的烦恼【字符串模拟】

    2760: [JLOI2011]小A的烦恼 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 406  Solved: 258[Submit][Statu ...