2541 Power operation

Source: Internet
Author: User

2541 Power Operation

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description Description

Starting with M, we only need 6 operations to calculate the M31:

M2=mxm,m4=m2xm2,m8=m4xm4,m16=m8xm8,m32=m16xm16,m31=m32÷m.

Please find out the minimum number of operations to calculate MN starting with M. At each step of the operation, it should be the positive integer number of M, in other words, similar m-3 is not allowed to occur.

Enter a description Input Description

Input as a positive integer n

Output description Output Description

The output is an integer that calculates the minimum number of operations for MN, starting with M.

Sample input Sample Input

Example 1
1

Example 2
31

Example 3
70

Sample output Sample Output

Example 1
0

Example 2
6

Example 3
8

Data range and Tips Data Size & Hint

N (1<=n<=1000)

There is no problem with the data, the N times that have occurred can be called directly

Category labels Tags Click here to expandDepth-First Search searchThe following:

The meaning of the iterative deepening search:

DFS is the first to specify the depth of Dfs, if the depth has not been the result, quit DFS,

Did not find, in this topic depth refers to the number of calculations, to achieve a good calculation of the number of times, in this number of times there is no result, returned to not find, for the kind of problem without the search boundary, you can do this

Because this topic does not say the most to calculate how many times, then if for a result has been the DFS calculation, not only has no boundary, and the number of calculations is not necessarily the least number of times. So use iterations to deepen your search.

So, for each search we limit the maximum number of operations we can do
So the size of the search is greatly reduced.
The same maintenance has been obtained for the MI array
The size of the array corresponds to several operations.
Plus a few pruning:
If the largest << (limit-k) in Mi can't reach n search failed
when creating a new MI, try to combine the large number so that it can also reduce the size

AC Code:

#include <cstdio>#defineMax (A, b) a>b?a:busing namespacestd;Const intn=101;intN,a[n];BOOLDfsintKintlimit) {    if(a[k]==n)return 1; if(K==limit)return 0; intmaxx=0;  for(intI=0; i<=k;i++) maxx=Max (maxx,a[k]); /*pruning, if each time the index * *, this is the biggest growth way, if this is still smaller than N, quit it*/    if(maxx<< (limit-k) <n)return 0;  for(inti=k;i>=0; i--) {/*The reverse loop is used here to speed up the calculation of large numbers, which can speed up the expansion .*/a[k+1]=a[i]+A[k]; if(Dfs (k +1, limit))return 1; A[k+1]=a[k]-A[i]; if(Dfs (k +1, limit))return 1; }    return 0;}intfind () {if(n==1)return 0; a[0]=1;  for(intI=1; i<= -; i++)if(Dfs (0, i))returnI/*deepening depth in turn*/}intMain () {scanf ("%d",&N); printf ("%d\n", find ()); return 0;}

2541 Power operation

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.