Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = xxx,     x3 = x2xx,     x4 = x3xx,     ...  ,     x31 = x30xx.

The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute  x31 with eight multiplications:

x2 = xxx,     x3 = x2xx,     x6 = x3xx3,     x7 = x6xx,     x14 = x7xx7
x15 = x14xx,     x30 = x15xx15,     x31 = x30xx.

This is not the shortest sequence of multiplications to compute  x31. There are many ways with only seven multiplications. The following is one of them:

x2 = xxx,     x4 = x2xx2,     x8 = x4xx4,     x10 = x8xx2
x20 = x10xx10,     x30 = x20xx10,     x31 = x30xx.

There however is no way to compute  x31 with fewer multiplications. Thus this is one of the most efficient ways to compute  x31 only by multiplications.

If division is also available, we can find a shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):

x2 = xxx,     x4 = x2xx2,     x8 = x4xx4,     x16 = x8xx8,     x32 = x16xx16,     x31 = x32 ÷ x.

This is one of the most efficient ways to compute  x31 if a division is as fast as a multiplication.

Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence of operations should be x to a positive integer's power. In other words, x-3, for example, should never appear.

Input

The input is a sequence of one or more lines each containing a single integer nn is positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output

Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.

Sample Input

1
31
70
91
473
512
811
953
0

Sample Output

0
6
8
9
11
9
13
12 这道题用到回溯法,不过要进行剪枝,最有效的剪枝在于预判,所以不要试图去深搜一次来得到最小步数,这样剪枝根本无法进行,可以去枚举尝试,从一到。。。,每次的这个i步作为约束条件,即判断,i步能否到达,因为深搜的结构是一颗解答树,你可以从当前步数获得将来一定不能到达的条件,如,当前步为step,我要求dp步到达,那么还剩下dp-step步,那么最后那一步我可以到达的最大数为当前数乘以pow(2,dp-ste)
,如果这一步的数还小于我n,那么就肯定无法到达了。。。。。。。 本题非原创,完全是参考别人的。。。。。。。
#include"iostream"
#include"stdio.h"
#include"cstring"
#include"algorithm"
using namespace std; int n;
int ans=1000000;
int a[1000];
int dp; int DFS(int step,int x)
{
if(a[step]==n) return 1; if(step>=dp) return 0; x=max(x,a[step]); if(x*(1<<(dp-step))<n) return 0; for(int i=0;i<=step;i++)
{
a[step+1]=a[step]+a[i]; if(DFS(step+1,x)) return 1; if(a[step]>a[i]) a[step+1]=a[step]-a[i];
else a[step+1]=a[i]-a[step]; if(DFS(step+1,x)) return 1;
} return 0;
}
int main()
{
while(cin>>n&&n)
{
a[0]=1;
if(n==1) cout<<0<<endl;
else
{
for(dp=1;;dp++)
{
if(DFS(0,1)) break;
}
cout<<dp<<endl;
}
}
return 0;
}

第二周习题F的更多相关文章

  1. oracle直通车第二周习题

    1.教材第二章课后作业 1,2,3,4题. 答:1. 创建一查询,显示与Blake在同一部门工作的雇员的项目和受雇日期,但是Blake不包含在内. 2. 显示位置在Dallas的部门内的雇员姓名.变化 ...

  2. 第二周习题O题

    Description   Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an or ...

  3. 20145213《Java程序设计》第二周学习总结

    20145213<Java程序设计>第二周学习总结 教材学习内容总结 本周娄老师给的任务是学习教材的第三章--基础语法.其实我觉得还蛮轻松的,因为在翻开厚重的书本,一股熟悉的气息扑面而来, ...

  4. 2018-2019-3《Java程序设计》第二周学习总结

    学号20175329 2018-2019-3<Java程序设计>第二周学习总结 教材学习内容总结      第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨 ...

  5. # 20175329 2018-2019-2 《Java程序设计》 第二周学习总结

    学号 2018-2019-3<Java程序设计>第二周学习总结 教材学习内容总结      第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨论一下JAVA与 ...

  6. # 20175329 2018-2019-2 《Java程序设计》第二周学习总结

    # 学号 2018-2019-3<Java程序设计>第三周学习总结 ## 教材学习内容总结 第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨论一下JAVA与 ...

  7. 2019-2020-1 20199329《Linux内核原理与分析》第二周作业

    <Linux内核原理与分析>第二周作业 一.上周问题总结: 未能及时整理笔记 Linux还需要多用 markdown格式不熟练 发布博客时间超过规定期限 二.本周学习内容: <庖丁解 ...

  8. python课程第二周重点记录

    python课程第二周重点记录 1.元组的元素不可被修改,元组的元素的元素可以被修改(字典在元组中,字典的值可以被修改) 2.个人感觉方便做加密解密 3.一些方法的使用 sb = "name ...

  9. 20145304 刘钦令 Java程序设计第二周学习总结

    20145304 <Java程序设计>第2周学习总结 教材学习内容总结 java可区分基本类型和类类型(即参考类型)两大类型系统. 基本类型主要可区分为整数.字节.浮点数.字符与布尔. 整 ...

随机推荐

  1. Luogu P1137 旅行计划 【拓扑排序+Dp】By cellur925

    题目传送门 由于满足游览先后顺序从西到东的性质,我们很自然的想到用拓扑排序处理出一个合理的游览顺序. 然鹅,之后呢? 事实上,拓扑排序常与Dp相结合,解决后效性.我们就可以在每次拓扑入队的时候更新答案 ...

  2. 爬虫—Requests高级用法

    Requests高级用法 1.文件上传 我们知道requests可以模拟提交一些数据.假如有的网站需要上传文件,我们也可以用requests来实现. import requests files = { ...

  3. DecimalFormat数字格式化用法“0”和“#”的区别

    1. 以“0”补位时: 如果数字少了,就会补“0”,小数和整数都会补: 如果数字多了,就切掉,但只切小数的末尾,整数不能切: 同时被切掉的小数位会进行四舍五入处理. 2. 以“#”补位时: 如果数字少 ...

  4. 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

  5. 彩色模型 分类: 图像处理 Matlab 2015-01-08 20:43 364人阅读 评论(0) 收藏

    彩色模型(又称彩色空间或彩色系统)是描述色彩的一种方法,本质上,彩色模型就是坐标系统和子空间的规范,系统中的每种颜色由单个点来表示.下面介绍两种最常用的彩色模型. 一.RGB彩色模型: RGB模型是最 ...

  6. NSUserDefaults保存用户名和密码

    #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...

  7. WPF学习08:MVVM 预备知识之COMMAND

    WPF内建的COMMAND是GOF 提出的23种设计模式中,命令模式的实现. 本文是WPF学习07:MVVM 预备知识之数据绑定的后续,将说明实现COMMAND的三个重点:ICommand  Comm ...

  8. 51全志R58平台Android4.4下Camera的HAL层修改

    51全志R58平台Android4.4下Camera的HAL层修改 2018/11/7 15:20 版本:V1.0 开发板:SC5806 1.系统编译: (略) 2.全志R58平台Android4.4 ...

  9. JData 整合ArtTemplate的前端框架

    因为项目需要和自己的兴趣,几个月前结合模板解析神速的ArtTemplate,自己写了个框架取名JData,多多指教啊---因为一直没时间写文档,为了能够更方便地使用和避免我把代码忘了,今天抽空把文档写 ...

  10. VUE 入坑系列 一 基础语法

    html代码 <div id="app"> {{message}} </div> JavaScript代码 var vm = new Vue({ el: & ...