uva 165 Stamps (回溯)

                                        uva 165 Stamps



Download as PDF

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 h, k 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(以(0,0)为样例终点)。h为可以贴的邮票上限,k为邮票的所给的面额的种类。要求用k种  1~h张邮票所能组成的最长连续序列(从一开始)。

解题思路:两层回溯,第一层找出k张邮票不同面额的搭配方式,第二层找出当前搭配方式所能组成的最长连续序列。


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int h, k, ans, temp, num[200], num2[200], vis[200], flag;

void DFS2(int sum, int result, int step) {  //检验当前不同面额能否组成result
	if (flag) return;
	if (sum == result) {
		flag = 1;
		return;
	}
	if (step == h) return;
	for (int i = 0; i < k; i++) {
		sum += num[i];
		DFS2(sum, result, step + 1);
		sum -= num[i];
	} 	
}

void DFS(int step) {
	if (step == k) {
		 temp = h;
		do {                    //找出当前面额情况所能组成的最长连续序列
			flag = 0;
			temp++;
			DFS2(0, temp, 0);
		} while (flag);

		if (temp - 1 > ans) {
			ans = temp - 1;
			for (int i = 0; i < k; i++) {
				num2[i] = num[i];
			}
		}
		return;
	}
	for (int i = num[step - 1]; i <= temp + 7; i++) {  //回溯找出不同的面额搭配方式
		if (!vis[i] && i > num[step - 1]) {
			vis[i] = 1;
			num[step] = i;
			DFS(step + 1);
			vis[i] = 0;
		}
	}
}

int main() {
	while (scanf("%d %d", &h, &k) == 2, h || k) {
		ans = 0;
		memset(vis, 0, sizeof(vis));
		memset(num, 0, sizeof(num));
		memset(num2, 0, sizeof(num2));
		flag = 0;
		num[0] = 1;
		vis[1] = 1;
		temp = h;

		DFS(1);

		for (int i = 0; i < k; i++) {   //注意输出格式
			printf("%3d", num2[i]);
		}
		printf(" ->%3d\n", ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值