POJ 3134 Power Calculus (iterative deepening dfs+ strong pruning)

Source: Internet
Author: User

Description

starting with X and repeatedly multiplying to X, we can compute x31 with thirty multiplications:x2= xxx, x3 = x2xx, x4 = x3xx, ..., x31 =x30xx.the operation of squaring can be appreciably shorten the sequence of multiplications. The following isA-compute x31 with eight multiplications:x2= xxx, x3 = x2xx, x6 = x3xx3, X7 = x6xx, x14 = x7xx7, x15 = x14xx, x30 = x15xx15, x31 =X30xx.this isNot the shortest sequence of multiplications to compute x31. There is many ways with only seven multiplications. The following isOne of them:x2= xxx, x4 = x2xx2, x8 = x4xx4, x8 = x4xx4, x10 = x8xx2, x20 = x10xx10, x30 = x20xx10, x31 =X30xx.if Division isAlso available, we can find a even shorter sequence of operations. It ispossible 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 isOne of the most efficient ways to compute x31ifA division is  asFast asa multiplication. Your Mission isTo write a program to find the least number of operations to compute XN by multiplication and division starting with X forThe given positive integer n. Products and quotients appearinginchThe sequence should is x to a positive integer ' s power. In others words, x−3, forExample, should never appear.

Input

 is  is  +  is indicated by a zero.

Output

 for inch  as leading or trailing spaces.

Sample Input

1  to  -  the 473  + 811 953 0

Sample Output

0 6 8 9  One 9  -  A

Source

Japan 2006

Find out how many steps you can take with multiplication and division to get X^n

In fact, the answer is up to 13, but since the branches of the tree are very large in iddfs, we have to add 2 pruning

1 Subtract this branch if the current sequence maximum m*2^ (dep-k) <n

2 If there are two numbers greater than N, subtract the branch. Because there is only one useful in it, we can always get the answer by a shorter path.

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intnum;6 intway[1006];7 BOOLDfsintNintStep) {8     if(Num>step)return false;9     if(way[num]==n)return true;Ten     if(way[num]<< (step-num) <n)return false;//Strong Pruning One      for(intI=0; i<=num;i++){ Anum++; -way[num]=way[num-1]+Way[i]; -         if(way[num]<= +&& DFS (N,step))return true; the  -way[num]=way[num-1]-Way[i]; -         if(way[num]>0&& DFS (N,step))return true; -num--; +     } -     return false; + } A intMain () at { -     intN; -      while(SCANF ("%d", &n) = =1){ -         if(n==0){ -              Break; -         } in  -         //Iterate deepen DFS to         inti; +          for(i=0;; i++){ -way[num=0]=1; the             if(Dfs (n,i)) *                  Break; $         }Panax Notoginsengprintf"%d\n", i); -          the     } +     return 0; A}
View Code

POJ 3134 Power Calculus (iterative deepening dfs+ strong pruning)

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.