zoukankan      html  css  js  c++  java
  • 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
    ", 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

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

  • 相关阅读:
    HSL制作配色表
    css3 text-overflow制作固定区域的博客列表
    git 安装
    mailto: HTML e-mail 链接
    Sublime Text2 新建文件快速生成Html头部信息和炫酷的代码补全
    Git教程
    javscript 一些常用的工具方法
    LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
    Dictionary and KeyValuePair关系
    C# 类动态添加属性、方法
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/9825903.html
Copyright © 2011-2022 走看看