zoukankan      html  css  js  c++  java
  • 暴力求解——POJ 3134Power Calculus

    Description

    Download as PDF

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

    Sample Input 

    1
    31
    70
    91
    473
    512
    811
    953
    0
    

    Sample Output 

    0
    6
    8
    9
    11
    9
    13
    12
    

    解题思路:

    题目大意:求用一个x,如何在最小的步数使用已经用过的凑出x^n,其中n是1~1000的,可以搜索,很容易想到,<=0和>=2000的点应该直接剪掉。

    这个题目方根就是搜索的时候控制搜索的层数即可。

     if(a[c]*(1<<(t-c))<n)
            return false;    //这个约束条件必须加,剪枝,这个点按最大比例每次都乘以2扩充也扩充不到n,那么就剪掉。

    程序代码:

    #include <iostream>
    using namespace std;
    int a[1010];
    int t,b,n;
    bool funt(int c)
    {
        if(c>t)
            return false;
        if(a[c]==n)
            return true;
        if(a[c]<=0||a[c]>10000)
            return false;
        if(a[c]*(1<<(t-c))<n)
            return false;
        for(int i=0;i<=c;i++)
        {
            a[c+1]=a[c]+a[i];
            if(funt(c+1))
                return true;
            a[c+1]=a[c]-a[i];
            if(funt(c+1))
                return true;
        }
        return 0;
    }
    
    int main()
    {
        while(cin>>n&&n)
        {
            if(n==1)
                cout<<0<<endl;
            else
            {
                t=1;
                while(1)
                {
                    a[0]=1;
                    if(funt(0))
                        break;
                    t++;
                }
                cout<<t<<endl;
            }
        }
        return 0;
        
    }
    View Code
    版权声明:此代码归属博主, 请务侵权!
  • 相关阅读:
    LVM快照备份与恢复
    Docker Images for MySQL Group Replication 5.7.14
    sysbench write and read only
    Leetcode: Closest Leaf in a Binary Tree
    Amazon | OA 2019 | Optimal Utilization
    物联网架构成长之路(27)-Docker练习之Zookeeper安装
    物联网架构成长之路(26)-Docker构建项目用到的镜像2
    物联网架构成长之路(25)-Docker构建项目用到的镜像1
    物联网架构成长之路(23)-Docker练习之Elasticsearch服务搭建
    物联网架构成长之路(22)-Docker练习之Etcd服务搭建
  • 原文地址:https://www.cnblogs.com/www-cnxcy-com/p/4694152.html
Copyright © 2011-2022 走看看