题意:输入正整数n(1<=n<=1000),问最少需要几次乘除法可以从x得到x的n次方,计算过程中x的指数要求是正的。

题解:这道题,他的结果是由1经过n次加减得到的,所以最先想到的就是暴力回溯,其中的剪枝函数,首先不能得到重复的数,其次深度有上限,上限是n-1,还有,如果当前序列的最大数乘以2的(dep-cnt)(dep与cnt分别表示深度上限和当前深度)次方小于n,则剪枝。但是这样的时间复杂度还是特别高,所以又想到了另外的方法,就是先列出深度,然后找在这个深度里是否存在一个方法得到n

代码:

 #include<math.h>
#include<vector>
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
const int maxn=;
int n,minn,dep;
int ans[maxn]; bool dfs(int num,int cnt){
if(cnt==dep){
if(num==n)
return ;
return ;
}
if(num*(<<(dep-cnt))<n) return ;
for(int i=cnt;i>=;i--){
ans[cnt+]=ans[i]+num;
if(dfs(ans[i]+num,cnt+)){
return ;
}
if(ans[i]<num){
ans[cnt+]=num-ans[i];
if(dfs(num-ans[i],cnt+)){
return ;
}
}
else if(ans[i]<num){
ans[cnt+]=ans[i]-num;
if(dfs((ans[i])-num,cnt+))
return ;
}
}
return ;
} //bool dfs(int num,int cnt){
// if(num==n){
// return 1;
// }
// if(cnt==dep){
// return 0;
// }
// if(num*(1<<dep-cnt)<n) return 0;
// for(int i=0;i<=cnt;i++){
// for(int j=1;j<=2;j++){
// if(j==1){
// ans[cnt+1]=num+ans[i];
// if(dfs(num+ans[i],cnt+1)){return 1;}
// }
// else{
// ans[cnt+1]=num-ans[i];
// if(num-ans[i]<0) continue;
// if(dfs(num-ans[i],cnt+1)) return 1;
// }
// }
// }
// return 0;
//} int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)&&n){
minn=;
memset(ans,,sizeof(ans));
ans[]=;
for(dep=;dep<=;dep++){
if(dfs(,))
break;
}
printf("%d\n",dep);
}
}

      

迭代加深搜索POJ 3134 Power Calculus的更多相关文章

  1. 迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)

    本题代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...

  2. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

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

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

  4. POJ 3134 - Power Calculus (IDDFS)

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

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

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

  6. POJ 3134 - Power Calculus

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

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

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

  8. poj 3134 Power Calculus(IDA*)

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

  9. 【算法•日更•第三十九期】迭代加深搜索:洛谷SP7579 YOKOF - Power Calculus 题解

    废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...

随机推荐

  1. code forces 439 C. The Intriguing Obsession

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. SSH框架的多表查询和增删查改 (方法一)上

    原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>  http://www.cnblogs.com/zhu520/p/7772823.html   因为最近在做Android 练习的 ...

  3. null transform hack 强制使用硬件加速

    -webkit-transform: translateZ(0); -webkit-transform: translate3d(0,0,0);   作用: 1.切换到硬件合成模式,通常所有事情都CP ...

  4. ruby 安装 mysql2 命令

    sudo apt-get install libmysql-ruby libmyclient-dev

  5. Linux 新建文件/文件夹,删除文件文件夹,查找文件 打开文件

    1.新建文件夹:mkdir xx 2.新建文件: touch 1.py 3.删除文件/文件夹: rm -r xx  rm 1.py 4.打开文件:cat 1.py 只显示前几行 :head -2 1. ...

  6. struts2框架的登录制作

    首先:我们要建一个web项目 接着: 我们先来导入struts的xml文件 第一步:右击你的项目名,鼠标到MyEclipse会看到一个add struts开头的文件,点开以后看到: 这里我们选择str ...

  7. 一起写框架-Ioc内核容器的实现-基础功能-容器对象名默认首字母小写(八)

    实现功能 --前面实现的代码-- 默认的对象名就类名.不符合Java的命名规范.我们希望默认的对象名首字母小写. 实现思路 创建一个命名规则的帮助类.实现将对大写开头的对象名修改为小写开头. 实现步骤 ...

  8. no suitable driver found for jdbc:mysql//localhost:3306/..

      出现这样的情况,一般有四种原因(网上查的): 一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql ...

  9. mysql 运维常见操作

    初始安装并赋予密码:   [root@Alinx html]# yum install -y mysql mysql-server                         #安装mysql可与 ...

  10. 掌握numpy(三)

    统计功能 前面都是介绍numpy的一些特性,被称为数学运算神器怎么能少了统计功能呢 ndarray的方法 a = np.array([[-2.5, 3.1, 7], [10, 11, 12]]) &g ...