博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第二周习题F
阅读量:7072 次
发布时间:2019-06-28

本文共 2669 字,大约阅读时间需要 8 分钟。

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.

 

 

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.

 

 

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.

 

 

 

13170914735128119530

 

 

 

06891191312 这道题用到回溯法,不过要进行剪枝,最有效的剪枝在于预判,所以不要试图去深搜一次来得到最小步数,这样剪枝根本无法进行,可以去枚举尝试,从一到。。。,每次的这个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))
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<

 

转载于:https://www.cnblogs.com/zsyacm666666/p/4692971.html

你可能感兴趣的文章
java实现杨辉三角
查看>>
StoreFront配置本地安装Receiver客户端
查看>>
svcs所看到的服务状态说明
查看>>
如何调试GlusterFS?
查看>>
36. Valid Sudoku
查看>>
【入门】C程序设计(第三版)谭浩强—第一章
查看>>
WdOS源码编译安装MySQL 5.5.25a
查看>>
Linux性能监测
查看>>
Excel自动生成工资条格式
查看>>
linux java环境变量设置
查看>>
Git 处理分支冲突 rebase
查看>>
Java设计模式之工厂模式
查看>>
测试经理能力要求
查看>>
linux修改用户密码的问题
查看>>
组策略应用之一:映射网络驱动器
查看>>
java第四次作业
查看>>
Dynamics CRM 请求服务时报access is denied错误
查看>>
Oracle in与exists语句
查看>>
我的友情链接
查看>>
通过文件句柄获取文件的路径
查看>>