当前位置: 首页 > news >正文

UVa1374 Power Calculus (IDA*)

题目链接

Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications: The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute x31 with eight multiplications:(略)
This is not the shortest sequence of multiplications to compute x31. There are many ways with only seven multiplications. The following is one of them:(略)
There however is no way to compute x31 with fewer multiplications. Thus this is one of the most efficient ways to compute x31 only by multiplications. If division is also available, we can find a shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):(略)
This is one of the most efficient ways to compute x31 if a division is as fast as a multiplication. Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence of operations should be x to a positive integer’s power. In other words, x−3, for example, should never appear.

Input
The input is a sequence of one or more lines each containing a single integer n. n is positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output
Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.

1.题意不再赘述,刚开始想到了快速幂,想在快速幂的过程中求出最小的步数,但实际上快速幂并不是择优选择的,而是从x1每次开方,当n&1时就乘一次x,实际上x的计算次数还是比较多的

2.看分析说是IDA*便开始构思代码。估价函数自己没想出来,但是看LRJ那么写却可以看懂(还是要多独立思考)。我们知道,每深入搜索一层,那么可以得到该层的最大数值是2当前层数,那么我们设置了最大搜索层数,如果当前的最大数值为x,x*2maxd-d<n,代表搜完规定层数的最大值仍然小于n,那么就剪枝(实际上我们发现一开始如果2maxd<n就不会搜索下去直接返回)。但是如何实现每一次搜索得到的序列集合呢,而且我们又要将集合中的元素都相互加减,那么我觉得需要数组将每个集合的存起来,还要用vis判重,但是实际上代码不许要写那么多

3.我们只用一个数组a记录到当前数x,那么我们每次只用新加入的数x对之前数组中所有的元素加减再dfs,那么可以优化很多dfs。例如刚开始只有{1},那么1对数组中所有元素加减,接下来加入了2,那么2在对{1,2}加减,这时的1就不需要再加减了(负数是需要返回的)

#include <iostream>

using namespace std;
int n,ans;
int a[1005];

bool dfs(int cur,int x,int maxd){
    if((x<<(maxd-cur))<n) return false;
    if(x<=0 || cur>maxd) return false;
    if(x==n || (x<<(maxd-cur))==n){ ans=cur; return true; };
    a[cur]=x;
    for(int i=0;i<=cur;i++){
        if(dfs(cur+1,x+a[i],maxd)) return true;
        if(dfs(cur+1,x-a[i],maxd)) return true;
    }
    return false;
}

int main()
{
    while(scanf("%d",&n) && n){
        for(int i=0;;i++){  //这里需要从0开始,没注意WA了一次
            if(dfs(0,1,i)){
                printf("%d\n",i);
                break;
            }
        }
    }
    return 0;
}

相关文章:

  • Codeforces Round #624 (Div. 3) D - Three Integers(枚举技巧)
  • Android应用程序使用Google Map
  • 程序员如何提高工作效率
  • Codeforces Round #624 (Div. 3) F - Moving Points(树状数组+去重离散化)
  • Codeforces Round #624 (Div. 3) 解(补)题报告
  • chipmunk物理引擎的基本概念和基本用法
  • STL之list
  • Medusa 3D 我的场景编辑器
  • 跟我学交换机配置(三)
  • 跟我学交换机配置(四)
  • 哈密顿回路 竞赛图 构造哈密顿回路(待更新)
  • 展现体验式营销的魅力(3,完)
  • UCF Local Programming Contest 2015 计蒜客重现 解(补)题报告
  • 那些年我们一起踩过的坑——读取字符(串)篇
  • Android开发指南-用户界面-菜单特性
  • [笔记] php常见简单功能及函数
  • 10个最佳ES6特性 ES7与ES8的特性
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • AHK 中 = 和 == 等比较运算符的用法
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • js写一个简单的选项卡
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • learning koa2.x
  • magento 货币换算
  • mysql 数据库四种事务隔离级别
  • PermissionScope Swift4 兼容问题
  • Python学习之路13-记分
  • 免费小说阅读小程序
  • 普通函数和构造函数的区别
  • 前端性能优化——回流与重绘
  • 我与Jetbrains的这些年
  • 小程序开发中的那些坑
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 容器镜像
  • #传输# #传输数据判断#
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (二)PySpark3:SparkSQL编程
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (蓝桥杯每日一题)love
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (一)WLAN定义和基本架构转
  • (转)IOS中获取各种文件的目录路径的方法
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET MVC第三章、三种传值方式
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道
  • .NET 药厂业务系统 CPU爆高分析
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • @Data注解的作用
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!