Description

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

x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x.

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

x2 = x × x, x3 = x2 × x, x6 = x3 × x3, x7 = x6 × x, x14 = x7 × x7, x15 = x14 × x, x30 = x15 × x15, x31 = x30 × x.

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 = x × x, x4 = x2 × x2, x8 = x4 × x4, x8 = x4 × x4, x10 = x8 × x2, x20 = x10 × x10, x30 = x20 × x10, x31 = x30 × x.

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

x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x16 = x8 × x8, x32 = x16 × x16, 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 should be x to a positive integer’s power. In others words, x−, for example, should never appear.

Input

The input is a sequence of one or more lines each containing a single integer n. n is positive and less than or equal to . 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


Sample Output


Source

求只用乘法和除法最快多少步可以求到x^n

其实答案最大13,但由于树的分支极为庞大在IDDFS的同时,我们还要加2个剪枝

1 如果当前序列最大值m*2^(dep-k)<n则减去这个分支

2 如果出现两个大于n的数则要减去分支。因为里面只有一个有用,我们一定可以通过另外更加短的路径得到答案

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int num;
int way[];
bool dfs(int n,int step){
if(num>step) return false;
if(way[num]==n) return true;
if(way[num]<<(step-num)<n) return false;//强剪枝
for(int i=;i<=num;i++){
num++;
way[num]=way[num-]+way[i];
if(way[num]<= && dfs(n,step)) return true; way[num]=way[num-]-way[i];
if(way[num]> && dfs(n,step)) return true;
num--;
}
return false;
}
int main()
{
int n;
while(scanf("%d",&n)==){
if(n==){
break;
} //迭代加深dfs
int i;
for(i=;;i++){
way[num=]=;
if(dfs(n,i))
break;
}
printf("%d\n",i); }
return ;
}

poj 3134 Power Calculus(迭代加深dfs+强剪枝)的更多相关文章

  1. POJ 3134 Power Calculus (迭代剪枝搜索)

    题目大意:略 题目里所有的运算都是幂运算,所以转化成指数的加减 由于搜索层数不会超过$2*log$层,所以用一个栈存储哪些数已经被组合出来了,不必暴力枚举哪些数已经被搜出来了 然后跑$iddfs$就行 ...

  2. POJ 2248 - Addition Chains - [迭代加深DFS]

    题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...

  3. POJ-3134-Power Calculus(迭代加深DFS)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  4. POJ 3134 Power Calculus ID-DFS +剪枝

    题意:给你个数n 让你求从x出发用乘除法最少多少步算出x^n. 思路: 一看数据范围 n<=1000 好了,,暴搜.. 但是 一开始写的辣鸡暴搜 样例只能过一半.. 大数据跑了10分钟才跑出来. ...

  5. POJ 3134 - Power Calculus

    迭代加深 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<al ...

  6. POJ 3134 - Power Calculus (IDDFS)

    题意:求仅仅用乘法和除法最快多少步能够求到x^n 思路:迭代加深搜索 //Accepted 164K 1094MS C++ 840B include<cstdio> #include< ...

  7. 迭代加深搜索POJ 3134 Power Calculus

    题意:输入正整数n(1<=n<=1000),问最少需要几次乘除法可以从x得到x的n次方,计算过程中x的指数要求是正的. 题解:这道题,他的结果是由1经过n次加减得到的,所以最先想到的就是暴 ...

  8. poj 3134 Power Calculus(IDA*)

    题目大意: 用最小的步数算出  x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A*   就是如果这个数一直平方  所需要的步骤数都不能达到最优   就剪掉 #include < ...

  9. poj2286The Rotation Game(迭代加深dfs)

    链接 把迭代加深理解错了 自己写了半天也没写对 所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索.这种方法虽然会导致重复的遍历 某些结点,但 ...

随机推荐

  1. 初学者使用IntellJ IDEA建立Struts2项目

    1.建立项目,打开IDEA,点击File——>new project,选择Java Module,点击Next 选中Web Application,Version选中3.0,选中create w ...

  2. Android学习【Android内核编译流程和错误笔记】

    博客:http://blog.csdn.net/muyang_ren Ubuntu14.04 LTS(要求是64位长期支持版LTS) Jdk1.8 内核:android4.0 一:jdk 1.解压jd ...

  3. 通过Url传多个参数方法

    MVC3通过URL传值,一般情况下都会遇到[从客户端(&)中检测到有潜在危险的 Request.Path 值]的问题 这个问题的解决方法,我的其他博文已经有了说明,这里给出连接[从客户端(&a ...

  4. 安装XP和Ubuntu双系统问题——Ubuntu安装时无法识别原有系统

    我主机本来安装了windows xp 和unbuntu15.04,由于在ubuntu下不小心卸载某依赖后,无法登入桌面,网上查了好多资料,原因各种,解决途径也各种,最终是还没有解决问题.各种更新,各种 ...

  5. 添加序号列(SQL Server)

    SELECT ROW_NUMBER() OVER (ORDER BY 实际缴费金额 ) AS A, --序号 RANK() OVER (ORDER BY 实际缴费金额 ) AS B, --相同跳过从新 ...

  6. Linux 开机报 or type Control-D to continue

    解决步骤: 1.输入root密码 2.看是哪个盘报的错,我这边是sda3(可能会是不同的盘),就是代码中标为FAIL 输入以下命令fsck -y /dev/sda3

  7. C++ list用法

    创建一个list实例并赋值: // 创建实例以及赋值 #include <iostream> #include <list> using namespace std; int ...

  8. (原创) jetson tk1 初始化

    1. 相关的网站: 1. Jetson TK1 support   https://developer.nvidia.com/jetson-tk1-support 2.official Wiki fo ...

  9. XP 安装

    提供一下裝系統的詳細步驟,盡量詳細到每一步都有,希望能對樓主有所幫助,不盡之處還請樓主不吝指出!謝謝 装XP的步骤如下: 开机时,按del键, 进入bios界面,一般选左侧第二项,(Advanced ...

  10. django TypeError: 'module' object is not callable

    原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...