【UVa】1374 Power Calculus(IDA*)
题目
分析
IDA*大法好,抄了lrj代码。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxans=14;
int n,a[maxans+1];
bool dfs(int d,int maxd)
{
if(a[d] == n) return true;
if(d == maxd) return false;
int maxv=a[0];
for(int i=1; i<=d; i++) maxv=max(maxv, a[i]);
if( (maxv << (maxd-d)) < n) return false;
for(int i=d; i>=0; i--)
{
a[d+1]=a[d]+a[i];
if(dfs(d+1 , maxd)) return true;
a[d+1] = a[d] - a[i];
if(dfs(d+1 , maxd)) return true;
}
return false;
}
int solve(int n)
{
if(n==1) return 0;
a[0]=1;
for(int maxd=1; maxd < maxans; maxd++)
if(dfs(0,maxd)) return maxd;
return maxans;
}
int main()
{
while( scanf("%d",&n)==1 && n!=0)
printf("%d\n",solve(n));
return 0;
}
【UVa】1374 Power Calculus(IDA*)的更多相关文章
- UVa 1374 Power Calculus (IDA*或都打表)
题意:给定一个数n,让你求从1至少要做多少次乘除才可以从 x 得到 xn. 析:首先这个是幂级的,次数不会很多,所以可以考虑IDA*算法,这个算法并不难,难在找乐观函数h(x), 这个题乐观函数可以是 ...
- 【Uva11212】 Editing a Book(IDA*)
[题意] 有n个数字的全排列,每次可以剪切一段粘贴到某个位置.问最后变成升序最少多少步. 如"{2,4,1,5,3,6}要2步 {3,4,5,1,2}只要一步 [分析] 迭代深搜真的AC了也 ...
- 【UVa】Partitioning by Palindromes(dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- 【UVa】1600 Patrol Robot(dfs)
题目 题目 分析 bfs可以搞,但是我还是喜欢dfs,要记忆化不然会T 代码 #include <cstdio> #include <cstring> #inc ...
- 【UVA】1596 Bug Hunt(模拟)
题目 题目 分析 算是个模拟吧 代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...
- 【UVA】10763 Foreign Exchange(map)
题目 题目 分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢. 代码 #include < ...
- 【题解】UVA10298 Power String(KMP)
UVA10298:https://www.luogu.org/problemnew/show/UVA10298 思路 设P[x]数组为 前x个字符的最大前缀长度等于后缀字串 由P数组的定义我们可以知道 ...
- 【Luogu】P2324骑士精神(IDA*)
题目链接 当guess>limit-deep的时候return就好了. guess是估价函数,值为不在自己地盘上的骑士个数.limit是本次迭代阈值.deep是已经走了多少步. 这个优化是显然的 ...
随机推荐
- c# html内容处理类
using System; using System.Text; using System.Text.RegularExpressions; using System.Net; using Syste ...
- java事务(二)
本地事务 事务类型 事务可以分为本地事务和分布式事务两种类型.这两种事务类型是根据访问并更新的数据资源的多少来进行区分的.本地事务是在单个数据源上进行数据的访问和更新,而分布式事务是跨越多个数据源来进 ...
- widow系统 LuaForWindows,安装 luasocket
参考 http://94it.net/a/jingxuanboke/2013/0625/49052.html 1. 我用的是 LuaForWindows_v5.1.4-46.exe 可以在我的百度网盘 ...
- (转)MapReduce Design Patterns(chapter 7 (part 1))(十三)
CHAPTER 7.Input and Output Patterns 本章关注一个最经常忽略的问题,来改进MapReduce 的value:自定义输入和输出.我们并不会总使用Mapreduce本身的 ...
- Android内存优化(三)避免可控的内存泄漏
相关文章 Android性能优化系列 Java虚拟机系列 前言 内存泄漏向来都是内存优化的重点,它如同幽灵一般存于我们的应用当中,有时它不会现身,但一旦现身就会让你头疼不已.因此,如何避免.发现和解决 ...
- Webdynpro ABAP 简单剖析
众所周知,WEBDYNPRO是今天来SAP主推的一个面向WEB的MVC编程框架,接触过J2EE的朋友都不会对MVC这种设计模式陌生,WEBDYNPRO ABAP的基本设计思路和很多著名的面向互联网的M ...
- log4j 格式详解
### 设置### log4j.rootLogger = debug,stdout,D,E ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.lo ...
- CF1114B Yet Another Array Partitioning Task
CF1114B Yet Another Array Partitioning Task 贪心,选择前 \(k*m\) 大的元素对答案进行贡献. 每次划分时,从当前位置往后扫,扫到 \(m\) 个前 \ ...
- BZOJ3998 TJOI2015 弦论 【后缀自动机】【贪心】
Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个. ...
- Codeforces 1027E Inverse Coloring 【DP】
Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...