• 第二周习题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;
    }
    

     

  • 相关阅读:
    React Native之Android应用开发IDE选项
    react-native环境配置——Android工程搭建
    《更换电池》
    canvas绘图是基于状态的绘图方式
    关于JSONP的一些概念
    iOS 学习笔记七 【博爱手把手教你使用2016年gitHub Mac客户端】
    iOS 学习笔记六 【APP中的文字和APP名字的国际化多语言处理】
    iOS 学习笔记五 【2016年百度地图定位详细使用方法】
    iOS 学习笔记四 【xcode 7.3 ESJsonFormat-Xcode 插件不能使用的解决办法】
    iOS 学习笔记三【segmentedControl分段控制器详细使用方法】
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4692971.html
Copyright © 2020-2023  润新知