zoukankan      html  css  js  c++  java
  • poj3134 Power Calculus 迭代加深搜索

    Description

    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.

    题意

    (x)通过乘或除的操作最少多少次能得到(x^n)

    题解

    (A[i])为深度i时得到的(x)的次方,最多有(13)次可以到达最大值,从(1)(13)一次枚举,顺便剪枝:若每次最大次方乘最大次方都无法到达答案,一定不满足,直接返回。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n;
    int A[20];
    int dfs(int d,int mxdep){
        if(A[d]==n)return 1;
        if(d==mxdep)return 0;
        int sz=A[0];
        for(int i=1;i<=d;i++)sz=max(sz,A[i]);
        if((sz<<(mxdep-d))<n)return 0;
        for(int i=d;i>=0;i--){
            A[d+1]=A[d]+A[i];
            if(dfs(d+1,mxdep))return 1;
            A[d+1]=A[d]-A[i];
            if(dfs(d+1,mxdep))return 1;
        }
        return 0;
    }
    int work(int x){
        if(x==1)return 0;
        A[0]=1;
        for(int i=1;i<13;i++)if(dfs(0,i))return i;
        return 13;
    }
    int main(){
        ios::sync_with_stdio(false);
        while(1){
            cin>>n;
            if(n==0)break;
            cout<<work(n)<<endl;
        }
    }
    
  • 相关阅读:
    IMWebConf 2017 官网彩蛋解谜
    解决SVG animation 在IE中不起作用
    百度大搜和度秘面经
    浅谈JavaScript原型与原型链
    听说2017你想写前端?
    如何制作icon-font小图标
    HTML5 CSS3 诱人的实例 :模仿优酷视频截图功能
    javaweb action无法跳转、表单无法跳转的解决方法
    hadoop备战:yarn框架的搭建(mapreduce2)
    liferay 指定默认首页
  • 原文地址:https://www.cnblogs.com/Nan-Cheng/p/9767598.html
Copyright © 2011-2022 走看看