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

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

  • 相关阅读:
    linux Chrome 安装
    jQuery.extend 函数详解
    Ubuntu Linux 下文件名乱码(无效的编码)的快速解决办法
    Django——model字段类型 2
    Django 中的用户认证
    Expression<Func<T, bool>>与Func<T, bool>的区别
    根据funID,personID获取最新规划包项目相关信息
    SET ANSI_NULLS ON 在T-SQL中是什么意思
    SQL Server 2008中如何为XML字段建立索引
    PowerDesigner 建模后如何导入到数据库
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/9825903.html
Copyright © 2011-2022 走看看