poj 3134 Power Calculus(迭代加深dfs+强剪枝)
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+强剪枝)的更多相关文章
- POJ 3134 Power Calculus (迭代剪枝搜索)
题目大意:略 题目里所有的运算都是幂运算,所以转化成指数的加减 由于搜索层数不会超过$2*log$层,所以用一个栈存储哪些数已经被组合出来了,不必暴力枚举哪些数已经被搜出来了 然后跑$iddfs$就行 ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- POJ-3134-Power Calculus(迭代加深DFS)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
- POJ 3134 Power Calculus ID-DFS +剪枝
题意:给你个数n 让你求从x出发用乘除法最少多少步算出x^n. 思路: 一看数据范围 n<=1000 好了,,暴搜.. 但是 一开始写的辣鸡暴搜 样例只能过一半.. 大数据跑了10分钟才跑出来. ...
- POJ 3134 - Power Calculus
迭代加深 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<al ...
- POJ 3134 - Power Calculus (IDDFS)
题意:求仅仅用乘法和除法最快多少步能够求到x^n 思路:迭代加深搜索 //Accepted 164K 1094MS C++ 840B include<cstdio> #include< ...
- 迭代加深搜索POJ 3134 Power Calculus
题意:输入正整数n(1<=n<=1000),问最少需要几次乘除法可以从x得到x的n次方,计算过程中x的指数要求是正的. 题解:这道题,他的结果是由1经过n次加减得到的,所以最先想到的就是暴 ...
- poj 3134 Power Calculus(IDA*)
题目大意: 用最小的步数算出 x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A* 就是如果这个数一直平方 所需要的步骤数都不能达到最优 就剪掉 #include < ...
- poj2286The Rotation Game(迭代加深dfs)
链接 把迭代加深理解错了 自己写了半天也没写对 所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索.这种方法虽然会导致重复的遍历 某些结点,但 ...
随机推荐
- struts2.1.3之后使用自定义Filter
struts2中 ActionContextCleanUp, StrutsPrepareAndExecuteFilter, StrutsPrepareFilter,StrutsExecuteFilte ...
- Android系统如何实现UI的自适应
做Android应用的人都知道,要一个apk适用多个不同的手机屏幕是很容易的,就是在项目的res文件夹下面有多套相关的资源文件.程序运行的时候,Android系统会根据当前设备的信息去加载不同文件夹下 ...
- Qt读取JSON和XML数据
QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...
- Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)
解题报告 四种情况相应以下四组数据. 给两字符串,推断第一个字符串是怎么变到第二个字符串. automaton 去掉随意字符后成功转换 array 改变随意两字符后成功转换 再者是两个都有和两个都没有 ...
- Android应用程序的安装位置
Android应用程序的默认安装位置以及是否可移动取决于开发者在其AndroidManifest.xml中的设置: <manifestxmlns:android="http://s ...
- Spinner( 微调) 组件
本节课重点了解 EasyUI 中 Spinner(微调)组件的使用方法,这个组件依赖于ValidateBox(验证框)组件. 一. 加载方式Spinner(微调)组件是其他两款高级微调组件的基础组件, ...
- SQL从入门到基础 - 05 数据分组、Having语句
一.数据分组 1. 按照年龄进行分组统计各个年龄段的人数: Select FAge,count(*) from T_Employee group by FAge; 2. Group by子句必须放到w ...
- uva 10929 - You can say 11
#include <cstdio> using namespace std; ]; int main() { while(gets(in)) { ] == ] == ) break; ; ...
- Linux 软链接和硬链接的理解与学习
理解前提: 首先要知道 Linux任意一个文件包含2个信息:第一个信息就是文件本身存的内容,第二个信息是文件的控制信息(读写,路径,大小等等),这2个信息是分开存储的,明白这点非常重要 理解总结: L ...
- Linux修改时间时区并在Tomcat中生效
Linux查看当前时间时区linux:~ # datelinux:~ # date –Rlinux:~ # zdump -v /usr/share/zoneinfo/Asia/Beijing ---- ...