zoukankan      html  css  js  c++  java
  • 寒假Day47:Power CalculusIDA*(dfs+剪枝)

    题面:

    Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:
    
    x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x.
    
    The operation of squaring can be appreciably shorten the sequence of multiplications. The following is a way to compute x31 with eight multiplications:
    
    x2 = x × x, x3 = x2 × x, x6 = x3 × x3, x7 = x6 × x, x14 = x7 × x7, x15 = x14 × x, x30 = x15 × x15, x31 = x30 × x.
    
    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 = x × x, x4 = x2 × x2, x8 = x4 × x4, x8 = x4 × x4, x10 = x8 × x2, x20 = x10 × x10, x30 = x20 × x10, x31 = x30 × x.
    
    If division is also available, we can find a even shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):
    
    x2 = x × x, x4 = x2 × x2, x8 = x4 × x4, x16 = x8 × x8, x32 = x16 × x16, 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 should be x to a positive integer’s power. In others 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.
    View Code

    测试数据:

    Sample Input
    1
    31
    70
    91
    473
    512
    811
    953
    0
    
    Sample Output
    0
    6
    8
    9
    11
    9
    13
    12

    题意:给出一个n,计算x-> x^n (通过乘或除运算)的最小步数

    剪枝条件:目前枚举到的步数+最坏情况还到不了n,就可以剪枝了

    剪枝部分看代码吧

    不会的话可以根据代码手动模拟一下。

    涉及算法:IDA*

    IDA*算法就是基于迭代加深的A*算法。

    A*算法是一种静态路网中求解最短路最有效的直接搜索方法。

    (其实我感觉就是一个dfs+剪枝)

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define inf 0x3f3f3f3f
    typedef long long ll;
    
    int n,ans;
    int a[35];
    
    bool dfs(int x, int y)
    {
        int w=y<<(ans-x);    
        if(x>ans||y<=0||w<n)//当当前数y在最坏情况下<n,说明到不了x^n
            return 0;
        if(y==n||w==n)
            return 1;
        a[x]=y;
        for(int i=0; i<=x; i++)
        {
            if(dfs(x+1,y+a[i])||dfs(x+1,y-a[i]))
                return 1;
        }
        return 0;
    }
    
    int main()
    {
        while(~scanf("%d",&n)&&n)//x-> x^n
        {
            for(int i=0; ; i++)//枚举次数,从0开始
            {
                ans=i;
                if(dfs(0,1))//说明x==(x^n)了
                    break;
            }
            printf("%d\n",ans);
        }
        return 0;
    }

     

  • 相关阅读:
    PostMan使用教程(1)
    测试工作量的评估方法
    Jmeter之Bean shell使用(二)
    centos7 编译安装redis
    Centos7 安装mariadb
    Centos7 安装使用virtualenvwrapper
    Centos7安装python3.6
    linux基础命令2
    linux基础命令
    linux目录结构
  • 原文地址:https://www.cnblogs.com/OFSHK/p/12458920.html
Copyright © 2011-2022 走看看