LA 3621 / POJ 3134 Power Calculus (迭代加深搜索)
2015-05-29 01:15
309 查看
题目链接:http://poj.org/problem?id=3134
题目大意:给出x和正整数n(1<=n<=1000),问最少需要几次乘除法可以得到x^n。
解法:n比较小,迭代加深搜索,浅层内求出解,加上剪枝就OK了。
迭代加深搜索学习资料:
http://www.nocow.cn/index.php/Translate:USACO/More_Search_Techniques
Code:
题目大意:给出x和正整数n(1<=n<=1000),问最少需要几次乘除法可以得到x^n。
解法:n比较小,迭代加深搜索,浅层内求出解,加上剪枝就OK了。
迭代加深搜索学习资料:
http://www.nocow.cn/index.php/Translate:USACO/More_Search_Techniques
Code:
//#pragma comment(linker, "/STACK:102400000,102400000") //C++ //int size = 256 << 20; // 256MB //char *p = (char*)malloc(size) + size; //__asm__("movl %0, %%esp\n" :: "r"(p)); //G++ #include<set> #include<map> #include<queue> #include<stack> #include<ctime> #include<deque> #include<cmath> #include<vector> #include<string> #include<cctype> #include<cstdio> #include<cstdlib> #include<cstring> #include<sstream> #include<iostream> #include<algorithm> #define REP(i,s,t) for(int i=(s);i<=(t);i++) #define REP2(i,s,t) for(int i=(s);i>=(t);i--) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned long ul; int n; const int N=1005; int tmp[20]; int dep; bool dfs(int cur,int x) { if(tmp[cur]==n) { return 1; } if(cur>=dep) { return 0; } x=max(x,tmp[cur]); if(x*(1<<(dep-cur))<n) { return 0; } REP(i,0,cur) { if(x+tmp[i]<=n) { tmp[cur+1]=x+tmp[i]; if(dfs(cur+1,x)) { return 1; } } if(x-tmp[i]>=0) { tmp[cur+1]=x>tmp[i]?x-tmp[i]:tmp[i]-x; if(dfs(cur+1,x)) { return 1; } } } return 0; } void solve(int n) { if(n==1) { printf("%d\n",0); } else { tmp[0]=1; dep=1; while(1) { if(dfs(0,1)) { break; } dep++; } printf("%d\n",dep); } } int main() { #ifdef ONLINE_JUDGE #else freopen("test.in","r",stdin); #endif while(~scanf("%d",&n)) { if(!n)break; solve(n); } return 0; }
相关文章推荐
- 使用mark-sweep算法的垃圾回收器
- First JAVA之低仿QQ(呵呵)
- 【学习拾遗】Jquery(一)--核心
- 在O(1)时间内删除单链表结点
- CF #305(Div.2) D. Mike and Feet(数学推导)
- Php RSS
- 分配All AD User到SharePoint Group中
- HeartBeat v2实现高可用LAMP
- JavaSE学习总结第21天_IO流3
- SharePoint 2013 网站迁移流程
- 在Ubuntu Server 12.04 LTS上搭建可远程访问的PostgreSQL 9.1环境
- windows8.1和ubunt14.04安装时问题!
- SharePoint 2013 一次性上传文件大小限制
- JavaWeb系列之五(HTTP协议和WEB概述与TOMCAT的简介、安装)
- SharePoint Server 2013 通过IP无法访问站点
- 解决Mac下GDB提示签名错误
- 注解解决Hibernate中should be mapped with insert="false" updatable=false
- IDAPython插件安装
- 后台开发-win8下IIS服务器服务器搭建
- hihoCoder 47周 拓扑排序