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−3, 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 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
31
70
91
473
512
811
953
0
Sample Output
0
6
8
9
11
9
13
12
Source
思路:用一个数组存每一次操作之后得到的数,剪下枝,迭代加深就可以。
详见代码。
#include <stdio.h>
#define max(A,B)(A>B?A:B) int n,dep,num[15]; bool dfs(int cnt,int x)//x是上一次操作之后得到的最大的数
{
if(num[cnt]==n) return 1; if(cnt>=dep) return 0; x=max(x,num[cnt]); if(x*(1<<(dep-cnt))<n) return 0;//假设最大的数都不能得到n就直接返回 for(int i=0;i<=cnt;i++)
{
num[cnt+1]=num[cnt]+num[i]; if(dfs(cnt+1,x)) return 1; if(num[cnt]>num[i]) num[cnt+1]=num[cnt]-num[i];
else num[cnt+1]=num[i]-num[cnt]; if(dfs(cnt+1,x)) return 1;
} return 0;
} int main()
{
while(~scanf("%d",&n) && n)
{
if(n==1) printf("0\n");
else
{
num[0]=1; for(dep=1;;dep++)
{
if(dfs(0,1)) break;
} printf("%d\n",dep);
}
}
}
POJ-3134-Power Calculus(迭代加深DFS)的更多相关文章
- POJ 3134 Power Calculus (迭代剪枝搜索)
题目大意:略 题目里所有的运算都是幂运算,所以转化成指数的加减 由于搜索层数不会超过$2*log$层,所以用一个栈存储哪些数已经被组合出来了,不必暴力枚举哪些数已经被搜出来了 然后跑$iddfs$就行 ...
- poj 3134 Power Calculus(迭代加深dfs+强剪枝)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- 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)
链接 把迭代加深理解错了 自己写了半天也没写对 所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索.这种方法虽然会导致重复的遍历 某些结点,但 ...
随机推荐
- 51nod 1459 迷宫游戏【最短路拓展】
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连 ...
- HDU 6213 Chinese Zodiac 【模拟/水题/生肖】
Problem Description The Chinese Zodiac, known as Sheng Xiao, is based on a twelve-year cycle, each y ...
- Codeforces 180C. Letter
题目链接:http://codeforces.com/problemset/problem/180/C 题意: 给你一个仅包含大写字母和小写字母的字符串,你可以将让小写字母转化为大写字母,大写字母转化 ...
- gcc与gdb的使用
1.gcc/g++编译过程: gcc/g++的编译格式: gcc [option][filename]... g++ [option][filename]... 1)processing:预处理过程, ...
- 洛谷——P1655 小朋友的球
P1655 小朋友的球 题目描述 @发源于 小朋友最近特别喜欢球.有一天他脑子抽了,从口袋里拿出了N个不同的球,想把它们放到M个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法,于是尝 ...
- [BZOJ 2809] Dispatching
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 Algorithm: 很容易看出此题贪心的思路: 只要在每个点的子树中贪心选取费用 ...
- 【树上莫队】【带修莫队】bzoj3052 [wc2013]糖果公园
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- Djanog|requirements.txt生成
Django | requirement.txt 生成 pip django 1 pip 通常我们熟悉使用的都是 pip, 这个工具确实方便项目管理依赖包.当想把当前项目依赖的包的名称和版本导入指 ...
- Java高级架构师(一)第17节:X-gen生成所需的DAO部分模板
- svn的安装(整合apache、ldap)包括错误解决post commit FS processing had error
2013年12月5日 admin 发表评论 阅读评论 以下是centos环境下,以yum安装apache及其相关软件.svn使用源码包编译,使用官网最新的1.8.5版本. 一.安装apache ope ...