Original title: 1374-power calculus
Test instructions: The minimum number of times to multiply or divide is to get x^n from X. (You can only select two from the numbers you've got.)
Example: X^31 can be obtained by a minimum of 6 operations (5 times, 1 times except) x^2 = X*xx^4 = (x^2) * (x^2) x^8 = (x^4) * (x^4) x^16 = (x^8) * (x^8) x^32 = (x^16) * (x^16) x^31 = (x^32) ÷x
Analysis: As you can see, each time you choose two actions from the number you've got, you have the idea of enumerating. This problem is no obvious enumeration limit, so it is natural to think of using iterative deepening search algorithm. Because the data range of n is 1~1000, it is possible to calculate that the maximum number of enumeration levels Maxd is 13. And it can be found that if the current number num*2^ (maxd-d) < N, there is no need to continue searching, backtracking (num is the number obtained by the first D step) So our ida* algorithm idea is basically complete.
Further optimization:
If only rely on the above idea, write out the program to run 2.7s (upper limit is 3s), so belong to just good AC.
We have a lot of optimization methods here, I'll say two I used.
1. When looking for a power, we should not draw two from the number we have obtained each time, so that it is very inefficient. And it's easy to get together, every time we're working on the previous step, it's enough to enumerate the other operands.
1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intMaxd = -;5 intN, f[1<< (Maxd-1)], Maxd, a[ the];6 7 BOOLDfsintD) {8 if(A[d] = = N)return true;9 if(D < Maxd && (a[d]<< (maxd-d)) >=N) {Ten for(inti = D; I >=0; i--) One for(intj =0; J <2; J + +) { A intNextn = j? A[D] + a[i]: a[d]-a[d-i]; - if(Nextn <=0|| F[NEXTN])Continue; -F[NEXTN] =1; the if(Nextn <=0)Continue; -A[d +1] =Nextn; - if(Dfs (d +1))return true; -F[NEXTN] =0; + } - } + return false; A } at intMain () { -a[0] =1; - while(SCANF ("%d", &n) = =1&&N) { - if(n = =1) {printf ("0\n");Continue;} - for(Maxd =1; Maxd < Maxd; maxd++) { -Memset (F,0,sizeof(f)); inf[1] =1; - if(Dfs (0)) Break; to } +printf"%d\n", maxd); - } the return 0; *}
2. Play the table.
Because the range of n is 1~1000, we can use a slightly slower algorithm, calculate the results in advance, save to the file, and then paste into the submitted code.
For example, my code is
1 intMain () {2Freopen ("ans_table","W", stdout);3 /*4 some code.5 */6 for(n =1; N <= +; n++) {7 if(n = =1) {printf ("ans[%d] = 0;\n", i);Continue;}8 for(Maxd =1; Maxd < Maxd; maxd++) {9 /*Ten some code. One */ A if(Dfs (0,1)) Break; - } -printf"ans[%d] =%d;\n", I, maxd); the } - return 0; -}
This allows us to generate the file "ans_table" locally.
The answers are all in shape.
Ans[1] = 0;
ANS[2] = 1;
ANS[3] = 2;
ANS[4] = 2;
ANS[5] = 3;
ANS[6] = 3;
ANS[7] = 4;
ANS[8] = 3;
ANS[9] = 4;
ANS[10] = 4;
ANS[11] = 5;
ANS[12] = 4;
Equivalent to generating a table directly in the form of code. Speed is naturally 0ms
Power Calculus Fast Power calculation (ida*/)