uva 529 Addition Chains

来源:互联网 发布:数控机床的编程特点 编辑:程序博客网 时间:2024/06/05 05:38

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=470

题目描述:



  Addition Chains 

An addition chain for n is an integer sequence $<a_0, a_1, a_2, \dots, a_m>$ with the following four properties:

  • a0 = 1
  • am = n
  • a0<a1<a2<...<am-1<am
  • For each k ( $1 \le k \le m$) there exist two (not neccessarily different) integers i and j ($0 \le i, j \le 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 Specification 

The input file will contain one or more test cases. Each test case consists of one line containing one integer n ( $1 \le n \le 10000$). Input is terminated by a value of zero (0) for n.

Output Specification 

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 

571215770

Sample Output 

1 2 4 51 2 4 6 71 2 4 8 121 2 4 5 10 151 2 4 8 9 17 34 68 77
题意:

a[0]=1 a[m]=n,a序列为严格递增序列, 对于任意的下标k在[1,m]内 都存在 1 <= j <= i <= k-1  使得 a[i]+a[j]=a[k],找出m最小的符合条件的序列,即长度最小的序列。

题解:

迭代深搜+剪枝

刚开始仅仅是DFS+剪枝,结果怎么剪都不行,过个300的数据就卡半天。无奈解题报告,说是迭代深搜。

迭代深搜其实就是DFS的变种(变种特别特别多,灵活变种与组合),即是一种限定了搜索深度的DFS,然后利用已限定的深度可以做各种各样的剪枝和其他操作,而整体模式更像是BFS,该深度没有答案,则深度+1,就像是一层一层的广度搜索。其实带有BFS思想的DFS以前也是写过的,也可以说算是这么一种。

对于剪枝:

1、当前深度要填的数 肯定来自于它前几位中,较大的两位组成,且其和 要小于等于末端值n 又要大于前一位的值(序列严格递增)。即  a[k-1] < a[i]+a[j]  <= n

2、对于当前要填的值 temp,由于已经确定了深搜的深度,那么 我们按最大的取值(i,j都取前一位,即a[k-1]*2) 延伸到 最大深度时候的temp,如果其值都比n小(能取的最大值都比n小),意味着在这趟搜索中它永远不可能达到n,所以剪掉这支。

代码:

/*use IDS I have not any other way to solve it*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int a[1000]={1};//a[0]=1int n=0;int depth=0;int flag=0;//find the min length/*DFS the sequence place schedule*/int DFS(int len,int depth){if(flag) return(0);if(len==depth&&a[len-1]==n){flag=1;return(0);}else{int i=0,j=0;for(i=len-1;i>=0;i--){for(j=i;j>=0;j--){if(a[i]+a[j]<=n&&a[i]+a[j]>a[len-1])//prune,the sequence is strictly increasing{a[len]=a[i]+a[j];//prune,IDS pruneint temp=a[len];int k=0;for(k=len+1;k<=depth;k++){temp*=2;}if(temp<n){continue;}DFS(len+1,depth);if(flag) return(0);}}}}return(0);}/*for test*/int test(){//find the law or formulaint i=300;for(i=1;i<=300;i++){printf("%d\n",i );}return(0);}/*main process*/int MainProc(){while(scanf("%d",&n)!=EOF&&n>0){//inita[0]=1;depth=0;int temp=1;while(temp<n){depth++;temp*=2;}depth++;//at least depth,temp>=n so depth++,it at least (not must) be this area,not the tem<n area,depth is same as lenflag=0;while(!flag){DFS(1,depth);if(!flag){depth++;}}//outprintf("%d",a[0] );int i=0;for(i=1;i<=depth-1;i++){printf(" %d",a[i] );}printf("\n");}return(0);}int main(int argc, char const *argv[]){/* code *///test();MainProc();return 0;}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 儿子2周4个月了不说话怎么办 三岁的宝宝还不会数数怎么办 2岁宝宝晚上不睡觉不听话怎么办 8个月的狗狗怕人不敢出门怎么办 狗太怕人了怎么办都不敢出门 媳妇和婆婆晚上都不想带孩子怎么办 婆婆和媳妇带孩子观念不一样怎么办 婆婆给媳妇买了不喜欢的家具怎么办 断奶后宝宝对奶粉很抗拒怎么办 两岁宝宝断奶不喝奶粉怎么办 宝宝两岁了断奶后奶粉不吃怎么办 宝宝断奶两天了不愿意吃奶粉怎么办 一岁四个月宝宝断奶不喝奶粉怎么办 四个月宝宝断奶不喝奶粉怎么办 四个月宝宝断奶后不吃奶粉怎么办 2岁的宝宝不开口说话怎么办 一周岁宝宝断奶不喝奶粉怎么办 八个月宝宝断奶不喝奶粉怎么办 婆家人总是用心机对待娘家人怎么办 婆婆老是背后说我娘家人坏话怎么办 婆家姐带孩子住娘家不走怎么办? 老是想在娘家不想回婆家怎么办 娘家和婆家同时向我借钱怎么办 土地确权后娘家婆家都没有怎么办 结了婚婆家向娘家借钱怎么办? 婆婆的娘家人从我家住怎么办 八个月宝宝断奶后不吃奶粉怎么办 吃母乳的宝宝不吸奶嘴怎么办 宝宝吸了奶嘴不吸母乳怎么办 婴儿吃了奶嘴不吸母乳怎么办 十一个月宝宝断奶不喝奶粉怎么办 孩子三门成绩全不及格家长该怎么办 宝宝快十个月了还不会爬怎么办 小孩写字老把手向里扭曲怎么办 孩子该上四年级了数学差的很怎么办 孩子上三年级了数学成绩好差怎么办 三年级数学老考70-80分怎么办 叛逆期的孩子用死来威胁家长怎么办 叛逆期的孩子抽烟喝酒家长该怎么办 大学遇到不好的老师加课怎么办 两岁的宝宝脾气古怪不听话怎么办