POJ 2248

An addition chain for n is an integer sequence <a0, a1,a2,...,am="">with the following four properties: 
  • a0 = 1 
  • am = n 
  • a0 < a1 < a2 < ... < am-1 < am 
  • For each k (1<=k<=m) there exist two (not necessarily different) integers i and j (0<=i, j<=k-1) with ak=ai+aj

You are given an integer n. Your job is to construct an addition chain for n with minimal length. If there is more than one such sequence, any one is acceptable. 
For example, <1,2,3,5> and <1,2,4,5> are both valid solutions when you are asked for an addition chain for 5.

Input

The input will contain one or more test cases. Each test case consists of one line containing one integer n (1<=n<=100). Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line containing the required integer sequence. Separate the numbers by one blank. 
Hint: The problem is a little time-critical, so use proper break conditions where necessary to reduce the search space. 

Sample Input

5
7
12
15
77
0

Sample Output

1 2 4 5
1 2 4 6 7
1 2 4 8 12
1 2 4 5 10 15
1 2 4 8 9 17 34 68 77
 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <stack>
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <list>
16 #include <iomanip>
17 #include <cctype>
18 #include <cassert>
19 #include <bitset>
20 #include <ctime>
21 
22 using namespace std;
23 
24 #define pau system("pause")
25 #define ll long long
26 #define pii pair<int, int>
27 #define pb push_back
28 #define pli pair<ll, int>
29 #define pil pair<int, ll>
30 #define clr(a, x) memset(a, x, sizeof(a))
31 
32 const double pi = acos(-1.0);
33 const int INF = 0x3f3f3f3f;
34 const int MOD = 1e9 + 7;
35 const double EPS = 1e-9;
36 
37 /*
38 #include <ext/pb_ds/assoc_container.hpp>
39 #include <ext/pb_ds/tree_policy.hpp>
40 using namespace __gnu_pbds;
41 #define TREE tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update>
42 TREE T;
43 */
44 
45 
46 int n, num[27];
47 bool dfs(int dep, int k) {
48     if (n == num[k]) {
49         for (int i = 1; i < k; ++i) {
50             printf("%d ", num[i]);
51         }
52         printf("%d\n", num[k]);
53         return true;
54     }
55     for (int j = 1; j <= k; ++j) {
56         num[k + 1] = num[k] + num[j];
57         if (num[k + 1] > n || k + 1 > dep) continue;
58         if (dfs(dep, k + 1)) return true;
59     }
60     return false;
61 }
62 int main() {
63     num[1] = 1;
64     while (scanf("%d", &n) && n) {
65         for (int dep = 1; ; ++dep) {
66             if (dfs(dep, 1)) break;
67         }
68     }
69     return 0;
70 }
View Code

解法:迭代加深搜索,扩展可等加成每次由最后一个和前面的相加。

转载于:https://www.cnblogs.com/BIGTOM/p/9825903.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值