zoukankan      html  css  js  c++  java
  • POJ2248 Addition Chains

     
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5122   Accepted: 2766   Special Judge

    Description

    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
    

    Source

     
     
    DFS迭代加深。
    每次优先取数列后面的大数凑成新数,如果数过小或者大于目标就剪掉。
     
     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mxn=300;
     9 int n;
    10 int a[mxn];
    11 int dep[mxn];
    12 bool v[mxn];
    13 bool dfs(int now,int limit){
    14     if(now>limit)return 0;
    15     int i,j;
    16     for(i=now-1;i;i--){
    17         for(j=i;j;j--){
    18             a[now]=a[i]+a[j];
    19             if(a[now]>n)continue;
    20             if(a[now]<=a[now-1])break;
    21             if(a[now]==n) return 1; 
    22             if(dfs(now+1,limit))return 1;
    23         }
    24     }
    25     return 0;
    26 }
    27 int main(){
    28     a[1]=1;
    29     a[2]=2;
    30     while(scanf("%d",&n) && n){
    31         if(n==1){
    32             printf("1 
    ");
    33             continue;
    34         }
    35         if(n==2){
    36             printf("1 2 
    ");
    37             continue;
    38         }
    39         memset(v,0,sizeof v);
    40         int i,j;
    41         int ans=1000;
    42         for(i=1;i<=100;i++){
    43             if(dfs(3,i)){
    44                 ans=i;
    45                 for(j=1;j<=i;j++)printf("%d ",a[j]);
    46                 printf("
    ");
    47                 break;    
    48             }
    49         }
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    Python随笔之字典Dict
    Python随笔之列表List
    Python基础整理,懒得分类了,大家对付看看吧
    shell批量推送公钥脚本
    Xcode 切换target 之后打包失败
    百度导航sdk错误日志
    前端项目中去掉替换双引号
    taro3.x项目中引用taro-ui以及taro-iconfont-cli
    taro
    JS中some() every() forEach() map() filter()的区别
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5850288.html
Copyright © 2011-2022 走看看