第二周习题F

Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = xxx,     x3 = x2xx,     x4 = x3xx,     ...  ,     x31 = x30xx.

The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute  x31 with eight multiplications:

x2 = xxx,     x3 = x2xx,     x6 = x3xx3,     x7 = x6xx,     x14 = x7xx7
x15 = x14xx,     x30 = x15xx15,     x31 = x30xx.

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:

x2 = xxx,     x4 = x2xx2,     x8 = x4xx4,     x10 = x8xx2
x20 = x10xx10,     x30 = x20xx10,     x31 = x30xx.

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):

x2 = xxx,     x4 = x2xx2,     x8 = x4xx4,     x16 = x8xx8,     x32 = x16xx16,     x31 = x32 ÷ x.

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 nn 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.

Sample Input

1
31
70
91
473
512
811
953
0

Sample Output

0
6
8
9
11
9
13
12

这道题用到回溯法,不过要进行剪枝,最有效的剪枝在于预判,所以不要试图去深搜一次来得到最小步数,这样剪枝根本无法进行,可以去枚举尝试,从一到。。。,每次的这个i步作为约束条件,即判断,i步能否到达,因为深搜的结构是一颗解答树,你可以从当前步数获得将来一定不能到达的条件,如,当前步为step,我要求dp步到达,那么还剩下dp-step步,那么最后那一步我可以到达的最大数为当前数乘以pow(2,dp-ste),如果这一步的数还小于我n,那么就肯定无法到达了。。。。。。。

本题非原创,完全是参考别人的。。。。。。。
#include"iostream"
#include"stdio.h"
#include"cstring"
#include"algorithm"
using namespace std;

int n;
int ans=1000000;
int a[1000];
int dp;

int DFS(int step,int x)
{
     if(a[step]==n) return 1;

    if(step>=dp) return 0;

    x=max(x,a[step]);

    if(x*(1<<(dp-step))<n) return 0;

    for(int i=0;i<=step;i++)
    {
        a[step+1]=a[step]+a[i];

        if(DFS(step+1,x)) return 1;

        if(a[step]>a[i]) a[step+1]=a[step]-a[i];
        else a[step+1]=a[i]-a[step];

        if(DFS(step+1,x)) return 1;
    }

    return 0;
    }
int main()
{
    while(cin>>n&&n)
    {
        a[0]=1;
        if(n==1) cout<<0<<endl;
        else
        {
        for(dp=1;;dp++)
        {
        if(DFS(0,1)) break;
        }
        cout<<dp<<endl;
        }
    }
    return 0;
}
时间: 2024-08-03 23:49:18

第二周习题F的相关文章

第二周习题O题

Description Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in V, then the bandwidth of a node v is defined as the maximum distance in the ordering between v and any node to which it is con

# 20175329 2018-2019-2 《Java程序设计》 第二周学习总结

学号 2018-2019-3<Java程序设计>第三周学习总结 教材学习内容总结? ? ? 第二三章与我们所学习的C语言有很多的相似点,在这里我想主要就以我所学习的效果来讨论一下JAVA与C语言的不同之处? ? ? ·数组使用方式不同? ? ? ? ? ? ? ? 在C语言中设置数组需要在设置变量后加上数组的容量,但是在JAVA中不能添加容量因为在JAVA中数组作为动态变量其大小可以变化? ? ? ·for语句? ? ? ? ? ? ? ? 可以定义变量类型并且在之前的学习中也没有见到过for

20155336 2016-2017-2《JAVA程序设计》第二周学习总结

20155336 2016-2017-2 <JAVA 程序设计>第二周学习总结 教材学习内容 1: GIT版本检测 2: JAVA中基本类型 整数 字节 浮点数 字符 布尔(▲) 通过API可以得知各个类型可存储的数值范围 public class Range {public static void main(String[] args){ //byte.short.int.long的范围 System.out.printf("%d~%d%n", Byte.MIN_VALU

20145123刘森明《Java程序设计》第二周学习总结

教材学习内容总结 这一章学习的知识,在以前的C语言中已经学习过了.所以学起来比较的轻松.主要讲的就是数据与变量的类型和运算符:流程控制等知识点. 教材学习中的问题和解决过程 这周在Java上花费的时间较多.首先对于java语言中,虽然之前对于java的语言的语法有所了解,但是对于具体的细节还存在问题.对于输出函数System.out.printf()和Syetem.out.println()的区别,看书后得知Syetem.out.println()函数在编译后会换行,在第一章的“hello wo

解题报告——2018级2016第二学期第二周作业

解题报告——2018级2016第二学期第二周作业 D:迷宫问题 题目描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. 输入 一个5 × 5的二维数组,表示一个迷宫.数据保证有唯一解. 输出 左上角到右

马哥linux 培训第二周作业

注意:第二周作业,请将以下题目整理在51cto博客当中,完成后请将对应的博文链接地址提交在答案栏中,提交格式如下:学号+姓名+博文链接地址eg:1+张三+http://mageedu.blog.51cto.com/4265610/1794420 本周作业内容:1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示. 文件管理的命令有cp.mv.rm 复制命令:cp 用法: cp [-adfilprsu] 来源文件(source) 目标文件(destination) cp [o

20145301第二周学习总结

20145301第二周学习总结 教材学习内容总结 3.1 类型.变量与运算符 整数 short 2字节,int 4字节,long 8字节 字节 byte 1字节 浮点数 float 4字节,double 8字节 字符 char 2字节(包括字母.汉字) 布尔 boolean 不用在意它的存储空间(只有true/false) 注释 // 单行注释,/ / 多行注释 下图为各类型范围: 常量定义 final加在定义类型之前,表示定义的该变量将不能改变其值,如 final int a=10; 局部变量

20145326《Java程序设计》第二周学习总结

20145326<Java程序设计>第二周学习总结 教材学习内容总结 本周学习教材第三章,本章主要讲述了java语言中的一些基础语法,java是个支持面向对象的程序语言,但在正式进入面向对象支持语法的探讨前,对于类型.变量.运算符.流程控制等,这些各种程序语言都会有的基本语法元素,还是要有一定的基础.虽然各种程序语言都有这些基本语法元素,但千万别因此而轻忽它们,因为各种程序语言都有其诞生的宗旨与演化过程,对这些基本语法元素,也就会有其独有的特性. 1. 类型 在java的世界中,并非每个东西都

HarvardX: PH525.4x Introduction to Bioconductor第二周笔记

PH525.4x第二周内容围绕GRange类的操作和使用Annotation进行数据关联两个主题展开,并展示了几个比较“炫”的功能.由于内容繁多,信息量大,故笔记之以便日后参考.该课程的演示数据为ChIP-seq的实验数据,背景为人类肝细胞(cell line:HepG2和GM12878)中被ESRRA (estrogen related receptor alpha)绑定的基因片段.在展示数据操作之前,首先检查bioconductor的版本号,不同版本的输出可能存在差异. library(Bi