Description

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

x2 = x × xx3 = x2 × xx4 = 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 × xx3 = x2 × xx6 = x3 × x3x7 = x6 × xx14 = x7 × x7x15 = x14 × xx30 = x15 × x15x31 = 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 × x2x8 = x4 × x4x8 = x4 × x4x10 = x8 × x2x20 = x10 × x10x30 = x20 × x10x31 = 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 × xx4 = x2 × x2x8 = x4 × x4x16 = x8 × x8x32 = x16 × x16x31 = 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−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

Source

思路:用一个数组存每一次操作之后得到的数,剪下枝,迭代加深就可以。

详见代码。

  1. #include <stdio.h>
  2. #define max(A,B)(A>B?A:B)
  3.  
  4. int n,dep,num[15];
  5.  
  6. bool dfs(int cnt,int x)//x是上一次操作之后得到的最大的数
  7. {
  8. if(num[cnt]==n) return 1;
  9.  
  10. if(cnt>=dep) return 0;
  11.  
  12. x=max(x,num[cnt]);
  13.  
  14. if(x*(1<<(dep-cnt))<n) return 0;//假设最大的数都不能得到n就直接返回
  15.  
  16. for(int i=0;i<=cnt;i++)
  17. {
  18. num[cnt+1]=num[cnt]+num[i];
  19.  
  20. if(dfs(cnt+1,x)) return 1;
  21.  
  22. if(num[cnt]>num[i]) num[cnt+1]=num[cnt]-num[i];
  23. else num[cnt+1]=num[i]-num[cnt];
  24.  
  25. if(dfs(cnt+1,x)) return 1;
  26. }
  27.  
  28. return 0;
  29. }
  30.  
  31. int main()
  32. {
  33. while(~scanf("%d",&n) && n)
  34. {
  35. if(n==1) printf("0\n");
  36. else
  37. {
  38. num[0]=1;
  39.  
  40. for(dep=1;;dep++)
  41. {
  42. if(dfs(0,1)) break;
  43. }
  44.  
  45. printf("%d\n",dep);
  46. }
  47. }
  48. }

POJ-3134-Power Calculus(迭代加深DFS)的更多相关文章

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

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

  2. poj 3134 Power Calculus(迭代加深dfs+强剪枝)

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

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

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

  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. HDU 2553 N皇后问题【棋盘型DFS】

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. HDU 2044 一只小蜜蜂(递归)

    一只小蜜蜂... Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. python 2 range, list, and set

    这里主要说的是用python中的range来模拟for循环 转载请声明本文的引用出处:仰望大牛的小清新 1.range(var1,var2,var3): range产生一个列表(list),var1& ...

  4. web.input()和web.data() 遇到特殊字符

    使用web.py的时候,web.input()和web.data() 都可以接收用户从浏览器端输入的参数. web.input()方法返回一个包含从url(GET方法)或http header(POS ...

  5. Typora

    Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...

  6. 22、Django实战第22天:课程评论

    1.编辑course-comment.html 2.编辑courses.views.py class CourseCommentView(LoginRequiredMixin, View): def ...

  7. 【OpenJudge8464】【序列DP】股票买卖

    股票买卖 总时间限制: 1000ms 内存限制: 65536kB [描述] 最近越来越多的人都投身股市,阿福也有点心动了.谨记着“股市有风险,入市需谨慎”,阿福决定先来研究一下简化版的股票买卖问题. ...

  8. scope的范围

    (一)scope=“singleton” 知识点:无论获取多少个bean,得到的总是一样的地址,singleton范围下只会创建一个bean实例 1.Bean4.java package com.in ...

  9. iOS 调H5方法不执行没反应的坑

    调用H5的方法需要给H5传一些参数,参数中包括图片的base64字符串. 错误一: 图片转base64,后面参数不能随便写,正确做法如下 NSData *imageData = UIImageJPEG ...

  10. maven-pom-properties

    出处: http://blog.csdn.net/taiyangdao/article/details/52358083