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. 1
  2. 31
  3. 70
  4. 91
  5. 473
  6. 512
  7. 811
  8. 953
  9. 0

Sample Output

  1. 0
  2. 6
  3. 8
  4. 9
  5. 11
  6. 9
  7. 13
  8. 12
  9.  
  10. 这道题用到回溯法,不过要进行剪枝,最有效的剪枝在于预判,所以不要试图去深搜一次来得到最小步数,这样剪枝根本无法进行,可以去枚举尝试,从一到。。。,每次的这个i步作为约束条件,即判断,i步能否到达,因为深搜的结构是一颗解答树,你可以从当前步数获得将来一定不能到达的条件,如,当前步为step,我要求dp步到达,那么还剩下dp-step步,那么最后那一步我可以到达的最大数为当前数乘以pow2dp-ste
    ,如果这一步的数还小于我n,那么就肯定无法到达了。。。。。。。
  11.  
  12. 本题非原创,完全是参考别人的。。。。。。。
  1. #include"iostream"
  2. #include"stdio.h"
  3. #include"cstring"
  4. #include"algorithm"
  5. using namespace std;
  6.  
  7. int n;
  8. int ans=1000000;
  9. int a[1000];
  10. int dp;
  11.  
  12. int DFS(int step,int x)
  13. {
  14. if(a[step]==n) return 1;
  15.  
  16. if(step>=dp) return 0;
  17.  
  18. x=max(x,a[step]);
  19.  
  20. if(x*(1<<(dp-step))<n) return 0;
  21.  
  22. for(int i=0;i<=step;i++)
  23. {
  24. a[step+1]=a[step]+a[i];
  25.  
  26. if(DFS(step+1,x)) return 1;
  27.  
  28. if(a[step]>a[i]) a[step+1]=a[step]-a[i];
  29. else a[step+1]=a[i]-a[step];
  30.  
  31. if(DFS(step+1,x)) return 1;
  32. }
  33.  
  34. return 0;
  35. }
  36. int main()
  37. {
  38. while(cin>>n&&n)
  39. {
  40. a[0]=1;
  41. if(n==1) cout<<0<<endl;
  42. else
  43. {
  44. for(dp=1;;dp++)
  45. {
  46. if(DFS(0,1)) break;
  47. }
  48. cout<<dp<<endl;
  49. }
  50. }
  51. return 0;
  52. }

第二周习题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. 组合数学练习题(二)——Chemist

    题意: 在一个 n 维无限空间中,一开始原点处有一个细胞.细胞每秒都会增殖,每个原有细胞都会消亡,在与它曼哈顿距离恰为 1的所有位置都会新增一个细胞.求 T 秒后,原点处会有多少细胞,答案 mod10 ...

  2. jQuery笔记之animate中的queue

    队列 队列的执行顺序 queue() dequeue() 输出对象里面的内容 依次出队 不过这样写太麻烦了,因为每次都要输出,所以我们看下面的方法 运用到队列输出的 <!DOCTYPE html ...

  3. tableView 顶部多出一部分解决方法

    1.self.automaticallyAdjustsScrollViewInsets = NO; 此方法解决之后 不能保证tableView 能彻底的滑动到底部 2. self.edgesForEx ...

  4. [CF1109F]Sasha and Algorithm of Silence's Sounds

    题意 有一个\(n*m\)的网格,每个格子有一个数,为\(1\)~\(n * m\)的排列 一个区间\((1<=l<=r<=n*m)\)是好的,当且仅当:数值在该区间内的格子,构成一 ...

  5. [ZOJ1140]Courses 课程

    Description 给出课程的总数P(1<=p<100),学生的总数N(1<=N<=300) 每个学生可能选了一门课程,也有可能多门,也有可能没有. 要求选出P个学生来组成 ...

  6. QT5每日一学(二)编写QT多窗口程序

    一.添加主窗口 1.首先打开Qt Creator,新建Qt Widgets Application,项目名称设置为windows,在类信息界面保持基类为QMainWindow.类名为MainWindo ...

  7. Android开机自启动

    1.原理 当Android启动时,会发出一个系统广播,内容为ACTION_BOOT_COMPLETED,它的字符串常量表示为 android.intent.action.BOOT_COMPLETED. ...

  8. Python variable 作用域和初始化

    Python 根据LEGB rule在不同的namespace中找变量 在def的函数中对global 变量做修改还是不推荐的,应该将其作为参数传入函数 try: do_something() cnt ...

  9. 199 Binary Tree Right Side View 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树,   1            <--- /   \2     3         <--- \  ...

  10. linux下常用网络操作汇总 专题

    centos 更改主机名,需要更改的几个地方: (1) /etc/sysconfig/network  //更改主机名(2)/etc/hostname  //更改主机名(3) /etc/hosts   ...