Poj3134 power calculus iddfs

Source: Internet
Author: User

Returns the maximum number of steps of multiplication and division. x ^ N can be obtained.

In fact, the answer is a maximum of 13, but because the branches of the tree are extremely large, while iddfs, we need to add two pruning

1 If the maximum value of the current sequence is M * 2 ^ (DEP-k) <n, then this branch is subtracted.

2. If there are two numbers greater than N, the Branch is subtracted. Because there is only one useful one, we can get the answer through another shorter path.

The program looks very slow, over Ms

This question can be directly typed into the table N up to 1000

Power Calculus
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:1526   Accepted:787

Description

StartingXAnd repeatedly multiplyingX, We can computeX31 with thirty multiplications:

X2 =X×X,X3 =X2 ×X,X4 =XX,...,X31 =X30 ×X.

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

X2 =X×X,X3 =X2 ×X,X6 =XX3,X7 =XX,X14 =XX7,X15 =X14×X,X30 =X15 ×X15,X31 =X30 ×X.

This is not the shortest sequence of multiplications to computeX31. There are always ways with only seven multiplications. The following is one of them:

X2 =X×X, X4 =X2 ×X2,X8 =XX4,X8 =XX4,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 computeX31 with six operations (five multiplications and one Division ):

X2 =X×X,X4 =X2 ×X2,X8 =XX4,X16 =X8 ×X8,X32 =X16 ×X16,X31 =X32 bytesX.

This is one of the most efficient ways to computeX31 if a division is as fast as a multiplication.

Your mission is to write a program to find the least number of operations to computeXNBy multiplication and division startingXFor the given positive integerN. Products and quotients appearing in the sequence
Shocould beXTo a positive integer's power. In others words,X−3, for example, shocould never appear.

Input

The input is a sequence of one or more lines each containing a single integerN.NIs positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output

Your program shocould print the least total number of multiplications and divisions required to computeXNStartingXFor the integerN. The numbers shoshould be written each in a separate line without any superfluous
Characters such as leading or trailing spaces.

Sample Input

13170914735128119530

Sample output

06891191312
#include<iostream>#include<cstring>#include<algorithm>#include<string>#include<cstdio>using namespace std;int dep,n;int a[20];bool iddfs(int cnt,int maxs){    if(a[cnt]==n) return true;    if(cnt>=dep) return false;    if(maxs>n && a[cnt]>n) return false;    maxs=max(maxs,a[cnt]);    if(maxs*(1<<(dep-cnt))<n) return false;    for(int i=0;i<=cnt;i++)    {        a[cnt+1]=a[cnt]+a[i];        if(iddfs(cnt+1,maxs)) return true;        if(a[cnt]>a[i]) a[cnt+1]=a[cnt]-a[i];        else a[cnt+1]=a[i]-a[cnt];        if(iddfs(cnt+1,maxs)) return true;    }    return false;}int main(){    while(~scanf("%d",&n)&&n)    {        if(n==1) printf("0\n");        else        {            a[0]=1;            for(dep=1;1;dep++)            {                if(iddfs(0,1))                {                    printf("%d\n",dep);                    break;                }            }        }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.