题目

题目

 


 

分析

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*)的更多相关文章

  1. UVa 1374 Power Calculus (IDA*或都打表)

    题意:给定一个数n,让你求从1至少要做多少次乘除才可以从 x 得到 xn. 析:首先这个是幂级的,次数不会很多,所以可以考虑IDA*算法,这个算法并不难,难在找乐观函数h(x), 这个题乐观函数可以是 ...

  2. 【Uva11212】 Editing a Book(IDA*)

    [题意] 有n个数字的全排列,每次可以剪切一段粘贴到某个位置.问最后变成升序最少多少步. 如"{2,4,1,5,3,6}要2步 {3,4,5,1,2}只要一步 [分析] 迭代深搜真的AC了也 ...

  3. 【UVa】Partitioning by Palindromes(dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...

  4. 【UVa】439 Knight Moves(dfs)

    题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <a ...

  5. 【UVa】1600 Patrol Robot(dfs)

    题目 题目     分析 bfs可以搞,但是我还是喜欢dfs,要记忆化不然会T     代码 #include <cstdio> #include <cstring> #inc ...

  6. 【UVA】1596 Bug Hunt(模拟)

    题目 题目     分析 算是个模拟吧     代码 #include <bits/stdc++.h> using namespace std; map<int,int> a[ ...

  7. 【UVA】10763 Foreign Exchange(map)

    题目 题目     分析 没什么好说的,字符串拼接一下再放进map.其实可以直接开俩数组排序后对比一下,但是我还是想熟悉熟悉map用法. 呃400ms,有点慢.     代码 #include < ...

  8. 【题解】UVA10298 Power String(KMP)

    UVA10298:https://www.luogu.org/problemnew/show/UVA10298 思路 设P[x]数组为 前x个字符的最大前缀长度等于后缀字串 由P数组的定义我们可以知道 ...

  9. 【Luogu】P2324骑士精神(IDA*)

    题目链接 当guess>limit-deep的时候return就好了. guess是估价函数,值为不在自己地盘上的骑士个数.limit是本次迭代阈值.deep是已经走了多少步. 这个优化是显然的 ...

随机推荐

  1. html dom SetInterVal()

    HTML DOM setInterval() 方法 HTML DOM Window 对象 定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInt ...

  2. jQuery 滑动选项卡SmartTab

    Smart Tab Overview Smart Tab is a jQuery plugin for tabbed interface. It is flexible and very easy t ...

  3. WIP - Study Perf (by quqi99)

    版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99) Perf Flame Graph sudo perf ...

  4. clean-css 安装 使用

    https://github.com/jakubpawlowicz/clean-css-cli https://davidwalsh.name/clean-css

  5. C# http post上传文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  6. IOS开发 多线程编程 - NSThread

    每个iOS应用程序都有个专门用来更新显示UI界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来极坏的用户体验.一般的解决方案就是将 ...

  7. postfix邮件服务器搭建03-webmail安装篇

    本文接着上文的安装进行,介绍另一个WebMail功能更加人性化的roundcube.当然也可以对已有的postfix邮件系统进行功能完善 1.下载安装roundcube cd /server/tool ...

  8. axios如何使用

    我们知道,vue2.0以后,vue就不再对vue-resource进行更新,而是推荐axios,而大型项目都会使用 Vuex 来管理数据,所以这篇博客将结合两者来发送请求 1.安装axios cnpm ...

  9. MySQL中表的复制

    1.语法 create table 表名 select .. from 表名 where 条件; 2.示例 1.复制MOSHOU.sanguo表的全部记录和字段,sanguo2 create tabl ...

  10. eclipse添加propedit插件

    1.propedit插件 这个插件基本上可以支持各种语言的转换. 2.方法如下: “help”--“Install new software”--“add” name:propedit Locatio ...