迭代加深搜索POJ 3134 Power Calculus

题意:输入正整数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的更多相关文章
- 迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)
本题代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- POJ 3134 Power Calculus ID-DFS +剪枝
题意:给你个数n 让你求从x出发用乘除法最少多少步算出x^n. 思路: 一看数据范围 n<=1000 好了,,暴搜.. 但是 一开始写的辣鸡暴搜 样例只能过一半.. 大数据跑了10分钟才跑出来. ...
- POJ 3134 - Power Calculus (IDDFS)
题意:求仅仅用乘法和除法最快多少步能够求到x^n 思路:迭代加深搜索 //Accepted 164K 1094MS C++ 840B include<cstdio> #include< ...
- 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
迭代加深 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<al ...
- POJ 3134 Power Calculus (迭代剪枝搜索)
题目大意:略 题目里所有的运算都是幂运算,所以转化成指数的加减 由于搜索层数不会超过$2*log$层,所以用一个栈存储哪些数已经被组合出来了,不必暴力枚举哪些数已经被搜出来了 然后跑$iddfs$就行 ...
- poj 3134 Power Calculus(IDA*)
题目大意: 用最小的步数算出 x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A* 就是如果这个数一直平方 所需要的步骤数都不能达到最优 就剪掉 #include < ...
- 【算法•日更•第三十九期】迭代加深搜索:洛谷SP7579 YOKOF - Power Calculus 题解
废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...
随机推荐
- Can you solve this equation?
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- Android 开发笔记___SD卡基本操作__图片读取写入
package com.example.alimjan.hello_world.Utils; import android.graphics.Bitmap; import android.graphi ...
- 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan支持组件注解限制(七)
实现功能 以上的代码我们发现.我们都是将@ComponentScan扫描的路径下的所有类都加载到容器中的. 而实际需求,我们并不希望所有的类都创建对象,而是加了组件注解@Controller,@Ser ...
- IntelliJ配置jenkins服务的Crumb Data
近期在做jenkins测试,IntelliJ并没有自动安装jenkins服务器,因此需要自己添加,但是如果不配置Crumb Data,jenkins的服务就不能使用. 首先在服务器中开启CSRF服务, ...
- 如何将FastReportOnlineDesign 灵活的应用到C/S B/S 程序当中?
一.好久没有写博客了,主要是停在这里太久了,有些事情让自己尽量不在去想,忘记不了一段难以忘怀的记忆,就让这一段美好的记忆沉没在无锡的太湖中吧!不在去想了.难以忘怀..... 二.废话不多说了,不如正题 ...
- c#中获取路径方法
要在c#中获取路径有好多方法,一般常用的有以下五种: //获取应用程序的当前工作目录. String path1 = System.IO.Directory.GetCurrentDirectory() ...
- Visual Studio 2017开发环境的安装
Visual Studio 2017是微软为了配合.NET战略推出的IDE开发环境,同时也是目前开发C#程序最新的工具,本节以Visual Studio 2017社区版的安装为例讲解具体的安装步骤. ...
- [转]Oracle执行计划详解
Oracle执行计划详解 --- 作者:TTT BLOG 本文地址:http://blog.chinaunix.net/u3/107265/showart_2192657.html --- 简介: ...
- 用StringBuilder和StringBuffer实现的Unicode解码方法的比较(Java)
初衷是用正则来写一个Unicode字符串转码的方法,一开始是打算结合StringBuilder写的,但是看到jdk7的Matcher.appendReplacement文档中一段示例代码用了Match ...
- java并发之同步辅助类(Semphore、CountDownLatch、CyclicBarrier、Phaser)
线程同步辅助类,主要学习两点: 1.上述几种同步辅助类的作用以及常用的方法 2.适用场景,如果有适当的场景可以用到,那无疑是最好的 semaphore(seməˌfôr) 含义 信号量就是可以声明多把 ...